IKColliderSaveLoader.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using kt.Physics;
  6. using kt.Serialization;
  7. using UnityEngine;
  8. namespace kt.ik
  9. {
  10. public class IKColliderSaveLoader : MonoBehaviour
  11. {
  12. public string directoryPath
  13. {
  14. get
  15. {
  16. return this.DirectoryPath;
  17. }
  18. set
  19. {
  20. this.DirectoryPath = value;
  21. }
  22. }
  23. public string fileName
  24. {
  25. get
  26. {
  27. return this.FileName;
  28. }
  29. set
  30. {
  31. this.FileName = value;
  32. }
  33. }
  34. public PermTempAction onPostLoad
  35. {
  36. get
  37. {
  38. return this.OnPostLoad;
  39. }
  40. }
  41. private void UpdateColliderMetaArray()
  42. {
  43. this.ColliderMetaArray = (from e in base.GetComponentsInChildren<IKColliderMeta>()
  44. where base.gameObject != e.gameObject
  45. select e).ToArray<IKColliderMeta>();
  46. foreach (IKColliderMeta ikcolliderMeta in this.ColliderMetaArray)
  47. {
  48. this.IKEffectorColliderDic[ikcolliderMeta.effectorType] = ikcolliderMeta.colliderList;
  49. }
  50. }
  51. public void ColliderSave()
  52. {
  53. this.UpdateColliderMetaArray();
  54. KasaiUtility.FileSave(this.directoryPath + this.fileName, new Action<StreamWriter>(this.Save));
  55. }
  56. public void ColliderLoad()
  57. {
  58. KasaiUtility.FileLoadGameData(this.fileName, delegate(StreamReader reader)
  59. {
  60. this.UpdateColliderMetaArray();
  61. if (this.ColliderMetaArray != null)
  62. {
  63. for (int i = 0; i < this.ColliderMetaArray.Length; i++)
  64. {
  65. foreach (ANativeColliderBase anativeColliderBase in this.ColliderMetaArray[i].colliderList)
  66. {
  67. if (anativeColliderBase)
  68. {
  69. UnityEngine.Object.DestroyImmediate(anativeColliderBase.gameObject);
  70. }
  71. }
  72. }
  73. }
  74. this.IKEffectorColliderDic.Clear();
  75. this.Load(reader);
  76. this.onPostLoad.Invoke();
  77. }, null);
  78. }
  79. public void Save(StreamWriter writer)
  80. {
  81. IKColliderSaveLoader.IKColliderDataPackage ikcolliderDataPackage = new IKColliderSaveLoader.IKColliderDataPackage();
  82. foreach (KeyValuePair<FullBodyIKMgr.IKEffectorType, List<ANativeColliderBase>> keyValuePair in this.IKEffectorColliderDic)
  83. {
  84. IKColliderSaveLoader.IKColliderData ikcolliderData = new IKColliderSaveLoader.IKColliderData();
  85. ikcolliderData.effectorType = keyValuePair.Key;
  86. ikcolliderData.colliderStatusList = (from col in keyValuePair.Value
  87. select col.status).ToList<NativeColliderStatus>();
  88. ikcolliderDataPackage.dataList.Add(ikcolliderData);
  89. }
  90. string value = JsonUtility.ToJson(ikcolliderDataPackage, true);
  91. writer.Write(value);
  92. }
  93. public void Load(StreamReader reader)
  94. {
  95. string json = reader.ReadToEnd();
  96. IKColliderSaveLoader.IKColliderDataPackage ikcolliderDataPackage = JsonUtility.FromJson<IKColliderSaveLoader.IKColliderDataPackage>(json);
  97. foreach (IKColliderSaveLoader.IKColliderData ikcolliderData in ikcolliderDataPackage.dataList)
  98. {
  99. string name = string.Format("IK_{0}_Collider", ikcolliderData.effectorType);
  100. this.IKEffectorColliderDic[ikcolliderData.effectorType] = new List<ANativeColliderBase>();
  101. ANativeColliderBase anativeColliderBase = null;
  102. foreach (NativeColliderStatus nativeColliderStatus in ikcolliderData.colliderStatusList)
  103. {
  104. switch (nativeColliderStatus.colliderType)
  105. {
  106. case NativeColliderStatus.ColliderType.Plane:
  107. anativeColliderBase = new GameObject(name).AddComponent<NativePlaneCollider>();
  108. break;
  109. case NativeColliderStatus.ColliderType.Capsule:
  110. anativeColliderBase = new GameObject(name).AddComponent<NativeCapsuleCollider>();
  111. break;
  112. case NativeColliderStatus.ColliderType.Sphere:
  113. anativeColliderBase = new GameObject(name).AddComponent<NativeSphereCollider>();
  114. break;
  115. }
  116. if (anativeColliderBase != null)
  117. {
  118. anativeColliderBase.SetStatus(nativeColliderStatus, this.fullbodyIK.body);
  119. this.IKEffectorColliderDic[ikcolliderData.effectorType].Add(anativeColliderBase);
  120. }
  121. }
  122. }
  123. }
  124. public List<ANativeColliderBase> GetColliderList(FullBodyIKMgr.IKEffectorType effector_type)
  125. {
  126. if (this.IKEffectorColliderDic.ContainsKey(effector_type))
  127. {
  128. return this.IKEffectorColliderDic[effector_type];
  129. }
  130. return null;
  131. }
  132. private string DirectoryPath = Path.GetFullPath("./GameData/system_gp003/collider/");
  133. private string FileName = "ik_collider.ikcol";
  134. public FullBodyIKMgr fullbodyIK;
  135. private IKColliderMeta[] ColliderMetaArray;
  136. private Dictionary<FullBodyIKMgr.IKEffectorType, List<ANativeColliderBase>> IKEffectorColliderDic = new Dictionary<FullBodyIKMgr.IKEffectorType, List<ANativeColliderBase>>();
  137. private PermTempAction OnPostLoad = new PermTempAction();
  138. [Serializable]
  139. public class IKColliderData : ASerializationVersionControl
  140. {
  141. public IKColliderData()
  142. {
  143. }
  144. public IKColliderData(KeyValuePair<FullBodyIKMgr.IKEffectorType, List<NativeColliderStatus>> pair)
  145. {
  146. this.effectorType = pair.Key;
  147. this.colliderStatusList = pair.Value;
  148. }
  149. public override int FixVersion
  150. {
  151. get
  152. {
  153. return 1000;
  154. }
  155. }
  156. public override void OnBeforeSerialize()
  157. {
  158. base.OnBeforeSerialize();
  159. this.StatusJsonStrList = new List<string>();
  160. for (int i = 0; i < this.colliderStatusList.Count; i++)
  161. {
  162. NativeColliderStatus nativeColliderStatus = this.colliderStatusList[i];
  163. string text = string.Empty;
  164. switch (nativeColliderStatus.colliderType)
  165. {
  166. case NativeColliderStatus.ColliderType.Plane:
  167. text = JsonUtility.ToJson(nativeColliderStatus as NativePlaneColliderStatus);
  168. break;
  169. case NativeColliderStatus.ColliderType.Capsule:
  170. text = JsonUtility.ToJson(nativeColliderStatus as NativeCapsuleColliderStatus);
  171. break;
  172. case NativeColliderStatus.ColliderType.Sphere:
  173. text = JsonUtility.ToJson(nativeColliderStatus as NativeSphereColliderStatus);
  174. break;
  175. case NativeColliderStatus.ColliderType.MaidPropCol:
  176. text = JsonUtility.ToJson(nativeColliderStatus as NativeMaidPropColliderStatus);
  177. break;
  178. }
  179. if (!string.IsNullOrEmpty(text))
  180. {
  181. this.StatusJsonStrList.Add(text);
  182. }
  183. }
  184. }
  185. public override void OnAfterDeserialize()
  186. {
  187. base.OnAfterDeserialize();
  188. this.colliderStatusList.Clear();
  189. for (int i = 0; i < this.StatusJsonStrList.Count; i++)
  190. {
  191. string json = this.StatusJsonStrList[i];
  192. NativeColliderStatus nativeColliderStatus = JsonUtility.FromJson<NativeColliderStatus>(json);
  193. switch (nativeColliderStatus.colliderType)
  194. {
  195. case NativeColliderStatus.ColliderType.Plane:
  196. {
  197. NativePlaneColliderStatus item = JsonUtility.FromJson<NativePlaneColliderStatus>(json);
  198. this.colliderStatusList.Add(item);
  199. break;
  200. }
  201. case NativeColliderStatus.ColliderType.Capsule:
  202. {
  203. NativeCapsuleColliderStatus item2 = JsonUtility.FromJson<NativeCapsuleColliderStatus>(json);
  204. this.colliderStatusList.Add(item2);
  205. break;
  206. }
  207. case NativeColliderStatus.ColliderType.Sphere:
  208. {
  209. NativeSphereColliderStatus item3 = JsonUtility.FromJson<NativeSphereColliderStatus>(json);
  210. this.colliderStatusList.Add(item3);
  211. break;
  212. }
  213. case NativeColliderStatus.ColliderType.MaidPropCol:
  214. {
  215. NativeMaidPropColliderStatus item4 = JsonUtility.FromJson<NativeMaidPropColliderStatus>(json);
  216. this.colliderStatusList.Add(item4);
  217. break;
  218. }
  219. }
  220. }
  221. this.StatusJsonStrList.Clear();
  222. this.StatusJsonStrList = null;
  223. }
  224. public FullBodyIKMgr.IKEffectorType effectorType;
  225. [NonSerialized]
  226. public List<NativeColliderStatus> colliderStatusList = new List<NativeColliderStatus>();
  227. [SerializeField]
  228. private List<string> StatusJsonStrList;
  229. }
  230. [Serializable]
  231. public class IKColliderDataPackage : ASerializationVersionControl
  232. {
  233. public override int FixVersion
  234. {
  235. get
  236. {
  237. return 1000;
  238. }
  239. }
  240. public List<IKColliderSaveLoader.IKColliderData> dataList = new List<IKColliderSaveLoader.IKColliderData>();
  241. }
  242. }
  243. }