gameObject.renderer.enabled //是控制一個物體是否在屏幕上渲染或顯示 而物體實際還是存在的 只是想當(dāng)于隱身 而物體本身的碰撞體還依然存在的GameObject.Destroy() //表示移除物體或物體上的組件 代表銷毀該物體 實際上該物體的內(nèi)存并沒有立即釋放 而是在你下下個場景中槽釋放內(nèi)存資源,就是你a場景中Destroy了 一般是在c場景中才真正釋放該物體的內(nèi)存資源(這是我的體會 不知道理解錯誤沒)gameObject.active //是否在場景中停用該物體 在你gameObject.active =false中 則你在場景中用find找不到該物體 //如果該物體有子物體 你要用SetActiveRecursively(false) 來控制是否在場景中停用該物體(遞歸的)
我們要介紹是銷毀物體Destroy()
1 using UnityEngine; 2 using System.Collections; 3 4 public class acc : MonoBehaviour { 5 6 // Use this for initialization 7 public Transform Q; 8 int speed=50; 9 void Start () {10 11 }12 13 // Update is called once per frame14 void Update () {15 float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;//左右移動16 float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;// 前后移動17 //主攝像機物體 移動18 transform.Translate(x,0,z);19 20 if(Input.GetKeyDown(KeyCode.Mouse0))21 {22 23 //實列化子彈24 Transform n = Instantiate(Q) as Transform;25 //發(fā)射子彈的位置為物體的位置26 n.position = transform.position;27 28 Vector3 f = transform.TransformDirection(Vector3.forward);29 n.gameObject.rigidbody.AddForce(f*3000);30 Destroy(n.gameObject,5);31 //Destroy(n.gameObject);32 //Destroy(gameObject);33 }34 35 36 }37 }5秒后 銷毀物體
Destroy(n.gameObject,5);
立即銷毀物體
Destroy(n.gameObject);
如果文件綁定在物體上用
Destroy(gameObject);
來銷毀物體
Unity資源內(nèi)存申請和釋放
GameObject, Transform, Mesh, Texture, Material, Shader, Script和各種其他Assets。
IEnumerator LoadResources() { // 清除干凈以免影響測試結(jié)果 Resources.UnloadUnusedAssets(); // 等待5秒以看到效果 yield return new WaitForSeconds(5.0f); // 通過Resources.Load加載一個資源 GameObject tank = Resources.Load("Role/Tank") as GameObject; yield return new WaitForSeconds(0.5f); // Instantiate一個資源出來 GameObject tankInst = GameObject.Instantiate(tank, Vector3.zero, Quaternion.identity) as GameObject; yield return new WaitForSeconds(0.5f); // Destroy一個資源 GameObject.Destroy(tankInst); yield return new WaitForSeconds(0.5f); //釋放無用資源 tank = null; Resources.UnloadUnusedAssets(); yield return new WaitForSeconds(0.5f); }執(zhí)行結(jié)果如下:
下面是統(tǒng)計結(jié)果:
| 數(shù)據(jù)描述 | Memory | Texture | Mesh | Material | GameObjects | Objects in Scene | Total Objects |
|---|---|---|---|---|---|---|---|
| 初始 | 72.8M | 1271/8.0M | 35/223.0K |
學(xué)習(xí)交流
熱門圖片
猜你喜歡的新聞
新聞熱點 2019-10-23 09:17:05
2019-10-21 09:20:02
2019-10-21 09:00:12
2019-09-26 08:57:12
2019-09-25 08:46:36
2019-09-25 08:15:43
疑難解答
|