MaidColliderCollect.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. public class MaidColliderCollect : MonoBehaviour
  6. {
  7. public string[] file_path_array
  8. {
  9. get
  10. {
  11. return this.file_path_array_;
  12. }
  13. }
  14. public static MaidColliderCollect AddColliderCollect(Maid maid)
  15. {
  16. if (maid == null)
  17. {
  18. NDebug.Assert("AddColliderCollect:メイドが居ません。", false);
  19. }
  20. if (maid.body0 == null || !maid.body0.isLoadedBody)
  21. {
  22. NDebug.Assert("AddColliderCollect:メイドのBodyが未だありません。", false);
  23. }
  24. MaidColliderCollect maidColliderCollect = maid.gameObject.GetComponent<MaidColliderCollect>();
  25. if (maidColliderCollect == null)
  26. {
  27. maidColliderCollect = maid.gameObject.AddComponent<MaidColliderCollect>();
  28. }
  29. return maidColliderCollect;
  30. }
  31. public static MaidColliderCollect AddCollider(Maid maid, MaidColliderCollect.ColliderType collider_type)
  32. {
  33. if (maid == null)
  34. {
  35. NDebug.Assert("AddColliderCollect:メイドが居ません。", false);
  36. }
  37. if (maid.body0 == null || !maid.body0.isLoadedBody)
  38. {
  39. NDebug.Assert("AddColliderCollect:メイドのBodyが未だありません。", false);
  40. }
  41. MaidColliderCollect maidColliderCollect = maid.gameObject.GetComponent<MaidColliderCollect>();
  42. if (maidColliderCollect == null)
  43. {
  44. maidColliderCollect = maid.gameObject.AddComponent<MaidColliderCollect>();
  45. }
  46. maidColliderCollect.AddCollider(collider_type);
  47. return maidColliderCollect;
  48. }
  49. public static void RemoveColliderAll(Maid maid)
  50. {
  51. if (maid == null)
  52. {
  53. return;
  54. }
  55. MaidColliderCollect component = maid.gameObject.GetComponent<MaidColliderCollect>();
  56. if (component != null)
  57. {
  58. for (int i = 0; i < 2; i++)
  59. {
  60. component.RemoveCollider((MaidColliderCollect.ColliderType)i);
  61. }
  62. }
  63. }
  64. public static void SuspendColliderAll(Maid maid, bool enable)
  65. {
  66. if (maid == null)
  67. {
  68. return;
  69. }
  70. MaidColliderCollect component = maid.gameObject.GetComponent<MaidColliderCollect>();
  71. if (component != null)
  72. {
  73. for (int i = 0; i < 2; i++)
  74. {
  75. component.SuspendCollider((MaidColliderCollect.ColliderType)i, enable);
  76. }
  77. }
  78. }
  79. public List<CapsuleCollider> AddCollider(MaidColliderCollect.ColliderType type)
  80. {
  81. this.RemoveCollider(type);
  82. List<CapsuleCollider> list = this.Read(type);
  83. this.collider_array_[(int)type] = list;
  84. return new List<CapsuleCollider>(list);
  85. }
  86. public void RemoveCollider(MaidColliderCollect.ColliderType type)
  87. {
  88. List<CapsuleCollider> list = this.collider_array_[(int)type];
  89. if (list != null)
  90. {
  91. for (int i = 0; i < list.Count; i++)
  92. {
  93. if (list[i] != null && list[i].gameObject != null)
  94. {
  95. UnityEngine.Object.Destroy(list[i].gameObject);
  96. }
  97. }
  98. list.Clear();
  99. }
  100. }
  101. public void SuspendCollider(MaidColliderCollect.ColliderType type, bool enable)
  102. {
  103. List<CapsuleCollider> list = this.collider_array_[(int)type];
  104. if (list != null)
  105. {
  106. for (int i = 0; i < list.Count; i++)
  107. {
  108. if (list[i] != null && list[i].gameObject != null)
  109. {
  110. list[i].enabled = enable;
  111. }
  112. }
  113. }
  114. }
  115. public List<CapsuleCollider> GetCollider(MaidColliderCollect.ColliderType type)
  116. {
  117. return this.collider_array_[(int)type];
  118. }
  119. public void SwitchCollider(MaidColliderCollect.ColliderType type)
  120. {
  121. for (int i = 0; i < 2; i++)
  122. {
  123. this.ActiveCollider(type, false);
  124. if (type != MaidColliderCollect.ColliderType.NON && i == (int)type)
  125. {
  126. this.ActiveCollider(type, true);
  127. }
  128. }
  129. }
  130. public void ActiveCollider(MaidColliderCollect.ColliderType type, bool active)
  131. {
  132. if (this.collider_array_[(int)type] != null)
  133. {
  134. foreach (CapsuleCollider capsuleCollider in this.collider_array_[(int)type])
  135. {
  136. capsuleCollider.gameObject.SetActive(active);
  137. }
  138. }
  139. }
  140. public List<CapsuleCollider> Read(MaidColliderCollect.ColliderType collider_type)
  141. {
  142. string path = this.file_path_array_[(int)collider_type];
  143. string str = this.add_obj_name_array_[(int)collider_type];
  144. int layer = this.layer_array_[(int)collider_type];
  145. List<CapsuleCollider> list = new List<CapsuleCollider>();
  146. path = Path.ChangeExtension(path, null);
  147. TextAsset textAsset = Resources.Load(path) as TextAsset;
  148. BinaryReader binaryReader = new BinaryReader(new MemoryStream(textAsset.bytes));
  149. Maid component = base.GetComponent<Maid>();
  150. Transform trBones = component.body0.m_trBones;
  151. int num = binaryReader.ReadInt32();
  152. for (int i = 0; i < num; i++)
  153. {
  154. string text = binaryReader.ReadString();
  155. int count = text.IndexOf("Bip01");
  156. text = text.Remove(0, count);
  157. Transform transform = trBones.Find(text);
  158. if (transform != null)
  159. {
  160. GameObject gameObject = new GameObject(str + transform.name);
  161. gameObject.layer = layer;
  162. gameObject.transform.SetParent(transform, false);
  163. CapsuleCollider capsuleCollider = this.ReadCapsuleCollider(binaryReader, gameObject);
  164. if (capsuleCollider != null)
  165. {
  166. list.Add(capsuleCollider);
  167. }
  168. }
  169. }
  170. binaryReader.Close();
  171. Resources.UnloadAsset(textAsset);
  172. return list;
  173. }
  174. public void Write(MaidColliderCollect.ColliderType collider_type)
  175. {
  176. string path = Path.Combine(Application.dataPath, "Resources/" + this.file_path_array_[(int)collider_type]);
  177. Dictionary<string, CapsuleCollider> dictionary = this.FindCapsuleCollider();
  178. Dictionary<string, CapsuleCollider> dictionary2 = new Dictionary<string, CapsuleCollider>();
  179. foreach (KeyValuePair<string, CapsuleCollider> keyValuePair in dictionary)
  180. {
  181. string key = keyValuePair.Key;
  182. for (int i = 0; i < 2; i++)
  183. {
  184. int num = keyValuePair.Key.IndexOf(this.add_obj_name_array_[i]);
  185. if (0 < num)
  186. {
  187. key = keyValuePair.Key.Remove(num - 1);
  188. break;
  189. }
  190. }
  191. dictionary2.Add(key, keyValuePair.Value);
  192. }
  193. MemoryStream memoryStream = new MemoryStream();
  194. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  195. binaryWriter.Write(dictionary.Count);
  196. foreach (KeyValuePair<string, CapsuleCollider> keyValuePair2 in dictionary)
  197. {
  198. binaryWriter.Write(keyValuePair2.Key);
  199. this.WriterCapsuleCollider(binaryWriter, keyValuePair2.Value);
  200. }
  201. File.WriteAllBytes(path, memoryStream.ToArray());
  202. }
  203. private CapsuleCollider ReadCapsuleCollider(BinaryReader reader, GameObject add_collider_object)
  204. {
  205. CapsuleCollider capsuleCollider = null;
  206. Vector3 zero = Vector3.zero;
  207. zero.x = reader.ReadSingle();
  208. zero.y = reader.ReadSingle();
  209. zero.z = reader.ReadSingle();
  210. int direction = reader.ReadInt32();
  211. float height = reader.ReadSingle();
  212. float radius = reader.ReadSingle();
  213. if (add_collider_object != null && add_collider_object.GetComponent<CapsuleCollider>() == null)
  214. {
  215. capsuleCollider = add_collider_object.AddComponent<CapsuleCollider>();
  216. capsuleCollider.center = zero;
  217. capsuleCollider.direction = direction;
  218. capsuleCollider.height = height;
  219. capsuleCollider.radius = radius;
  220. capsuleCollider.isTrigger = true;
  221. capsuleCollider.enabled = false;
  222. capsuleCollider.enabled = true;
  223. }
  224. return capsuleCollider;
  225. }
  226. private void WriterCapsuleCollider(BinaryWriter writer, CapsuleCollider collider)
  227. {
  228. writer.Write(collider.center.x);
  229. writer.Write(collider.center.y);
  230. writer.Write(collider.center.z);
  231. writer.Write(collider.direction);
  232. writer.Write(collider.height);
  233. writer.Write(collider.radius);
  234. }
  235. private Dictionary<string, CapsuleCollider> FindCapsuleCollider()
  236. {
  237. Action<Transform, string, Dictionary<string, CapsuleCollider>> FindFunc = null;
  238. FindFunc = delegate(Transform target, string path, Dictionary<string, CapsuleCollider> path_dic)
  239. {
  240. path += target.name;
  241. CapsuleCollider component = target.GetComponent<CapsuleCollider>();
  242. if (component != null)
  243. {
  244. path_dic.Add(path, component);
  245. }
  246. for (int j = 0; j < target.childCount; j++)
  247. {
  248. FindFunc(target.GetChild(j), path + "/", path_dic);
  249. }
  250. };
  251. Dictionary<string, CapsuleCollider> dictionary = new Dictionary<string, CapsuleCollider>();
  252. for (int i = 0; i < base.transform.childCount; i++)
  253. {
  254. FindFunc(base.transform.GetChild(i), string.Empty, dictionary);
  255. }
  256. return dictionary;
  257. }
  258. private string[] add_obj_name_array_ = new string[]
  259. {
  260. "OvrGrabHit_",
  261. "OvrTouchHit_"
  262. };
  263. private string[] file_path_array_ = new string[]
  264. {
  265. "System/maid_collider.bytes",
  266. "System/maid_collider_touch.bytes"
  267. };
  268. private int[] layer_array_ = new int[]
  269. {
  270. 17,
  271. 19
  272. };
  273. private List<CapsuleCollider>[] collider_array_ = new List<CapsuleCollider>[2];
  274. public enum ColliderType
  275. {
  276. NON = -1,
  277. Grab,
  278. Touch,
  279. MAX
  280. }
  281. }