c#对象池技术

1.对象池脚本

public class NewObjectPool : MonoBehaviour {

    public GameObject _gameObject;//需要创建 的预制对象
    public int int_PooledAmout=15;//数量
    public Transform _transform;//容器
    public bool bool_IsGrow =true;//是否动态创建
   List<GameObject> _gameObjects;

    private void Awake() {
        _gameObjects=new List<GameObject>();
        for(int i=0;i<int_PooledAmout;i++){
            GameObject obj =(GameObject)Instantiate(_gameObject);
            obj.transform.SetParent(_transform);
            obj.transform.localScale =Vector3.one;
            obj.transform.position=_transform.position;
            obj.transform.rotation=_transform.rotation;
            obj.SetActive(false);
            _gameObjects.Add(obj);
        }
    }

    public void Reset(){
        for (int i = 0; i < _gameObjects.Count; i++)
        {
            _transform.GetChild(i).gameObject.SetActive(false);
        }
    }

    public GameObject GetPooledGameObject(){
        for(int i=0;i<_gameObjects.Count;i++){
            if(!_gameObjects[i].activeInHierarchy){
                return _gameObjects[i];
            }
        }
        if(bool_IsGrow){
            GameObject obj = (GameObject)Instantiate(_gameObject);
            obj.transform.SetParent(_transform);
            obj.transform.localScale = Vector3.one;
            obj.transform.position = _transform.position;
            obj.transform.rotation = _transform.rotation;
            _gameObjects.Add(obj);
            return obj;
        }
        return null;
    }
}

2.DataClass嵌套类

public class DataClass
{
    public class GoodData
    {
        public string id;
        public string name;
        public string info;
        public int price;
        public bool isSelled;
        public string icon;
    }
}

猜你喜欢

转载自www.cnblogs.com/xiaomao21/p/9080996.html
今日推荐