123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class MaidColliderCollect : MonoBehaviour
- {
- public string[] file_path_array
- {
- get
- {
- return this.file_path_array_;
- }
- }
- public static MaidColliderCollect AddColliderCollect(Maid maid)
- {
- if (maid == null)
- {
- NDebug.Assert("AddColliderCollect:メイドが居ません。", false);
- }
- if (maid.body0 == null || !maid.body0.isLoadedBody)
- {
- NDebug.Assert("AddColliderCollect:メイドのBodyが未だありません。", false);
- }
- MaidColliderCollect maidColliderCollect = maid.gameObject.GetComponent<MaidColliderCollect>();
- if (maidColliderCollect == null)
- {
- maidColliderCollect = maid.gameObject.AddComponent<MaidColliderCollect>();
- }
- return maidColliderCollect;
- }
- public static MaidColliderCollect AddCollider(Maid maid, MaidColliderCollect.ColliderType collider_type)
- {
- if (maid == null)
- {
- NDebug.Assert("AddColliderCollect:メイドが居ません。", false);
- }
- if (maid.body0 == null || !maid.body0.isLoadedBody)
- {
- NDebug.Assert("AddColliderCollect:メイドのBodyが未だありません。", false);
- }
- MaidColliderCollect maidColliderCollect = maid.gameObject.GetComponent<MaidColliderCollect>();
- if (maidColliderCollect == null)
- {
- maidColliderCollect = maid.gameObject.AddComponent<MaidColliderCollect>();
- }
- maidColliderCollect.AddCollider(collider_type);
- return maidColliderCollect;
- }
- public static void RemoveColliderAll(Maid maid)
- {
- if (maid == null)
- {
- return;
- }
- MaidColliderCollect component = maid.gameObject.GetComponent<MaidColliderCollect>();
- if (component != null)
- {
- for (int i = 0; i < 2; i++)
- {
- component.RemoveCollider((MaidColliderCollect.ColliderType)i);
- }
- }
- }
- public static void SuspendColliderAll(Maid maid, bool enable)
- {
- if (maid == null)
- {
- return;
- }
- MaidColliderCollect component = maid.gameObject.GetComponent<MaidColliderCollect>();
- if (component != null)
- {
- for (int i = 0; i < 2; i++)
- {
- component.SuspendCollider((MaidColliderCollect.ColliderType)i, enable);
- }
- }
- }
- public List<CapsuleCollider> AddCollider(MaidColliderCollect.ColliderType type)
- {
- this.RemoveCollider(type);
- List<CapsuleCollider> list = this.Read(type);
- this.collider_array_[(int)type] = list;
- return new List<CapsuleCollider>(list);
- }
- public void RemoveCollider(MaidColliderCollect.ColliderType type)
- {
- List<CapsuleCollider> list = this.collider_array_[(int)type];
- if (list != null)
- {
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i] != null && list[i].gameObject != null)
- {
- UnityEngine.Object.Destroy(list[i].gameObject);
- }
- }
- list.Clear();
- }
- }
- public void SuspendCollider(MaidColliderCollect.ColliderType type, bool enable)
- {
- List<CapsuleCollider> list = this.collider_array_[(int)type];
- if (list != null)
- {
- for (int i = 0; i < list.Count; i++)
- {
- if (list[i] != null && list[i].gameObject != null)
- {
- list[i].enabled = enable;
- }
- }
- }
- }
- public List<CapsuleCollider> GetCollider(MaidColliderCollect.ColliderType type)
- {
- return this.collider_array_[(int)type];
- }
- public void SwitchCollider(MaidColliderCollect.ColliderType type)
- {
- for (int i = 0; i < 2; i++)
- {
- this.ActiveCollider(type, false);
- if (type != MaidColliderCollect.ColliderType.NON && i == (int)type)
- {
- this.ActiveCollider(type, true);
- }
- }
- }
- public void ActiveCollider(MaidColliderCollect.ColliderType type, bool active)
- {
- if (this.collider_array_[(int)type] != null)
- {
- foreach (CapsuleCollider capsuleCollider in this.collider_array_[(int)type])
- {
- capsuleCollider.gameObject.SetActive(active);
- }
- }
- }
- public List<CapsuleCollider> Read(MaidColliderCollect.ColliderType collider_type)
- {
- string path = this.file_path_array_[(int)collider_type];
- string str = this.add_obj_name_array_[(int)collider_type];
- int layer = this.layer_array_[(int)collider_type];
- List<CapsuleCollider> list = new List<CapsuleCollider>();
- path = Path.ChangeExtension(path, null);
- TextAsset textAsset = Resources.Load(path) as TextAsset;
- 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(str + transform.name);
- gameObject.layer = layer;
- gameObject.transform.SetParent(transform, false);
- CapsuleCollider capsuleCollider = this.ReadCapsuleCollider(binaryReader, gameObject);
- if (capsuleCollider != null)
- {
- list.Add(capsuleCollider);
- }
- }
- }
- binaryReader.Close();
- Resources.UnloadAsset(textAsset);
- return list;
- }
- public void Write(MaidColliderCollect.ColliderType collider_type)
- {
- string path = Path.Combine(Application.dataPath, "Resources/" + this.file_path_array_[(int)collider_type]);
- Dictionary<string, CapsuleCollider> dictionary = this.FindCapsuleCollider();
- Dictionary<string, CapsuleCollider> dictionary2 = new Dictionary<string, CapsuleCollider>();
- foreach (KeyValuePair<string, CapsuleCollider> keyValuePair in dictionary)
- {
- string key = keyValuePair.Key;
- for (int i = 0; i < 2; i++)
- {
- int num = keyValuePair.Key.IndexOf(this.add_obj_name_array_[i]);
- if (0 < num)
- {
- key = keyValuePair.Key.Remove(num - 1);
- break;
- }
- }
- dictionary2.Add(key, keyValuePair.Value);
- }
- MemoryStream memoryStream = new MemoryStream();
- BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
- binaryWriter.Write(dictionary.Count);
- foreach (KeyValuePair<string, CapsuleCollider> keyValuePair2 in dictionary)
- {
- binaryWriter.Write(keyValuePair2.Key);
- this.WriterCapsuleCollider(binaryWriter, keyValuePair2.Value);
- }
- File.WriteAllBytes(path, memoryStream.ToArray());
- }
- private CapsuleCollider ReadCapsuleCollider(BinaryReader reader, GameObject add_collider_object)
- {
- CapsuleCollider capsuleCollider = null;
- 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 (add_collider_object != null && add_collider_object.GetComponent<CapsuleCollider>() == null)
- {
- capsuleCollider = add_collider_object.AddComponent<CapsuleCollider>();
- capsuleCollider.center = zero;
- capsuleCollider.direction = direction;
- capsuleCollider.height = height;
- capsuleCollider.radius = radius;
- capsuleCollider.isTrigger = true;
- capsuleCollider.enabled = false;
- capsuleCollider.enabled = true;
- }
- return capsuleCollider;
- }
- private void WriterCapsuleCollider(BinaryWriter writer, CapsuleCollider collider)
- {
- writer.Write(collider.center.x);
- writer.Write(collider.center.y);
- writer.Write(collider.center.z);
- writer.Write(collider.direction);
- writer.Write(collider.height);
- writer.Write(collider.radius);
- }
- private Dictionary<string, CapsuleCollider> FindCapsuleCollider()
- {
- Action<Transform, string, Dictionary<string, CapsuleCollider>> FindFunc = null;
- FindFunc = delegate(Transform target, string path, Dictionary<string, CapsuleCollider> path_dic)
- {
- path += target.name;
- CapsuleCollider component = target.GetComponent<CapsuleCollider>();
- if (component != null)
- {
- path_dic.Add(path, component);
- }
- for (int j = 0; j < target.childCount; j++)
- {
- FindFunc(target.GetChild(j), path + "/", path_dic);
- }
- };
- Dictionary<string, CapsuleCollider> dictionary = new Dictionary<string, CapsuleCollider>();
- for (int i = 0; i < base.transform.childCount; i++)
- {
- FindFunc(base.transform.GetChild(i), string.Empty, dictionary);
- }
- return dictionary;
- }
- private string[] add_obj_name_array_ = new string[]
- {
- "OvrGrabHit_",
- "OvrTouchHit_"
- };
- private string[] file_path_array_ = new string[]
- {
- "System/maid_collider.bytes",
- "System/maid_collider_touch.bytes"
- };
- private int[] layer_array_ = new int[]
- {
- 17,
- 19
- };
- private List<CapsuleCollider>[] collider_array_ = new List<CapsuleCollider>[2];
- public enum ColliderType
- {
- NON = -1,
- Grab,
- Touch,
- MAX
- }
- }
|