Unity3D 优化 1 ( cs 实例化与内存变化)
2016-12-28 14:55
148 查看
1. 通过 public Object prefab 来实例化
测试
结论:
当删除点击按钮,删除go的时候,内存没有任何变化。
当删除点击按钮,删除go的时候,接着删除挂上Test.cs的go的时候,内存有变化。
当删除点击按钮,删除go的时候,同时把prefab = null,内存有变化。
理解:
因为当删除点击按钮,删除go的时候,Test.cs 还在引用prefab,所以,prefab并不是unused,
但是,当删除点击按钮,删除go的时候,接着删除挂上Test.cs的go的时候,prefab就没有任何引用了,所以,Resources.UnloadUnusedAssets();,就直接unload了prefab。内存就是变化了。
2. 直接实例化
结论
1. 用Resources.Load ,当删除点击按钮,删除go的时候,内存有变化。
2. 用UnityEditor.AssetDatabase.LoadAssetAtPath ,当删除点击按钮,删除go的时候,内存没有任何变化。
理解:
因为prefab是局部变量,那么,当删除go的时候,就prefab就没有任何的引用,那么Resources.UnloadUnusedAssets();,就直接unload了prefab。
UnityEditor.AssetDatabase.LoadAssetAtPath 可能在AssetDatabase内部,就引用了prefab,所以,Resources.UnloadUnusedAssets();就不能unload prefab
测试
using UnityEngine; using System.Collections; using CodeStage.AdvancedFPSCounter; public class Test : MonoBehaviour { public Object prefab; private GameObject go; // Use this for initialization void Start () { go = GameObject.Instantiate(prefab) as GameObject; } void OnGUI() { if (GUILayout.Button("xxxxxxxxx")) { GameObject.Destroy(go); go = null; Resources.UnloadUnusedAssets(); System.GC.Collect(); } } void OnDestroy() { Resources.UnloadUnusedAssets(); System.GC.Collect(); } }
结论:
当删除点击按钮,删除go的时候,内存没有任何变化。
当删除点击按钮,删除go的时候,接着删除挂上Test.cs的go的时候,内存有变化。
当删除点击按钮,删除go的时候,同时把prefab = null,内存有变化。
理解:
因为当删除点击按钮,删除go的时候,Test.cs 还在引用prefab,所以,prefab并不是unused,
但是,当删除点击按钮,删除go的时候,接着删除挂上Test.cs的go的时候,prefab就没有任何引用了,所以,Resources.UnloadUnusedAssets();,就直接unload了prefab。内存就是变化了。
2. 直接实例化
using UnityEngine; using System.Collections; using CodeStage.AdvancedFPSCounter; public class Test : MonoBehaviour { private GameObject go; // Use this for initialization void Start () { //string path = "xxxxx.prefab"; //Object prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<Object>(path); //go = GameObject.Instantiate(prefab) as GameObject; string path = "xxxxx"; Object prefab = Resources.Load<Object>(path); go = GameObject.Instantiate(prefab) as GameObject; } void OnGUI() { if (GUILayout.Button("xxxxxxxxx")) { GameObject.Destroy(go); go = null; Resources.UnloadUnusedAssets(); System.GC.Collect(); } } }
结论
1. 用Resources.Load ,当删除点击按钮,删除go的时候,内存有变化。
2. 用UnityEditor.AssetDatabase.LoadAssetAtPath ,当删除点击按钮,删除go的时候,内存没有任何变化。
理解:
因为prefab是局部变量,那么,当删除go的时候,就prefab就没有任何的引用,那么Resources.UnloadUnusedAssets();,就直接unload了prefab。
UnityEditor.AssetDatabase.LoadAssetAtPath 可能在AssetDatabase内部,就引用了prefab,所以,Resources.UnloadUnusedAssets();就不能unload prefab
相关文章推荐
- Unity3D 优化 2 ( lua 实例化与内存变化)
- 【Unity3D游戏开发】性能优化之Texture图片空间和内存占用分析(三七)
- Unity优化大全(九)之 Memory - Unity3D内部的内存
- Unity优化大全(九)之 Memory - Unity3D内部的内存
- Unity3D 优化 5 (AssetBundle的SerializedFile内存)
- 对象实例化,jvm执行时内存变化,
- Unity优化大全(八)之 Memory - Unity3D内部的内存
- Unity3D对于内存的管理与优化
- Unity3d 如何优化内存
- Unity3D游戏内存瘦身 UI优化是关键
- Unity优化大全(九)之 Memory - Unity3D内部的内存
- mips1处理器内存操作优化总结
- 优化SQL Server的内存占用之执行缓存
- 优化SQL Server的内存占用之执行缓存篇
- 提高&优化 CS(Half-life: Counter-Strike)反恐精英和反恐零点行动(condition zero)画面效果/画质
- XP 可关闭的服务 优化系统内存 减少内存占用...
- mips1处理器内存操作优化总结
- 提高&优化 CS(Half-life: Counter-Strike)反恐精英和反恐零点行动(condition zero)画面效果/画质
- 优化不是你想像-2:主动内存预取技术
- J2ME内存占用详解及优化方法