using System; using System.Collections.Generic; using System.IO; using PrivateMaidMode; using UnityEngine; public class MaidPartsCollider : MonoBehaviour { public string FilePath { get { return this.filePath; } } public List GetColliderData() { return this.colliderArray; } public static MaidPartsCollider AddPartCollider(Maid maid) { if (maid == null) { NDebug.Assert("AddPartCollider:メイドがいません。", false); } if (maid.body0 == null || !maid.body0.isLoadedBody) { NDebug.Assert("AddPartCollider:メイドのBodyがありません。", false); } MaidPartsCollider maidPartsCollider = maid.gameObject.GetComponent(); if (maidPartsCollider == null) { maidPartsCollider = maid.gameObject.AddComponent(); } return maidPartsCollider; } public List AddCollider() { this.RemoveCollider(); this.Read(); return this.colliderArray; } public void RemoveCollider() { if (this.colliderArray != null) { for (int i = 0; i < this.colliderArray.Count; i++) { if (this.colliderArray[i] != null && this.colliderArray[i].gameObject != null) { UnityEngine.Object.Destroy(this.colliderArray[i].gameObject); } } this.colliderArray.Clear(); } } public void Read() { this.colliderArray = new List(); string path = Path.ChangeExtension(this.filePath, null); TextAsset textAsset = (TextAsset)Resources.Load(path); BinaryReader binaryReader = new BinaryReader(new MemoryStream(textAsset.bytes)); Maid component = base.GetComponent(); Transform trBones = component.body0.m_trBones; int num = binaryReader.ReadInt32(); for (int i = 0; i < num; i++) { string text = binaryReader.ReadString(); int count = text.IndexOf("Bip01"); text = text.Remove(0, count); Transform transform = trBones.Find(text); if (transform != null) { GameObject gameObject = new GameObject(this.addObjName + transform.name); gameObject.layer = this.layerNum; gameObject.transform.SetParent(transform, false); PartColliderData partColliderData = this.ReadColliderData(binaryReader, gameObject); if (partColliderData != null) { this.colliderArray.Add(partColliderData); } } } binaryReader.Close(); Resources.UnloadAsset(textAsset); } public void Write() { string path = Path.Combine(Application.dataPath, "Resources/" + this.filePath); Dictionary dictionary = this.FindPartColliderData(); Dictionary dictionary2 = new Dictionary(); foreach (KeyValuePair keyValuePair in dictionary) { string key = keyValuePair.Key; int num = keyValuePair.Key.IndexOf(this.addObjName); if (num > 0) { key = keyValuePair.Key.Remove(num - 1); } dictionary2.Add(key, keyValuePair.Value); } MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(dictionary2.Count); foreach (KeyValuePair keyValuePair2 in dictionary2) { binaryWriter.Write(keyValuePair2.Key); this.WriteColliderData(binaryWriter, keyValuePair2.Value); } File.WriteAllBytes(path, memoryStream.ToArray()); } private PartColliderData ReadColliderData(BinaryReader reader, GameObject addColliderObject) { PartColliderData partColliderData = null; int value = reader.ReadInt32(); Vector3 zero = Vector3.zero; zero.x = reader.ReadSingle(); zero.y = reader.ReadSingle(); zero.z = reader.ReadSingle(); int direction = reader.ReadInt32(); float height = reader.ReadSingle(); float radius = reader.ReadSingle(); if (addColliderObject != null && addColliderObject.GetComponent() == null) { partColliderData = addColliderObject.AddComponent(); CapsuleCollider capsuleCollider = partColliderData.capsuleCollider; capsuleCollider.center = zero; capsuleCollider.direction = direction; capsuleCollider.height = height; capsuleCollider.radius = radius; capsuleCollider.isTrigger = true; capsuleCollider.enabled = false; capsuleCollider.enabled = true; partColliderData.point = (TouchDataBase.TouchPoint)Enum.ToObject(typeof(TouchDataBase.TouchPoint), value); } return partColliderData; } private void WriteColliderData(BinaryWriter writer, PartColliderData colliderData) { writer.Write((int)colliderData.point); CapsuleCollider capsuleCollider = colliderData.capsuleCollider; writer.Write(capsuleCollider.center.x); writer.Write(capsuleCollider.center.y); writer.Write(capsuleCollider.center.z); writer.Write(capsuleCollider.direction); writer.Write(capsuleCollider.height); writer.Write(capsuleCollider.radius); } private Dictionary FindPartColliderData() { Action> FindFunc = null; FindFunc = delegate(Transform target, string path, Dictionary pathDic) { path += target.name; PartColliderData component = target.GetComponent(); if (component != null) { pathDic.Add(path, component); } for (int j = 0; j < target.childCount; j++) { FindFunc(target.GetChild(j), path + "/", pathDic); } }; Dictionary dictionary = new Dictionary(); for (int i = 0; i < base.transform.childCount; i++) { FindFunc(base.transform.GetChild(i), string.Empty, dictionary); } return dictionary; } private string addObjName = "OvrPartsHit_"; private string filePath = "System/maid_part_collider.bytes"; private int layerNum = 17; private List colliderArray = new List(); }