Unity删除资产

时间:2016-05-23 18:19:40

标签: c# unity3d

我试图在将资源添加到对象数组后卸载资产,但它返回一个错误,说我一次只能删除一个,但我不知道该怎么做

object[] tempItems = Resources.LoadAll("Inventory/Weapons");
foreach (object i in tempItems)
{
    GameObject it = (GameObject)i;
    Debug.Log(it);
    itemList.Add(it);
    Debug.Log(itemList.Count);
}


foreach (object i in tempItems)
{
    GameObject it = (GameObject)i;
    Resources.UnloadAsset(it);
}

UnloadAsset只能用于单个资产,不能用于GameObject的/ Components或AssetBundles

我现在正在使用

    UnityEngine.Object[] tempItems = Resources.LoadAll("Inventory/Weapons");
    foreach (UnityEngine.Object i in tempItems)
    {
        GameObject it = (GameObject)i;
        Debug.Log(it);
        itemList.Add(it);
        Debug.Log(itemList.Count);
    }
    foreach (UnityEngine.Object i in tempItems)
    {
        Resources.UnloadAsset(i);
    }

但我仍然得到错误

UnloadAsset may only be used on individual assets and can not be used on GameObject's / Components or AssetBundles

这是错误的一行

        Resources.UnloadAsset(i);

1 个答案:

答案 0 :(得分:0)

卸载Object而不是GameObject。施放GameObject it = (GameObject)i;Resources.UnloadAsset(it);完全没必要,可能就是问题。

Object[] tempItems = Resources.LoadAll("Inventory/Weapons",typeof(GameObject));
foreach (Object i in tempItems)
{
    GameObject it = (GameObject)i;
    Debug.Log(it);
    itemList.Add(it);
    Debug.Log(itemList.Count);
}

foreach (Object i in tempItems)
{
    Resources.UnloadAsset(i);
}

我还建议您使用带有大写“O”的Object。不要以为它会有所作为,只是在案例中。