0

我找到了一个很棒的对象池脚本,但它在 UnityScript 中,而我的项目在 C# 中。我一直在尝试转换它,但我遇到了一个我不太理解的错误。这是我的脚本:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EasyObjectPool : MonoBehaviour {
    private class PoolInfo{

    public string poolName;
    public GameObject prefab;
    public int poolSize;
    public bool canGrowPoolSize = true;

}

private class Pool{

    private List<PoolObject> list = new List<PoolObject>();
    public bool  canGrowPoolSize;

    public void  Add (PoolObject poolObject){
        list.Add(poolObject);
    }

    public int Count (){
        return list.Count;
    }


    public GameObject ObjectAt ( int index  ){

        PoolObject result;
        if(index < list.Count) {
            result = list[index];
        }

        return result;

    }
}
static public EasyObjectPool instance ;
PoolInfo[] poolInfo;

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 GameObject 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;
}

}

错误是:“无法将类型‘UnityEngine.GameObject’隐式转换为‘PoolObject’”

对于行:

PoolObject currentObject = pool.ObjectAt(index);

return poolObject;

该脚本引用了另一个名为 PoolObject 的脚本,但它没有给我任何错误,所以我不认为它与这些错误有关。这是那个脚本:

using UnityEngine;
using System.Collections;

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



}
}

有人告诉我我在这里做错了什么吗?

4

2 回答 2

0

问题 1

在函数GetObjectFromPool()中,您试图返回 aGameObject但实际上您正在给它 a PoolObject

所以改变这一行:

public GameObject GetObjectFromPool(string poolName)
{
...
}

对此:

public PoolObject GetObjectFromPool(string poolName)
{
...
}

现在您正在返回一个PoolObject类型。

问题 2

同样的事情发生在ObjectAt

public GameObject ObjectAt(int index)
{           
    PoolObject result = null;

    if(index < list.Count)
       result = list[index];

    return result;      
}

只需再次交换GameObjectPoolObject您就完成了。


可预见的未来问题

将会有另一个调用. GetObjectFromPool()_gameObjectPoolObject

于 2014-06-25T21:13:55.497 回答
-1

pool.ObjectAt返回一个游戏对象,而不是类型的附加组件PoolObject

改成ObjectAt这样:

public PoolObject ObjectAt ( int index  ){

    PoolObject result;
    if(index < list.Count) {
        result = list[index];
    }

    return result;

}
于 2014-06-25T21:15:26.830 回答