BaseObject.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. #if CODE_COVERAGE
  6. internal class BaseObject
  7. {
  8. public static T CreateInstance<T>()
  9. {
  10. return Activator.CreateInstance<T>();
  11. }
  12. public static void DestroyImmediate(object o)
  13. {
  14. if (o is BaseObject)
  15. {
  16. var obj = o as BaseObject;
  17. obj.OnDestroy();
  18. s_Objects.Remove(obj.GetInstanceID());
  19. }
  20. else if (o is UnityEngine.Object)
  21. {
  22. var obj = o as UnityEngine.Object;
  23. Undo.ClearUndo(obj);
  24. UnityEngine.Object.DestroyImmediate(obj);
  25. }
  26. }
  27. public static BaseObject InstanceIDToObject(int instanceID)
  28. {
  29. var obj = default(BaseObject);
  30. s_Objects.TryGetValue(instanceID, out obj);
  31. return obj;
  32. }
  33. private static Dictionary<int, BaseObject> s_Objects = new Dictionary<int, BaseObject>();
  34. private static int s_InstanceID = 0;
  35. private int m_InstanceID;
  36. public string name { get; set; }
  37. public HideFlags hideFlags = HideFlags.None;
  38. public BaseObject()
  39. {
  40. m_InstanceID = ++s_InstanceID;
  41. s_Objects.Add(m_InstanceID, this);
  42. }
  43. internal virtual void OnEnable() {}
  44. internal virtual void OnDestroy() {}
  45. public int GetInstanceID()
  46. {
  47. return m_InstanceID;
  48. }
  49. public override bool Equals(object other)
  50. {
  51. if ((other == null))
  52. return false;
  53. return object.ReferenceEquals(this, other);
  54. }
  55. public override int GetHashCode()
  56. {
  57. return m_InstanceID.GetHashCode();
  58. }
  59. public static bool operator==(BaseObject t1, BaseObject t2)
  60. {
  61. if (object.ReferenceEquals(t1, null))
  62. return object.ReferenceEquals(t2, null);
  63. return object.ReferenceEquals(t1, t2);
  64. }
  65. public static bool operator!=(BaseObject t1, BaseObject t2)
  66. {
  67. return !(t1 == t2);
  68. }
  69. }
  70. #else
  71. internal class BaseObject : ScriptableObject
  72. {
  73. public static void DestroyImmediate(object o)
  74. {
  75. if (o is UnityEngine.Object)
  76. {
  77. var obj = o as UnityEngine.Object;
  78. Undo.ClearUndo(obj);
  79. UnityEngine.Object.DestroyImmediate(obj);
  80. }
  81. }
  82. public static BaseObject InstanceIDToObject(int instanceID)
  83. {
  84. return EditorUtility.InstanceIDToObject(instanceID) as BaseObject;
  85. }
  86. internal virtual void OnEnable() {}
  87. internal virtual void OnDestroy() {}
  88. }
  89. #endif
  90. }