123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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<PartColliderData> 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<MaidPartsCollider>();
- if (maidPartsCollider == null)
- {
- maidPartsCollider = maid.gameObject.AddComponent<MaidPartsCollider>();
- }
- return maidPartsCollider;
- }
- public List<PartColliderData> 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<PartColliderData>();
- 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<Maid>();
- 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<string, PartColliderData> dictionary = this.FindPartColliderData();
- Dictionary<string, PartColliderData> dictionary2 = new Dictionary<string, PartColliderData>();
- foreach (KeyValuePair<string, PartColliderData> 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<string, PartColliderData> 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<PartColliderData>() == null)
- {
- partColliderData = addColliderObject.AddComponent<PartColliderData>();
- 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<string, PartColliderData> FindPartColliderData()
- {
- Action<Transform, string, Dictionary<string, PartColliderData>> FindFunc = null;
- FindFunc = delegate(Transform target, string path, Dictionary<string, PartColliderData> pathDic)
- {
- path += target.name;
- PartColliderData component = target.GetComponent<PartColliderData>();
- if (component != null)
- {
- pathDic.Add(path, component);
- }
- for (int j = 0; j < target.childCount; j++)
- {
- FindFunc(target.GetChild(j), path + "/", pathDic);
- }
- };
- Dictionary<string, PartColliderData> dictionary = new Dictionary<string, PartColliderData>();
- 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<PartColliderData> colliderArray = new List<PartColliderData>();
- }
|