简体   繁体   English

从Unity Asset Bundle中销毁已加载的资产

[英]Destroying loaded assets from a Unity Asset Bundle

I've got an asset bundle loading in my project and I'm adding all of them to a list so that I can iterate through each of the individual object inside the asset bundle. 我在项目中加载了一个资产束,并将它们全部添加到列表中,以便可以遍历资产束中的每个单独对象。 However, I'm having trouble deleting the loaded object when I no longer need it in my scene. 但是,当我在场景中不再需要加载的对象时,无法删除它。

In my research I know of the Bundle.UnloadAll but from what I've read it destroys the entire bundle which I don't want. 在我的研究中,我知道Bundle.UnloadAll,但是根据我的阅读,它破坏了我不想要的整个捆绑。 Right now my code looks like this: 现在,我的代码如下所示:

if(GUI.Button(new Rect(10,130,100,50), "Forward"))
{
    if( index > 0 && object_List[index] != null)
    {
        Destroy((GameObject)object_List[index]);
    }

    Instantiate((GameObject)object_List[index]);
    index ++;
}

This code iterates through my list containing the loaded in asset bundle objects and should spawn the next one in the list. 此代码遍历包含加载的资产捆绑对象的列表,并应生成列表中的下一个。 At the same time, it should destroy the previously loaded one. 同时,它应该销毁先前装载的武器。 But when I run this code, I get the following error: 但是,当我运行此代码时,出现以下错误:

Destroying assets is not permitted to avoid data loss. 禁止销毁资产,以避免数据丢失。 If you really want to remove an asset use DestroyImmediate (theObject, true); 如果您确实要删除资产,请使用DestroyImmediate(theObject,true);

So I change my code to its suggestion and I run into this error: 所以我将代码更改为建议,然后遇到此错误:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. MissingReferenceException:'GameObject'类型的对象已被破坏,但您仍在尝试访问它。 Your script should either check if it is null or you should not destroy the object. 您的脚本应检查其是否为null或不破坏该对象。

However, nothing gets removed from my list and the first object to get spawned still remains. 但是,没有任何东西从我的列表中删除,第一个要生成的对象仍然保留。

Has anyone ever ran into a similar problem? 有没有人遇到过类似的问题? Is what I'm trying to do even possible? 我要做什么甚至有可能吗?

Any help would be appreciated. 任何帮助,将不胜感激。

The mistake you're making, is that you're trying to destroy the asset, not the ingame gameObject. 您犯的错误是您要破坏资产,而不是游戏中的GameObject。 When you call Instantiate((GameObject)object_List[index]); 当您调用Instantiate((GameObject)object_List[index]); it returns a reference to the loaded object, so for example : 它返回对已加载对象的引用,例如:

GameObject myObject = Instantiate((GameObject)object_List[index]);

will give you the object, and later you'll want to call Destroy(myObject); 将给您对象,稍后您将要调用Destroy(myObject); to destroy it. 摧毁它。

Instead of destroying them, you could perhaps try disabling / enabling the gameobjects when needed (though it's not clear why you're destroying them, so this might not serve your purposes). 除了销毁它们之外,您还可以尝试在需要时禁用/启用游戏对象(尽管尚不清楚为什么销毁它们,因此这可能无法达到您的目的)。

if(GUI.Button(new Rect(10,130,100,50), "Forward"))
{
    if( index > 0 && object_List[index] != null)
    {
        ((GameObject)object_List[index]).SetActive(false);
    }

    ((GameObject)object_List[index]).SetActive(true);
    index ++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM