【问题标题】:Unity Remove AssetUnity 移除资产
【发布时间】:2016-05-24 03:12:26
【问题描述】:

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

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 的/组件或 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);

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    卸载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。不要认为它会有所作为,只是以防万一。

    【讨论】:

    • 使用带有大写 o 的对象会返回兼容性错误,同样使用建议的 unloadAsset 也会返回相同的错误
    • Object' 是 UnityEngine.Object' and object' 之间的模糊引用
    • @Sniffle6 修改了我的答案。再看一遍。如果出现错误,请记下您从中得到错误的代码行。
    • 我使用了 UnityEngine.Object,它没有返回模棱两可的错误,但我仍然得到 UnloadAsset 只能用于单个资产,并且在我使用卸载时不能用于 GameObject 的/组件或 AssetBundles 跨度>
    • 用新代码/错误更新了原始问题
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 2022-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多