zoukankan      html  css  js  c++  java
  • Unity 自动生成组件索引类工具

    Unity 自动生成组件索引类工具


    需求由来

    我们在写UI类时 需要获取预设中的组件

    joystick = transform.Find("joystick");
    background = transform.Find("joystick/background");
    stick = transform.Find("joystick/stick");
    direction = transform.Find("joystick/direction");
    

    缺点

    1. 需要手写代码
    2. 修改游戏对象名字需要手动修改代码
    3. 修改层级需要手动修改代码
    4. 手写容易出问题

    解决方案

    使用编辑器自动生成如下索引类

    public class MianBase : MonoBehaviour
    {
        protected UISprite Left_Sprite;
        protected UITexture Right_Texture;
        protected UISprite Bottom_Sprite;
        protected UILabel Top_Label;
    
        public void InitBase()
        {
            Left_Sprite = transform.Find("Left/Sprite").GetComponent<UISprite>();
            Right_Texture = transform.Find("Right/Texture").GetComponent<UITexture>();
            Bottom_Sprite = transform.Find("Bottom/Sprite").GetComponent<UISprite>();
            Top_Label = transform.Find("Top/Label").GetComponent<UILabel>();
        }
    }
    

    然后UI代码继承这个类
    就可以很方便的使用这些组件


    工具制作

    效果如图所示

    工具制作核心思想

    • 选中预设父级
    • 迭代获取子对象和子对象上的脚本
    • 索引变量名可自动填充可自定义
    • 序列化类

    工具使用方法

    1. 选择预设父级
    2. 左上角菜单栏ZouQiang/UI/创建预设索引类(快捷键Ctrl+Alt+1)打开窗口
    3. 自定义变量名或自动填充变量名
    4. 点击生成代码按钮

    如果您有更好的解决方案,请一定不吝赐教,感谢不尽!

    源码:https://github.com/QiangZou/ZouQiang/blob/master/Assets/ZouQiang/Editor/AssetBundlesTool.cs

  • 相关阅读:
    Python3之format
    xml文件整理
    某系统采集2018
    sublime+python3 中文环境
    python3文本读取与写入常用代码
    redis3.0集群使用发现的一些问题
    mysql字符集
    redis3.0集群搭建
    安装Maven、nexus
    一键安装mysql5.6
  • 原文地址:https://www.cnblogs.com/zouqiang/p/9585889.html
Copyright © 2011-2022 走看看