ReferenceCollector.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //Object并非C#基础中的Object,而是 UnityEngine.Object
  5. using Object = UnityEngine.Object;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9. //使其能在Inspector面板显示,并且可以被赋予相应值
  10. [Serializable]
  11. public class ReferenceCollectorData
  12. {
  13. public string key;
  14. //Object并非C#基础中的Object,而是 UnityEngine.Object
  15. public Object gameObject;
  16. }
  17. //继承IComparer对比器,Ordinal会使用序号排序规则比较字符串,因为是byte级别的比较,所以准确性和性能都不错
  18. public class ReferenceCollectorDataComparer: IComparer<ReferenceCollectorData>
  19. {
  20. public int Compare(ReferenceCollectorData x, ReferenceCollectorData y)
  21. {
  22. return string.Compare(x.key, y.key, StringComparison.Ordinal);
  23. }
  24. }
  25. //继承ISerializationCallbackReceiver后会增加OnAfterDeserialize和OnBeforeSerialize两个回调函数,如果有需要可以在对需要序列化的东西进行操作
  26. //ET在这里主要是在OnAfterDeserialize回调函数中将data中存储的ReferenceCollectorData转换为dict中的Object,方便之后的使用
  27. //注意UNITY_EDITOR宏定义,在编译以后,部分编辑器相关函数并不存在
  28. public class ReferenceCollector: MonoBehaviour, ISerializationCallbackReceiver
  29. {
  30. //用于序列化的List
  31. public List<ReferenceCollectorData> data = new List<ReferenceCollectorData>();
  32. //Object并非C#基础中的Object,而是 UnityEngine.Object
  33. private readonly Dictionary<string, Object> dict = new Dictionary<string, Object>();
  34. #if UNITY_EDITOR
  35. //添加新的元素
  36. public void Add(string key, Object obj)
  37. {
  38. SerializedObject serializedObject = new SerializedObject(this);
  39. //根据PropertyPath读取数据
  40. //如果不知道具体的格式,可以右键用文本编辑器打开一个prefab文件(如Bundles/UI目录中的几个)
  41. //因为这几个prefab挂载了ReferenceCollector,所以搜索data就能找到存储的数据
  42. SerializedProperty dataProperty = serializedObject.FindProperty("data");
  43. int i;
  44. //遍历data,看添加的数据是否存在相同key
  45. for (i = 0; i < data.Count; i++)
  46. {
  47. if (data[i].key == key)
  48. {
  49. break;
  50. }
  51. }
  52. //不等于data.Count意为已经存在于data List中,直接赋值即可
  53. if (i != data.Count)
  54. {
  55. //根据i的值获取dataProperty,也就是data中的对应ReferenceCollectorData,不过在这里,是对Property进行的读取,有点类似json或者xml的节点
  56. SerializedProperty element = dataProperty.GetArrayElementAtIndex(i);
  57. //对对应节点进行赋值,值为gameobject相对应的fileID
  58. //fileID独一无二,单对单关系,其他挂载在这个gameobject上的script或组件会保存相对应的fileID
  59. element.FindPropertyRelative("gameObject").objectReferenceValue = obj;
  60. }
  61. else
  62. {
  63. //等于则说明key在data中无对应元素,所以得向其插入新的元素
  64. dataProperty.InsertArrayElementAtIndex(i);
  65. SerializedProperty element = dataProperty.GetArrayElementAtIndex(i);
  66. element.FindPropertyRelative("key").stringValue = key;
  67. element.FindPropertyRelative("gameObject").objectReferenceValue = obj;
  68. }
  69. //应用与更新
  70. EditorUtility.SetDirty(this);
  71. serializedObject.ApplyModifiedProperties();
  72. serializedObject.UpdateIfRequiredOrScript();
  73. }
  74. //删除元素,知识点与上面的添加相似
  75. public void Remove(string key)
  76. {
  77. SerializedObject serializedObject = new SerializedObject(this);
  78. SerializedProperty dataProperty = serializedObject.FindProperty("data");
  79. int i;
  80. for (i = 0; i < data.Count; i++)
  81. {
  82. if (data[i].key == key)
  83. {
  84. break;
  85. }
  86. }
  87. if (i != data.Count)
  88. {
  89. dataProperty.DeleteArrayElementAtIndex(i);
  90. }
  91. EditorUtility.SetDirty(this);
  92. serializedObject.ApplyModifiedProperties();
  93. serializedObject.UpdateIfRequiredOrScript();
  94. }
  95. public void Clear()
  96. {
  97. SerializedObject serializedObject = new SerializedObject(this);
  98. //根据PropertyPath读取prefab文件中的数据
  99. //如果不知道具体的格式,可以直接右键用文本编辑器打开,搜索data就能找到
  100. var dataProperty = serializedObject.FindProperty("data");
  101. dataProperty.ClearArray();
  102. EditorUtility.SetDirty(this);
  103. serializedObject.ApplyModifiedProperties();
  104. serializedObject.UpdateIfRequiredOrScript();
  105. }
  106. public void Sort()
  107. {
  108. SerializedObject serializedObject = new SerializedObject(this);
  109. data.Sort(new ReferenceCollectorDataComparer());
  110. EditorUtility.SetDirty(this);
  111. serializedObject.ApplyModifiedProperties();
  112. serializedObject.UpdateIfRequiredOrScript();
  113. }
  114. #endif
  115. //使用泛型返回对应key的gameobject
  116. public T Get<T>(string key) where T : class
  117. {
  118. Object dictGo;
  119. if (!dict.TryGetValue(key, out dictGo))
  120. {
  121. return null;
  122. }
  123. return dictGo as T;
  124. }
  125. //使用泛型返回对应key的gameobject
  126. public T GetComponent<T>(string key) where T : Component
  127. {
  128. Object dictGo;
  129. if (!dict.TryGetValue(key, out dictGo))
  130. {
  131. return null;
  132. }
  133. GameObject go = (GameObject)dictGo;
  134. return go.GetComponent<T>();
  135. }
  136. public Object GetObject(string key)
  137. {
  138. Object dictGo;
  139. if (!dict.TryGetValue(key, out dictGo))
  140. {
  141. return null;
  142. }
  143. return dictGo;
  144. }
  145. public void OnBeforeSerialize()
  146. {
  147. }
  148. //在反序列化后运行
  149. public void OnAfterDeserialize()
  150. {
  151. dict.Clear();
  152. foreach (ReferenceCollectorData referenceCollectorData in data)
  153. {
  154. if (!dict.ContainsKey(referenceCollectorData.key))
  155. {
  156. dict.Add(referenceCollectorData.key, referenceCollectorData.gameObject);
  157. }
  158. }
  159. }
  160. }