using System; using System.Collections.Generic; using System.IO; using System.Linq; using kt.Physics; using kt.Serialization; using UnityEngine; namespace kt.ik { public class IKColliderSaveLoader : MonoBehaviour { public string directoryPath { get { return this.DirectoryPath; } set { this.DirectoryPath = value; } } public string fileName { get { return this.FileName; } set { this.FileName = value; } } public PermTempAction onPostLoad { get { return this.OnPostLoad; } } private void UpdateColliderMetaArray() { this.ColliderMetaArray = (from e in base.GetComponentsInChildren() where base.gameObject != e.gameObject select e).ToArray(); foreach (IKColliderMeta ikcolliderMeta in this.ColliderMetaArray) { this.IKEffectorColliderDic[ikcolliderMeta.effectorType] = ikcolliderMeta.colliderList; } } public void ColliderSave() { this.UpdateColliderMetaArray(); KasaiUtility.FileSave(this.directoryPath + this.fileName, new Action(this.Save)); } public void ColliderLoad() { KasaiUtility.FileLoadGameData(this.fileName, delegate(StreamReader reader) { this.UpdateColliderMetaArray(); if (this.ColliderMetaArray != null) { for (int i = 0; i < this.ColliderMetaArray.Length; i++) { foreach (ANativeColliderBase anativeColliderBase in this.ColliderMetaArray[i].colliderList) { if (anativeColliderBase) { UnityEngine.Object.DestroyImmediate(anativeColliderBase.gameObject); } } } } this.IKEffectorColliderDic.Clear(); this.Load(reader); this.onPostLoad.Invoke(); }, null); } public void Save(StreamWriter writer) { IKColliderSaveLoader.IKColliderDataPackage ikcolliderDataPackage = new IKColliderSaveLoader.IKColliderDataPackage(); foreach (KeyValuePair> keyValuePair in this.IKEffectorColliderDic) { IKColliderSaveLoader.IKColliderData ikcolliderData = new IKColliderSaveLoader.IKColliderData(); ikcolliderData.effectorType = keyValuePair.Key; ikcolliderData.colliderStatusList = (from col in keyValuePair.Value select col.status).ToList(); ikcolliderDataPackage.dataList.Add(ikcolliderData); } string value = JsonUtility.ToJson(ikcolliderDataPackage, true); writer.Write(value); } public void Load(StreamReader reader) { string json = reader.ReadToEnd(); IKColliderSaveLoader.IKColliderDataPackage ikcolliderDataPackage = JsonUtility.FromJson(json); foreach (IKColliderSaveLoader.IKColliderData ikcolliderData in ikcolliderDataPackage.dataList) { string name = string.Format("IK_{0}_Collider", ikcolliderData.effectorType); this.IKEffectorColliderDic[ikcolliderData.effectorType] = new List(); ANativeColliderBase anativeColliderBase = null; foreach (NativeColliderStatus nativeColliderStatus in ikcolliderData.colliderStatusList) { switch (nativeColliderStatus.colliderType) { case NativeColliderStatus.ColliderType.Plane: anativeColliderBase = new GameObject(name).AddComponent(); break; case NativeColliderStatus.ColliderType.Capsule: anativeColliderBase = new GameObject(name).AddComponent(); break; case NativeColliderStatus.ColliderType.Sphere: anativeColliderBase = new GameObject(name).AddComponent(); break; } if (anativeColliderBase != null) { anativeColliderBase.SetStatus(nativeColliderStatus, this.fullbodyIK.body); this.IKEffectorColliderDic[ikcolliderData.effectorType].Add(anativeColliderBase); } } } } public List GetColliderList(FullBodyIKMgr.IKEffectorType effector_type) { if (this.IKEffectorColliderDic.ContainsKey(effector_type)) { return this.IKEffectorColliderDic[effector_type]; } return null; } private string DirectoryPath = Path.GetFullPath("./GameData/system_gp003/collider/"); private string FileName = "ik_collider.ikcol"; public FullBodyIKMgr fullbodyIK; private IKColliderMeta[] ColliderMetaArray; private Dictionary> IKEffectorColliderDic = new Dictionary>(); private PermTempAction OnPostLoad = new PermTempAction(); [Serializable] public class IKColliderData : ASerializationVersionControl { public IKColliderData() { } public IKColliderData(KeyValuePair> pair) { this.effectorType = pair.Key; this.colliderStatusList = pair.Value; } public override int FixVersion { get { return 1000; } } public override void OnBeforeSerialize() { base.OnBeforeSerialize(); this.StatusJsonStrList = new List(); for (int i = 0; i < this.colliderStatusList.Count; i++) { NativeColliderStatus nativeColliderStatus = this.colliderStatusList[i]; string text = string.Empty; switch (nativeColliderStatus.colliderType) { case NativeColliderStatus.ColliderType.Plane: text = JsonUtility.ToJson(nativeColliderStatus as NativePlaneColliderStatus); break; case NativeColliderStatus.ColliderType.Capsule: text = JsonUtility.ToJson(nativeColliderStatus as NativeCapsuleColliderStatus); break; case NativeColliderStatus.ColliderType.Sphere: text = JsonUtility.ToJson(nativeColliderStatus as NativeSphereColliderStatus); break; case NativeColliderStatus.ColliderType.MaidPropCol: text = JsonUtility.ToJson(nativeColliderStatus as NativeMaidPropColliderStatus); break; } if (!string.IsNullOrEmpty(text)) { this.StatusJsonStrList.Add(text); } } } public override void OnAfterDeserialize() { base.OnAfterDeserialize(); this.colliderStatusList.Clear(); for (int i = 0; i < this.StatusJsonStrList.Count; i++) { string json = this.StatusJsonStrList[i]; NativeColliderStatus nativeColliderStatus = JsonUtility.FromJson(json); switch (nativeColliderStatus.colliderType) { case NativeColliderStatus.ColliderType.Plane: { NativePlaneColliderStatus item = JsonUtility.FromJson(json); this.colliderStatusList.Add(item); break; } case NativeColliderStatus.ColliderType.Capsule: { NativeCapsuleColliderStatus item2 = JsonUtility.FromJson(json); this.colliderStatusList.Add(item2); break; } case NativeColliderStatus.ColliderType.Sphere: { NativeSphereColliderStatus item3 = JsonUtility.FromJson(json); this.colliderStatusList.Add(item3); break; } case NativeColliderStatus.ColliderType.MaidPropCol: { NativeMaidPropColliderStatus item4 = JsonUtility.FromJson(json); this.colliderStatusList.Add(item4); break; } } } this.StatusJsonStrList.Clear(); this.StatusJsonStrList = null; } public FullBodyIKMgr.IKEffectorType effectorType; [NonSerialized] public List colliderStatusList = new List(); [SerializeField] private List StatusJsonStrList; } [Serializable] public class IKColliderDataPackage : ASerializationVersionControl { public override int FixVersion { get { return 1000; } } public List dataList = new List(); } } }