我在将对象池脚本从UnityScript转换为C#时遇到了麻烦,在这里我得到了很多帮助。现在我在尝试从池中实际获取游戏对象时遇到问题。我有三个互相影响的脚本,所以我不太确定哪里出了问题。这是对象池的两个脚本,我相信它们都是平方的,并且没有给出任何错误:public class EasyObjectPool : MonoBehaviour { [System.Serializable] public class PoolInfo{ [SerializeField] public string poolName; public GameObject prefab; public int poolSize; public bool canGrowPoolSize = true;}[System.Serializable]public class Pool{ public List<PoolObject> list = new List<PoolObject>(); public bool canGrowPoolSize; public void Add (PoolObject poolObject){ list.Add(poolObject); } public int Count (){ return list.Count; } public PoolObject ObjectAt ( int index ){ PoolObject result = null; if(index < list.Count) { result = list[index]; } return result; }}static public EasyObjectPool instance ;[SerializeField]PoolInfo[] poolInfo = null;private Dictionary<string, Pool> poolDictionary = new Dictionary<string, Pool>();void Start () { instance = this; CheckForDuplicatePoolNames(); CreatePools();}private void CheckForDuplicatePoolNames() { for (int index = 0; index < poolInfo.Length; index++) { string poolName= poolInfo[index].poolName; if(poolName.Length == 0) { Debug.LogError(string.Format("Pool {0} does not have a name!",index)); } for (int internalIndex = index + 1; internalIndex < poolInfo.Length; internalIndex++) { if(poolName == poolInfo[internalIndex].poolName) { Debug.LogError(string.Format("Pool {0} & {1} have the same name. Assign different names.", index, internalIndex)); } } }}private void CreatePools() { foreach(PoolInfo currentPoolInfo in poolInfo){ Pool pool = new Pool(); pool.canGrowPoolSize = currentPoolInfo.canGrowPoolSize; for(int index = 0; index < currentPoolInfo.poolSize; index++) { //instantiate GameObject go = Instantiate(currentPoolInfo.prefab) as GameObject; PoolObject poolObject = go.GetComponent<PoolObject>(); if(poolObject == null) { Debug.LogError("Prefab must have PoolObject script attached!: " + currentPoolInfo.poolName); } else { //set state poolObject.ReturnToPool(); //add to pool pool.Add(poolObject); } } Debug.Log("Adding pool for: " + currentPoolInfo.poolName); poolDictionary[currentPoolInfo.poolName] = pool; }}public PoolObject GetObjectFromPool ( string poolName ){ PoolObject poolObject = null; if(poolDictionary.ContainsKey(poolName)) { Pool pool = poolDictionary[poolName]; //get the available object for (int index = 0; index < pool.Count(); index++) { PoolObject currentObject = pool.ObjectAt(index); if(currentObject.AvailableForReuse()) { //found an available object in pool poolObject = currentObject; break; } } if(poolObject == null) { if(pool.canGrowPoolSize) { Debug.Log("Increasing pool size by 1: " + poolName); foreach (PoolInfo currentPoolInfo in poolInfo) { if(poolName == currentPoolInfo.poolName) { GameObject go = Instantiate(currentPoolInfo.prefab) as GameObject; poolObject = go.GetComponent<PoolObject>(); //set state poolObject.ReturnToPool(); //add to pool pool.Add(poolObject); break; } } } else { Debug.LogWarning("No object available in pool. Consider setting canGrowPoolSize to true.: " + poolName); } } } else { Debug.LogError("Invalid pool name specified: " + poolName); } return poolObject;}}和:public class PoolObject : MonoBehaviour {[HideInInspector]public bool availableForReuse = true;void Activate () { availableForReuse = false; gameObject.SetActive(true);}public void ReturnToPool () { gameObject.SetActive(false); availableForReuse = true;}public bool AvailableForReuse () { //true when isAvailableForReuse & inactive in hierarchy return availableForReuse && (gameObject.activeInHierarchy == false);}}最初的UnityScript表示要使用以下语句从池中检索对象:var poolObject : PoolObject = EasyObjectPool.instance.GetObjectFromPool(poolName);这就是我尝试在射击脚本中尝试从游泳池发射子弹预制棒的方法:public class ShootScript : MonoBehaviour {public PoolObject poolObject;private Transform myTransform;private Transform cameraTransform;private Vector3 launchPosition = new Vector3();public float fireRate = 0.2f;public float nextFire = 0;// Use this for initializationvoid Start () { myTransform = transform; cameraTransform = myTransform.FindChild("BulletSpawn");}void Update () { poolObject = EasyObjectPool.instance.GetObjectFromPool<poolName>(); if(Input.GetButton("Fire1") && Time.time > nextFire){ nextFire = Time.time + fireRate; launchPosition = cameraTransform.TransformPoint(0, 0, 0.2f); poolObject.Activate(); poolObject.transform.position = launchPosition; poolObject.transform.rotation = Quaternion.Euler(cameraTransform.eulerAngles.x + 90, myTransform.eulerAngles.y, 0); }}}我的拍摄脚本给了我两个错误:1.找不到类型或名称空间名称“ poolName”。您是否缺少using指令或程序集引用?对于该行:poolObject = EasyObjectPool.instance.GetObjectFromPool<poolName>();2.'PoolObject.Activate()'由于其保护级别而无法访问对于该行:poolObject.Activate();我误翻译了UnityScript还是错过了其他东西?任何输入,不胜感激 最佳答案 如果函数是通用的,则在中编写的内容应为类似于PoolObject的类名,而不是通用的。所以要解决这个问题,您只需要更改poolObject = EasyObjectPool.instance.GetObjectFromPool<poolName>();至string poolName = "thePoolNameYouWant";poolObject = EasyObjectPool.instance.GetObjectFromPool(poolName);该功能默认情况下是私有的,因此要解决“由于其保护级别而无法访问”的错误,您需要通过更改此功能将该功能公开void Activate () { availableForReuse = false; gameObject.SetActive(true);}对此public void Activate () { availableForReuse = false; gameObject.SetActive(true);} 08-06 19:45