using System; using System.Collections.Generic; using System.IO; using UnityEngine; public class CacheBoneDataArray : MonoBehaviour { public void CreateCache(Transform root) { if (CacheBoneDataArray.save_bone_path_set_ == null) { CacheBoneDataArray.save_bone_path_set_ = new HashSet(); string[] saveBonePathArray = CacheBoneDataArray.GetSaveBonePathArray(); for (int i = 0; i < saveBonePathArray.Length; i++) { CacheBoneDataArray.save_bone_path_set_.Add(saveBonePathArray[i]); } } this.path_dic_ = new Dictionary(); this.bone_data = new CacheBoneDataArray.BoneData(root, null); this.bip_root_trans_ = this.bone_data.transform; this.bust_trans_ = new Transform[2]; Action bust_search = null; bust_search = delegate(CacheBoneDataArray.BoneData target_bone_data) { if (this.bust_trans_[0] != null && this.bust_trans_[1] != null) { return; } string name = target_bone_data.transform.gameObject.name; if (name == "Mune_L") { this.bust_trans_[0] = target_bone_data.transform; } else if (name == "Mune_R") { this.bust_trans_[1] = target_bone_data.transform; } for (int j = 0; j < target_bone_data.child_bone_array.Length; j++) { bust_search(target_bone_data.child_bone_array[j]); } }; bust_search(this.bone_data); NDebug.Assert(this.bust_trans_[0] != null && this.bust_trans_[1] != null, "胸ボーンのサーチに失敗しました"); Action create_path_dic = null; create_path_dic = delegate(CacheBoneDataArray.BoneData target_bone_data) { if (!this.path_dic_.ContainsKey(target_bone_data.path)) { this.path_dic_.Add(target_bone_data.path, target_bone_data); } for (int j = 0; j < target_bone_data.child_bone_array.Length; j++) { create_path_dic(target_bone_data.child_bone_array[j]); } }; create_path_dic(this.bone_data); } public byte[] GetAnmBinary(bool use_bust_keyL, bool use_bust_keyR) { if (this.bone_data == null) { NDebug.Assert("ボーンデータがありません", false); return null; } Action write_bone_data = null; write_bone_data = delegate(BinaryWriter w, CacheBoneDataArray.BoneData target_bone_data) { if (CacheBoneDataArray.save_bone_path_set_.Contains(target_bone_data.path)) { int num = 2; w.Write(1); w.Write(target_bone_data.path); Vector3 localPosition = target_bone_data.transform.localPosition; Quaternion localRotation = target_bone_data.transform.localRotation; float[] array = new float[] { localRotation.x, localRotation.y, localRotation.z, localRotation.w }; if (target_bone_data.transform == this.bip_root_trans_) { array = new float[] { localRotation.x, localRotation.y, localRotation.z, localRotation.w, localPosition.x, localPosition.y, localPosition.z }; } for (int i = 0; i < array.Length; i++) { w.Write((byte)(100 + i)); w.Write(num); for (int j = 0; j < num; j++) { w.Write((float)j); w.Write(array[i]); w.Write(0); w.Write(0); } } } for (int k = 0; k < target_bone_data.child_bone_array.Length; k++) { write_bone_data(w, target_bone_data.child_bone_array[k]); } }; MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write("CM3D2_ANIM"); binaryWriter.Write(1001); write_bone_data(binaryWriter, this.bone_data); binaryWriter.Write(0); binaryWriter.Write((!use_bust_keyL) ? 0 : 1); binaryWriter.Write((!use_bust_keyR) ? 0 : 1); binaryWriter.Close(); memoryStream.Close(); byte[] result = memoryStream.ToArray(); memoryStream.Dispose(); return result; } public List>> GetTest() { List>> result = new List>>(); Action>>, CacheBoneDataArray.BoneData> write_bone_data = null; write_bone_data = delegate(List>> dest, CacheBoneDataArray.BoneData target_bone_data) { if (CacheBoneDataArray.save_bone_path_set_.Contains(target_bone_data.path)) { string path = target_bone_data.path; KeyValuePair value = new KeyValuePair(target_bone_data.transform.localRotation, target_bone_data.transform.localPosition); KeyValuePair> item = new KeyValuePair>(path, value); dest.Add(item); } for (int i = 0; i < target_bone_data.child_bone_array.Length; i++) { write_bone_data(result, target_bone_data.child_bone_array[i]); } }; write_bone_data(result, this.bone_data); return result; } public byte[] GetFrameBinary(bool use_bust_keyL, bool use_bust_keyR) { if (this.bone_data == null) { NDebug.Assert("ボーンデータがありません", false); return null; } Action write_bone_data = null; write_bone_data = delegate(BinaryWriter w, CacheBoneDataArray.BoneData target_bone_data) { if (CacheBoneDataArray.save_bone_path_set_.Contains(target_bone_data.path)) { w.Write(1); w.Write(target_bone_data.path); Vector3 localPosition = target_bone_data.transform.localPosition; Quaternion localRotation = target_bone_data.transform.localRotation; float[] array = new float[] { localRotation.x, localRotation.y, localRotation.z, localRotation.w }; if (target_bone_data.transform == this.bip_root_trans_) { array = new float[] { localRotation.x, localRotation.y, localRotation.z, localRotation.w, localPosition.x, localPosition.y, localPosition.z }; } for (int i = 0; i < array.Length; i++) { w.Write(array[i]); } } for (int j = 0; j < target_bone_data.child_bone_array.Length; j++) { write_bone_data(w, target_bone_data.child_bone_array[j]); } }; MemoryStream memoryStream = new MemoryStream(); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write("CM3D2_FRAME_ANIM"); binaryWriter.Write(1001); write_bone_data(binaryWriter, this.bone_data); binaryWriter.Write(0); binaryWriter.Write((!use_bust_keyL) ? 0 : 1); binaryWriter.Write((!use_bust_keyR) ? 0 : 1); binaryWriter.Close(); memoryStream.Close(); byte[] result = memoryStream.ToArray(); memoryStream.Dispose(); return result; } public KeyValuePair SetFrameBinary(byte[] binary) { if (this.bone_data == null) { NDebug.Assert("ボーンデータがありません", false); return new KeyValuePair(false, false); } MemoryStream memoryStream = new MemoryStream(binary); BinaryReader binaryReader = new BinaryReader(memoryStream); string a = binaryReader.ReadString(); if (a != "CM3D2_FRAME_ANIM") { binaryReader.Close(); memoryStream.Close(); memoryStream.Dispose(); return new KeyValuePair(false, false); } int num = binaryReader.ReadInt32(); bool flag = false; if (1001 <= num) { flag = true; } Dictionary> dictionary = new Dictionary>(); while (binaryReader.ReadByte() != 0) { string key = binaryReader.ReadString(); CacheBoneDataArray.BoneData boneData = this.path_dic_[key]; float[] array = new float[4]; if (boneData.transform == this.bip_root_trans_) { array = new float[7]; } for (int i = 0; i < array.Length; i++) { array[i] = binaryReader.ReadSingle(); } if (flag && (boneData.transform == this.bust_trans_[0] || boneData.transform == this.bust_trans_[1])) { int key2 = 0; if (boneData.transform == this.bust_trans_[1]) { key2 = 1; } KeyValuePair value = new KeyValuePair(boneData.transform, array); dictionary.Add(key2, value); } else { boneData.transform.localRotation = new Quaternion(array[0], array[1], array[2], array[3]); if (4 < array.Length) { boneData.transform.localPosition = new Vector3(array[4], array[5], array[6]); } } } bool flag2 = false; bool value2 = false; if (flag) { flag2 = (binaryReader.ReadByte() != 0); value2 = (binaryReader.ReadByte() != 0); List> list = new List>(); if (flag2) { list.Add(dictionary[0]); } else if (flag2) { list.Add(dictionary[1]); } foreach (KeyValuePair keyValuePair in list) { float[] value3 = keyValuePair.Value; keyValuePair.Key.localRotation = new Quaternion(value3[0], value3[1], value3[2], value3[3]); if (4 < value3.Length) { keyValuePair.Key.transform.localPosition = new Vector3(value3[4], value3[5], value3[6]); } } } binaryReader.Close(); memoryStream.Close(); memoryStream.Dispose(); return new KeyValuePair(flag2, value2); } public CacheBoneDataArray.BoneData bone_data { get; private set; } private static string[] GetSaveBonePathArray() { return new string[] { "Bip01", "Bip01/Bip01 Footsteps", "Bip01/Bip01 Pelvis", "Bip01/Bip01 Pelvis/Bip01 L Thigh", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot/Bip01 L Toe0", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot/Bip01 L Toe0/Bip01 L Toe01", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot/Bip01 L Toe1", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot/Bip01 L Toe1/Bip01 L Toe11", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot/Bip01 L Toe2", "Bip01/Bip01 Pelvis/Bip01 L Thigh/Bip01 L Calf/Bip01 L Foot/Bip01 L Toe2/Bip01 L Toe21", "Bip01/Bip01 Pelvis/Bip01 R Thigh", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot/Bip01 R Toe0", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot/Bip01 R Toe0/Bip01 R Toe01", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot/Bip01 R Toe1", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot/Bip01 R Toe1/Bip01 R Toe11", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot/Bip01 R Toe2", "Bip01/Bip01 Pelvis/Bip01 R Thigh/Bip01 R Calf/Bip01 R Foot/Bip01 R Toe2/Bip01 R Toe21", "Bip01/Bip01 Spine", "Bip01/Bip01 Spine/Bip01 Spine0a", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 Neck", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 Neck/Bip01 Head", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger0", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger0/Bip01 L Finger01", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger0/Bip01 L Finger01/Bip01 L Finger02", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger1", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger1/Bip01 L Finger11", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger1/Bip01 L Finger11/Bip01 L Finger12", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger2", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger2/Bip01 L Finger21", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger2/Bip01 L Finger21/Bip01 L Finger22", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger3", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger3/Bip01 L Finger31", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger3/Bip01 L Finger31/Bip01 L Finger32", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger4", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger4/Bip01 L Finger41", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 L Clavicle/Bip01 L UpperArm/Bip01 L Forearm/Bip01 L Hand/Bip01 L Finger4/Bip01 L Finger41/Bip01 L Finger42", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger0", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger0/Bip01 R Finger01", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger0/Bip01 R Finger01/Bip01 R Finger02", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger1", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger1/Bip01 R Finger11", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger1/Bip01 R Finger11/Bip01 R Finger12", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger2", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger2/Bip01 R Finger21", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger2/Bip01 R Finger21/Bip01 R Finger22", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger3", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger3/Bip01 R Finger31", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger3/Bip01 R Finger31/Bip01 R Finger32", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger4", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger4/Bip01 R Finger41", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Bip01 R Clavicle/Bip01 R UpperArm/Bip01 R Forearm/Bip01 R Hand/Bip01 R Finger4/Bip01 R Finger41/Bip01 R Finger42", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Mune_L", "Bip01/Bip01 Spine/Bip01 Spine0a/Bip01 Spine1/Bip01 Spine1a/Mune_R" }; } private Transform bip_root_trans_; private Transform[] bust_trans_; private Dictionary path_dic_; private static HashSet save_bone_path_set_; public class BoneData { public BoneData(Transform trans, string hierarchy_path) { this.transform = trans; this.path = hierarchy_path; if (!string.IsNullOrEmpty(this.path)) { this.path += "/"; } this.path += trans.gameObject.name; List list = new List(); for (int i = 0; i < this.transform.childCount; i++) { Transform child = this.transform.GetChild(i); string name = child.name; if (0 > name.IndexOf("_IK") && 0 > name.IndexOf("_SCL") && 0 > name.IndexOf("nub")) { list.Add(new CacheBoneDataArray.BoneData(child, this.path)); } } this.child_bone_array = list.ToArray(); } public Transform transform { get; private set; } public CacheBoneDataArray.BoneData[] child_bone_array { get; private set; } public string path { get; private set; } } }