UGUI-Misc

作者:追风剑情 发布于:2019-7-3 21:24 分类:Unity3d

示例:演示了如何正确销毁对象

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public static class Misc
  6. {
  7. // 销毁对象
  8. static public void Destroy(UnityEngine.Object obj)
  9. {
  10. if (obj != null)
  11. {
  12. if (Application.isPlaying)
  13. {
  14. if (obj is GameObject)
  15. {
  16. GameObject go = obj as GameObject;
  17. go.transform.parent = null;
  18. }
  19.  
  20. Object.Destroy(obj);
  21. }
  22. else Object.DestroyImmediate(obj);
  23. }
  24. }
  25.  
  26. // 立即销毁对象
  27. static public void DestroyImmediate(Object obj)
  28. {
  29. if (obj != null)
  30. {
  31. if (Application.isEditor) Object.DestroyImmediate(obj);
  32. else Object.Destroy(obj);
  33. }
  34. }
  35. }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号