
Experience sharing, not question!
:(I found that when I changed my scene at run-time, the atlases that used in previous scene were still referenced and were not releasing at all!! that's wasting memory for me!
So I went through this topic, and I found one related topic:
http://www.tasharen.com/forum/index.php?topic=1979.msg10010#msg10010With solutions in this topic, I tried
1.panel.Refresh();
2.UIDrawCall.ReleaseAll();
and so on.
Finally I get it work at Editor with code below:
public void SwitchToScene()
{
Application.LoadLevel(1);
NGUIText.bitmapFont = null;
NGUIText.dynamicFont = null;
foreach (UIPanel panel in GameObject.FindObjectsOfType<UIPanel>())
{
panel.Refresh();
}
StartCoroutine(ClearScene());
}
IEnumerator ClearScene()
{
yield return new WaitForEndOfFrame
(); Resources.UnloadUnusedAssets();
}
And the memory of unused texture will be free in *Editor* mode!
But at run-time, the memory was not free at all!! so I keep going through, I found NGUITools.DestroyImmediate is different between editor and run-time vertion. In Editor, the object will destroy immediately, but at playing, the object will destroy at the end of the frame, so i add a 'WaitForEndOfFrame' coroutine at my code,
Yeah, it doesn't help, texture still there! I debugged that code through and found that Unity3D call my coroutine before UIDrawCall.OnDestroy!
static public void Destroy (UIDrawCall dc)
{
if (dc)
{
if (Application.isPlaying)
{
if (mActiveList.Remove(dc))
{
NGUITools.SetActive(dc.gameObject, false);
mInactiveList.Add(dc);
}
}
else
{
mActiveList.Remove(dc);
NGUITools.DestroyImmediate(dc.gameObject);
}
}
}
Yes, you found it! UIDrawCall doesn't release at run-time! it was just disable! still weird about it ? hahah
void OnDisable()
{
depthStart = int.MaxValue;
depthEnd = int.MinValue;
panel = null;
manager = null;
mMaterial = null;
mTexture = null;
NGUITools.DestroyImmediate(mDynamicMat);
mDynamicMat = null;
// !< WARN:codes that I add, the referenced material at mDynamicMat was released but not the real one!
if (mRenderer != null)
{
mRenderer
.sharedMaterials = new Material
[] { }; }
}
All of above is all my experience to free the UIAtlas memory.
Excuse for my poor English, and thanks for reading, I hope this will help you!.