ReferenceCollector.cs 7.8 KB

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