zoukankan      html  css  js  c++  java
  • 获取多个游戏对象上某个共同属性的思路

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

    思考前我的做法是:

    内部属性:

    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());
        }
    }
    

      

  • 相关阅读:
    2018 秋招找工作总结
    Java 实现 LRU 缓存
    历时2个月,星云链DApp开发总结
    Java 版快速排序 + 最挫的优化
    MacOS 下防止 rm 命令误删
    Java使用Log日志系统(common-logging和log4j)
    IDEA+Maven+Spring+SpringMVC+SpringJDBC整合Demo
    Java简单实现并发编程
    设计模式学习笔记——单例模式
    Java获取网页内容
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/9115252.html
Copyright © 2011-2022 走看看