ReferenceCollector.cs 8.5 KB

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