0

優れたオブジェクト プーリング スクリプトを見つけましたが、それは UnityScript で作成されており、私のプロジェクトは C# で作成されています。変換しようとしましたが、よくわからないエラーが 1 つあります。これが私のスクリプトです:

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()では を返そうとしてGameObjectいますが、実際には を与えてい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;      
}

もう一度交換GameObjectするだけで完了です。PoolObject


予想される将来の問題

を呼び出す別のクラスがあるGetObjectFromPool()ため、そのクラスがGameObjectgameObjectを必要とする場合は、からコンポーネントを取得するようにしてくださいPoolObject

于 2014-06-25T21:13:55.497 に答える