【问题标题】:(Unity) Trying to convert an object pool script from UnityScript to C#- stuck on an error(Unity) 尝试将对象池脚本从 UnityScript 转换为 C#- 遇到错误
【发布时间】:2014-06-25 20:40:50
【问题描述】:

我找到了一个很棒的对象池脚本,但它在 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);



}
}

谁能告诉我我在这里做错了什么?

【问题讨论】:

  • 第 106 行和第 144 行是哪一个?在这些行上发布代码。
  • 对不起,我不知道这个代码函数没有给行编号。我编辑了我的问题
  • PoolObject 是不是您没有在上面粘贴的另一个类?
  • 这是实际实例化对象的脚本的名称,但它没有给我任何错误,所以我不认为它是相关的。我用那个脚本再次更新了我的问题。
  • 是的,我就是这么想的。我在下面的回答应该可以解决您的问题。祝你游戏好运!

标签: c# unity3d unityscript object-pooling


【解决方案1】:

问题 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(),因此请确保如果该类需要GameObject,则从PoolObject 中获取gameObject 组件。

【讨论】:

    【解决方案2】:

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

    ObjectAt 更改为:

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

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 2022-06-13
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2013-10-05
      • 2012-11-26
      • 2018-12-29
      相关资源
      最近更新 更多