unity3d Ugui 5.x学习GridLayoutGroup 脚本创建预设并改变外面容器的大小及单例模式

GridLayoutGroup组件

加载到游戏体上一般加载到 GameObject身上 它表示把内部游戏体的排列。其中cell Size x y 代表 内部游戏体的x的大小和y的大小。 


创建


编写脚本

采用单例模式

 private CommandPanel instance;


    public CommandPanel Instance
    {
        get {
            if (instance == null) 
            {
                instance = new CommandPanel();
            }
            return instance; 
        }
        set { instance = value; }
    }

public CommandPanel()
    {
        instance = this;
    }

单例模式的构造函数一般为私有的,但是这里 只能通过共有方法构造当前实例;

 public GameObject textNode;
    public IDictionary<string, UnityAction<string>> textListDele;
    public GameObject toggle; //预设 toggle
    public Transform content; //正文
    public RectTransform rectcontent; //正文大小
    public GridLayoutGroup grid; //容器
    public int addLineHeight = 25; 行高
    public int count;  //容器容纳的游戏体个数

//注册Text

  public void ZhuCeText(string label, UnityAction<string> funcBank) 
    {
        // textListDele.Add(label, funcBank);
          CreateText(label, funcBank);
    }

其中UnityAction<string> funcBank 委托方法

 public void CreateToggle(string label, UnityAction<bool> funcBank) 
    {
        count++;
        GameObject togglegame =Instantiate(toggle);           //初始化预设游戏体
        ToggleNode togglenode = togglegame.GetComponent<ToggleNode>();  //获得游戏体上自定义ToggleNode 组件属性
        togglenode.label.text = label;          //给自定义ToggleNode 组件label属性text 赋值
        togglenode.toggle.onValueChanged.AddListener(funcBank);    //给自定义ToggleNode 组件Toggle属性改变值时间 添加监听时间 (勾选框发生变化的时间 就触发监听事件)

        rectcontent.sizeDelta = new Vector2(rectcontent.sizeDelta.x, rectcontent.sizeDelta.y + addLineHeight);  //改变原来的位置
        grid.constraintCount = count; //grid 里面包含多少组件  
        togglenode.transform.parent = content;  //把构造的预设放到content下面

    }

如果给BUTTON添加事件 监听则

 //给button按钮添加事件监听
        textNodes.button.onClick.AddListener(delegate
        {
            ExcuteAction(textNodes.label.text, textNodes.inputtext.text);
        });

public void ExcuteAction(string label,string value) 
    {
        UnityAction<string> temp = null;
        textListDele.TryGetValue(label, out temp);//获取与指定的键相关联的值
        if (temp != null) 
        {
            temp(value);
        }
    }

调用 上面的注册事件

第一种方法 调用 使用拉姆塔表达式

 //void Update() 
    //{
    //    if (Input.GetKeyDown(KeyCode.A)) 
    //    {
    //        ZhuCeToggleFun("开启无敌", (bool isOn) =>
    //        {
    //            if (isOn)
    //            {
    //                Debug.Log("开启无敌");
    //            }else
    //            {
    //                Debug.Log("关闭无敌");
    //            }
    //        });
    //    }
    //}

第二种

 void Update() 
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            ZhuCeToggle("开启无敌", new UnityAction<bool>(Function)); //委托
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            ZhuCeText("获取礼包", new UnityAction<string>(GetGvie)); //委托
        }
    }
    public void GetGvie(string data) 
    {
        if (data == "123") 
        {
            Debug.Log("获取礼包");
        }
    }
    public void Function(bool isOn)
    {
        if (isOn)
        {
            Debug.Log("开启无敌");
        }
        else 
        {
            Debug.Log("关闭无敌");
        }
    }

相比用第一种方式最好。



猜你喜欢

转载自blog.csdn.net/u010773333/article/details/52101972