轻松取到UI上面的GameObject

本文介绍了一款Unity游戏开发中实用的GameObject引用管理组件。该组件能够简化GameObject的查找和管理过程,避免使用复杂的绝对路径。通过实现IEnumerable接口,支持键值对形式的数据存储与检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

           最近因为换工作,又有好几天没有写了,诶, 换个工作人都累的半死,so,跳槽有风险,需谨慎呀!

           下面写了一个我们在实际开发中会经常用到的组件,可以轻松的取到我们游戏界面中你想要的Gameobject,告别繁琐的Gameobject的超长的绝对路径。

 

 public class GameObjectRef : MonoBehaviour, IEnumerable<KeyValuePair<string, GameObject>>
    {
        public GameObject[] Refs;
        public string[]     Names;
        
        private Dictionary<string, GameObject> refMaps;
        
        public void Awake()
        {
            //Init();
        }
        
        public void Init()
        {
            int len = Refs != null ? Refs.Length : 0;
            refMaps = new Dictionary<string, GameObject>(len);
            for (int i = 0; i < len; i++)
                refMaps.Add(Names[i], Refs[i]);
        }
        
        public GameObject this[string key]
        {
            get
            {
                if (refMaps == null)
                    Init ();
                
                if (refMaps != null && refMaps.ContainsKey(key))
                    return refMaps[key];
                
                return null;
            }
            
            set
            {
                if (refMaps == null)
                    refMaps = new Dictionary<string, GameObject>(Refs != null ? Refs.Length : 0);
                
                if (refMaps.ContainsKey(key))
                {
                    int idx = Array.IndexOf(Names, key);
                    if (idx != -1)
                    {
                        refMaps[key] = value;
                        //Names[idx] = key;
                        Refs[idx] = value;
                    }
                }
                else
                {
                    int lenNames = Names != null ? Names.Length : 0;
                    int lenRefs = Refs != null ? Refs.Length : 0;
                    var newNames = new String[lenNames + 1];
                    var newRefs = new GameObject[lenRefs + 1];
                    if (Names != null && Refs != null)
                    {
                        Array.Copy(Names, newNames, lenNames);
                        Array.Copy(Refs, newRefs, lenRefs);
                    }
                    newNames[lenNames] = key;
                    newRefs[lenRefs] = value;
                    
                    refMaps.Add(key, value);
                    Names = newNames;
                    Refs = newRefs;
                }
            }
        }
        
        public IEnumerator<KeyValuePair<string, GameObject>> GetEnumerator()
        {
            return refMaps.GetEnumerator();
        }
        
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        
        public void Remove(string key)
        {
            if (refMaps == null || ! refMaps.ContainsKey(key))
                return;
            
            if (Names == null || Names.Length <= 0 ||
                Refs == null || Refs.Length <= 0)
                return;
            
            int idx = Array.IndexOf(Names, key);
            if (idx == -1)
                return;
            
            var newNames = new string[Names.Length - 1];
            var newRefs = new GameObject[Refs.Length - 1];
            Array.Copy(Names, 0, newNames, 0, idx);
            Array.Copy(Refs, 0, newRefs, 0, idx);
            if (idx < Names.Length - 1)
            {
                Array.Copy(Names, idx + 1, newNames, idx, Names.Length - idx - 1);
                Array.Copy(Refs, idx + 1, newRefs, idx, Refs.Length - idx - 1);
            }
            
            refMaps.Remove(key);
            Names = newNames;
            Refs = newRefs;
        }
        
        public void ChangeName(string oldName, string newName)
        {
            if (refMaps == null || ! refMaps.ContainsKey(oldName))
                return;
            
            if (Names == null || name.Length <= 0)
                return;
            
            int idx = Array.IndexOf(Names, oldName);
            if (idx == -1)
                return;
            
            GameObject go = refMaps[oldName];
            refMaps.Remove(oldName);
            refMaps.Add(newName, go);
            Names[idx] = newName;
        }
        
        public void Add(string name, GameObject go)
        {
            int lenNames = Names != null ? Names.Length : 0;
            int lenRefs = Refs != null ? Refs.Length : 0;
            
            var newNames = new String[lenNames + 1];
            var newRefs = new GameObject[lenRefs + 1];
            
            if (Names != null && Refs != null)
            {
                Array.Copy(Names, newNames, lenNames);
                Array.Copy(Refs, newRefs, lenRefs);
            }
            newNames[lenNames] = name;
            newRefs[lenRefs] = go;
            
            //refMaps.Add(name, go);
            Names = newNames;
            Refs = newRefs;
        }
    }


            真的很实用,希望能帮到你们!

------------------------------------------------------广告之后马上回来--------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值