• 获取多个游戏对象上某个共同属性的思路


    假如,现在有个需求,实现获取游戏对象名字的代码?

    思考前我的做法是:

    内部属性:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ItemCtrl : MonoBehaviour
    {
        GameObject obj = null;
        public GameObject cachedGameObject
        {
            get
            {
                if (null == obj)
                    obj = gameObject;
                return obj;
            }
        }
    
        public string GetName()
        {
            return cachedGameObject.name;
        }
    }

    外部管理:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class LgsTest : MonoBehaviour
    {
        ItemCtrl[] arryItem;
    
        List<string> nameList = new List<string>();
    
        void CollectItemName()
        {
            nameList.Clear();
            for (int i = 0, iMax = arryItem.Length; i < iMax; ++i)
            {
                nameList.Add(arryItem[i].GetName());
            }
        }
    }
    

    思考后我的做法:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ItemCtrl : MonoBehaviour
    {
        GameObject obj = null;
        public GameObject cachedGameObject
        {
            get
            {
                if (null == obj)
                    obj = gameObject;
                return obj;
            }
        }
    
        public string GetName()
        {
            return cachedGameObject.name;
        }
    
    
        void OnEnable()
        {
            itemList.Add(this);
        }
    
        void OnDisable()
        {
            itemList.Remove(this);
        }
    
        static List<ItemCtrl> itemList = new List<ItemCtrl>();
        public static void CollectItemNames(ref List<string> nameList)
        {
            nameList.Clear();
    
            for (int i = 0, iMax = itemList.Count; i < iMax; ++i)
                nameList.Add(itemList[i].GetName());
        }
    }
    

      

  • 相关阅读:
    centos7 忘记mysql5.7密码
    阿里云Linux(Centos7)下搭建SVN服务器
    JAVA金额格式字符串转数值
    win10下RabbitMQ的安装和配置
    Oracle update 两表及以上关联更新,出现多值情况,不是一对一更新
    java.lang.OutOfMemoryError: java heap space
    bootstrap.min.css.map
    css 边距等常用设置
    html 标签
    数据库总结
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/9115252.html
Copyright © 2020-2023  润新知