I have some problems understanding what exactly "unused" means for Resources.UnloadUnusedAssets. From the docs:> An asset is deemed to be unused if it isn't reached after walking the whole game object hierarchy, including script components. Static variables are also examined.> The script execution stack, however, is not examined so an asset referenced only from within the script stack will be unloaded and, if necessary, loaded back in the next time one of its properties or methods is used. This requires extra care for assets which have been modified in memory. Make sure to call EditorUtility.SetDirty before an asset garbage collection is triggered.
I'm using C#, and my questions are:
- static variables: does it mean ALL static variables of ANY C# class in ANY loaded assembly? (public/private/protected)?
- what is meant by the "script stack"? is it the execution stack of c#?
- what about fields of normal C# instances (non MonoBehaviour) that reference C# instances corresponding to assets, and themselves are references from a MonoBehaviour on an existing GameObject. E.g. MyMonoBehaviour has a private field of type MyClass which has a field myTexture of type Texture2D. Is this deemed as "used" or "unused"?
------ Edit 1 ------
I mainly load resources dynamically using WWW, so at many times they are not (yet) referenced anywhere in the scene.
Yet still it's hard to construct a simple case where, based on my understanding of the docs, I could say in advance if the resource will be still valid at some point or not. Here is an example:
public class UnloadUnusedAssetsTest : MonoBehaviour {
class MyClass {
private Texture tex;
public MyClass(Texture tex) { this.tex = tex; }
public Texture Get() { return tex; }
}
private MyClass myClass;
IEnumerator Start () {
var path = "file://" + Application.dataPath + "/../../TestConfig/Images/ImageSmall_01.png";
var www = new WWW(path);
yield return www;
myClass = new MyClass (www.texture);
Debug.Log(myClass.Get().width);
www.Dispose();
www = null;
GC.Collect();
var unloader = Resources.UnloadUnusedAssets();
while (!unloader.isDone) {
yield return null;
}
Debug.Log(myClass.Get().width);
}
}
In the above example, I expect the texture referenced by myClass.tex to have been unloaded at the last line in the method, but it is not the case, even if:
- myClass.tex is not referenced anywhere in the scene
- myClass.tex is not directly available from any MonoBehaviour in the scene
- i did a cleanup of the loading WWW
- and waited for UnloadUnusedAssets() to finish.
------ Edit 2 ------
made an answer out of this edit.
Thanks!
↧