MaidPartsCollider.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using PrivateMaidMode;
  5. using UnityEngine;
  6. public class MaidPartsCollider : MonoBehaviour
  7. {
  8. public string FilePath
  9. {
  10. get
  11. {
  12. return this.filePath;
  13. }
  14. }
  15. public List<PartColliderData> GetColliderData()
  16. {
  17. return this.colliderArray;
  18. }
  19. public static MaidPartsCollider AddPartCollider(Maid maid)
  20. {
  21. if (maid == null)
  22. {
  23. NDebug.Assert("AddPartCollider:メイドがいません。", false);
  24. }
  25. if (maid.body0 == null || !maid.body0.isLoadedBody)
  26. {
  27. NDebug.Assert("AddPartCollider:メイドのBodyがありません。", false);
  28. }
  29. MaidPartsCollider maidPartsCollider = maid.gameObject.GetComponent<MaidPartsCollider>();
  30. if (maidPartsCollider == null)
  31. {
  32. maidPartsCollider = maid.gameObject.AddComponent<MaidPartsCollider>();
  33. }
  34. return maidPartsCollider;
  35. }
  36. public List<PartColliderData> AddCollider()
  37. {
  38. this.RemoveCollider();
  39. this.Read();
  40. return this.colliderArray;
  41. }
  42. public void RemoveCollider()
  43. {
  44. if (this.colliderArray != null)
  45. {
  46. for (int i = 0; i < this.colliderArray.Count; i++)
  47. {
  48. if (this.colliderArray[i] != null && this.colliderArray[i].gameObject != null)
  49. {
  50. UnityEngine.Object.Destroy(this.colliderArray[i].gameObject);
  51. }
  52. }
  53. this.colliderArray.Clear();
  54. }
  55. }
  56. public void Read()
  57. {
  58. this.colliderArray = new List<PartColliderData>();
  59. string path = Path.ChangeExtension(this.filePath, null);
  60. TextAsset textAsset = (TextAsset)Resources.Load(path);
  61. BinaryReader binaryReader = new BinaryReader(new MemoryStream(textAsset.bytes));
  62. Maid component = base.GetComponent<Maid>();
  63. Transform trBones = component.body0.m_trBones;
  64. int num = binaryReader.ReadInt32();
  65. for (int i = 0; i < num; i++)
  66. {
  67. string text = binaryReader.ReadString();
  68. int count = text.IndexOf("Bip01");
  69. text = text.Remove(0, count);
  70. Transform transform = trBones.Find(text);
  71. if (transform != null)
  72. {
  73. GameObject gameObject = new GameObject(this.addObjName + transform.name);
  74. gameObject.layer = this.layerNum;
  75. gameObject.transform.SetParent(transform, false);
  76. PartColliderData partColliderData = this.ReadColliderData(binaryReader, gameObject);
  77. if (partColliderData != null)
  78. {
  79. this.colliderArray.Add(partColliderData);
  80. }
  81. }
  82. }
  83. binaryReader.Close();
  84. Resources.UnloadAsset(textAsset);
  85. }
  86. public void Write()
  87. {
  88. string path = Path.Combine(Application.dataPath, "Resources/" + this.filePath);
  89. Dictionary<string, PartColliderData> dictionary = this.FindPartColliderData();
  90. Dictionary<string, PartColliderData> dictionary2 = new Dictionary<string, PartColliderData>();
  91. foreach (KeyValuePair<string, PartColliderData> keyValuePair in dictionary)
  92. {
  93. string key = keyValuePair.Key;
  94. int num = keyValuePair.Key.IndexOf(this.addObjName);
  95. if (num > 0)
  96. {
  97. key = keyValuePair.Key.Remove(num - 1);
  98. }
  99. dictionary2.Add(key, keyValuePair.Value);
  100. }
  101. MemoryStream memoryStream = new MemoryStream();
  102. BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
  103. binaryWriter.Write(dictionary2.Count);
  104. foreach (KeyValuePair<string, PartColliderData> keyValuePair2 in dictionary2)
  105. {
  106. binaryWriter.Write(keyValuePair2.Key);
  107. this.WriteColliderData(binaryWriter, keyValuePair2.Value);
  108. }
  109. File.WriteAllBytes(path, memoryStream.ToArray());
  110. }
  111. private PartColliderData ReadColliderData(BinaryReader reader, GameObject addColliderObject)
  112. {
  113. PartColliderData partColliderData = null;
  114. int value = reader.ReadInt32();
  115. Vector3 zero = Vector3.zero;
  116. zero.x = reader.ReadSingle();
  117. zero.y = reader.ReadSingle();
  118. zero.z = reader.ReadSingle();
  119. int direction = reader.ReadInt32();
  120. float height = reader.ReadSingle();
  121. float radius = reader.ReadSingle();
  122. if (addColliderObject != null && addColliderObject.GetComponent<PartColliderData>() == null)
  123. {
  124. partColliderData = addColliderObject.AddComponent<PartColliderData>();
  125. CapsuleCollider capsuleCollider = partColliderData.capsuleCollider;
  126. capsuleCollider.center = zero;
  127. capsuleCollider.direction = direction;
  128. capsuleCollider.height = height;
  129. capsuleCollider.radius = radius;
  130. capsuleCollider.isTrigger = true;
  131. capsuleCollider.enabled = false;
  132. capsuleCollider.enabled = true;
  133. partColliderData.point = (TouchDataBase.TouchPoint)Enum.ToObject(typeof(TouchDataBase.TouchPoint), value);
  134. }
  135. return partColliderData;
  136. }
  137. private void WriteColliderData(BinaryWriter writer, PartColliderData colliderData)
  138. {
  139. writer.Write((int)colliderData.point);
  140. CapsuleCollider capsuleCollider = colliderData.capsuleCollider;
  141. writer.Write(capsuleCollider.center.x);
  142. writer.Write(capsuleCollider.center.y);
  143. writer.Write(capsuleCollider.center.z);
  144. writer.Write(capsuleCollider.direction);
  145. writer.Write(capsuleCollider.height);
  146. writer.Write(capsuleCollider.radius);
  147. }
  148. private Dictionary<string, PartColliderData> FindPartColliderData()
  149. {
  150. Action<Transform, string, Dictionary<string, PartColliderData>> FindFunc = null;
  151. FindFunc = delegate(Transform target, string path, Dictionary<string, PartColliderData> pathDic)
  152. {
  153. path += target.name;
  154. PartColliderData component = target.GetComponent<PartColliderData>();
  155. if (component != null)
  156. {
  157. pathDic.Add(path, component);
  158. }
  159. for (int j = 0; j < target.childCount; j++)
  160. {
  161. FindFunc(target.GetChild(j), path + "/", pathDic);
  162. }
  163. };
  164. Dictionary<string, PartColliderData> dictionary = new Dictionary<string, PartColliderData>();
  165. for (int i = 0; i < base.transform.childCount; i++)
  166. {
  167. FindFunc(base.transform.GetChild(i), string.Empty, dictionary);
  168. }
  169. return dictionary;
  170. }
  171. private string addObjName = "OvrPartsHit_";
  172. private string filePath = "System/maid_part_collider.bytes";
  173. private int layerNum = 17;
  174. private List<PartColliderData> colliderArray = new List<PartColliderData>();
  175. }