;D 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#msg10010 (http://www.tasharen.com/forum/index.php?topic=1979.msg10010#msg10010)
With 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:And the memory of unused texture will be free in *Editor* mode!
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() { Resources.UnloadUnusedAssets(); }
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!Yes, you found it! UIDrawCall doesn't release at run-time! it was just disable! still weird about it ? hahah
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); } } }
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) { } }
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!.