如需转载,请保留本文链接。
核心思想:根据不同类的hashcode值不同,将其作为key值,将子弹存在对应key的list中。
核心方法如下:
//泛型添加子弹到dic void Add<T>(GameObject obj) where T:BulletBase { int hashcode = typeof(T).GetHashCode(); if (TSleepBulletDic.ContainsKey(hashcode)) { TSleepBulletDic[hashcode].Add(obj); } else { TSleepBulletDic.Add(hashcode, new List<GameObject>()); TSleepBulletDic[hashcode].Add(obj); } obj.SetActive(false); obj.transform.parent = BulletRoot.transform; } //return泛型子弹 GameObject getBullet<T>() { int hashcode = typeof(T).GetHashCode(); int TSleepBulletDic_ListCout = TSleepBulletDic[hashcode].Count; if (TSleepBulletDic.ContainsKey(hashcode)) { for (int i = 0; i < TSleepBulletDic_ListCout; i++) { if (!TSleepBulletDic[hashcode][i].activeSelf) { TSleepBulletDic[hashcode][i].transform.position = _instanceConfig.BulletBornPosition<T>(); TSleepBulletDic[hashcode][i].SetActive(true); ActiveBulletList.Add(TSleepBulletDic[hashcode][i]); BulletOverflow(); return TSleepBulletDic[hashcode][i]; } } } return null; }
如有需要,附百度云Unity Package下载地址:https://pan.baidu.com/s/1dFEnQUH 密码: tm6a