UnityGUI(五)GUI控件综合使用

cs 复制代码
public event UnityAction clickEvent;

protected override void StyleOffDraw()
 {
     if( GUI.Button(guiPos.Pos, content ) )
     {
         clickEvent?.Invoke();
     }
 }

protected override void StyleOnDraw()
 {
    if (GUI.Button(guiPos.Pos, content, style))
     {
         clickEvent?.Invoke();
     }
 }

Invoke() ≈ "执行委托里的所有方法"

clickEvent?.Invoke() ≈ "如果有方法在里面,就执行它们"

clickEvent.Invoke() + 手动判空 ≈ 同上,但麻烦且容易出错

cs 复制代码
public class CustomGUIToggleGroup : MonoBehaviour
{
    public CustomGUIToggle[] toggles;

    //记录上一次为true的 toggle
    private CustomGUIToggle frontTurTog;

    void Start()
    {
        if (toggles.Length == 0)
            return;

        //通过遍历 来为多个 多选框 添加 监听事件函数
        //在函数中做 处理 
        //当一个为true时 另外两个变成false
        for (int i = 0; i < toggles.Length; i++)
        {
            CustomGUIToggle toggle = toggles[i];
            toggle.changeValue += (value) =>
            {
                //当传入的 value  是ture时 需要把另外两个 
                //变成false
                if( value )
                {
                    //意味着另外两个要变成false
                    for (int j = 0; j < toggles.Length; j++)
                    {
                        //这里有闭包  toggle就是上一个函数中申明的变量
                        //改变了它的生命周期
                        if( toggles[j] != toggle )
                        {
                            toggles[j].isSel = false;
                        }
                    }
                    //记录上一次为true的toggle
                    frontTurTog = toggle;
                }
                //来判断 当前变成false的这个toggle是不是上一次为true
                //如果是 就不应该让它变成false
                else if( toggle == frontTurTog)
                {
                    //强制改成 true
                    toggle.isSel = true;
                }
            };
        }
    }

}
相关推荐
郝学胜-神的一滴5 分钟前
Python 高级编程 026:内置数据结构之骈文纵论
开发语言·数据结构·python·程序人生·软件工程
隐积7 小时前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
名字还没想好☜10 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏11 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
海盗123411 小时前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
酷在前行12 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr82812 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
废弃的小码农13 小时前
功能测试--Day07--Python编程基础
开发语言·python
fengci.14 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
程序员zgh14 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++