123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- 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<IKColliderMeta>()
- where base.gameObject != e.gameObject
- select e).ToArray<IKColliderMeta>();
- 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<StreamWriter>(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<FullBodyIKMgr.IKEffectorType, List<ANativeColliderBase>> 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<NativeColliderStatus>();
- 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<IKColliderSaveLoader.IKColliderDataPackage>(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 anativeColliderBase = null;
- foreach (NativeColliderStatus nativeColliderStatus in ikcolliderData.colliderStatusList)
- {
- switch (nativeColliderStatus.colliderType)
- {
- case NativeColliderStatus.ColliderType.Plane:
- anativeColliderBase = new GameObject(name).AddComponent<NativePlaneCollider>();
- break;
- case NativeColliderStatus.ColliderType.Capsule:
- anativeColliderBase = new GameObject(name).AddComponent<NativeCapsuleCollider>();
- break;
- case NativeColliderStatus.ColliderType.Sphere:
- anativeColliderBase = new GameObject(name).AddComponent<NativeSphereCollider>();
- break;
- }
- if (anativeColliderBase != null)
- {
- anativeColliderBase.SetStatus(nativeColliderStatus, this.fullbodyIK.body);
- this.IKEffectorColliderDic[ikcolliderData.effectorType].Add(anativeColliderBase);
- }
- }
- }
- }
- public List<ANativeColliderBase> 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<FullBodyIKMgr.IKEffectorType, List<ANativeColliderBase>> IKEffectorColliderDic = new Dictionary<FullBodyIKMgr.IKEffectorType, List<ANativeColliderBase>>();
- private PermTempAction OnPostLoad = new PermTempAction();
- [Serializable]
- public class IKColliderData : ASerializationVersionControl
- {
- public IKColliderData()
- {
- }
- public IKColliderData(KeyValuePair<FullBodyIKMgr.IKEffectorType, List<NativeColliderStatus>> 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<string>();
- 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<NativeColliderStatus>(json);
- switch (nativeColliderStatus.colliderType)
- {
- case NativeColliderStatus.ColliderType.Plane:
- {
- NativePlaneColliderStatus item = JsonUtility.FromJson<NativePlaneColliderStatus>(json);
- this.colliderStatusList.Add(item);
- break;
- }
- case NativeColliderStatus.ColliderType.Capsule:
- {
- NativeCapsuleColliderStatus item2 = JsonUtility.FromJson<NativeCapsuleColliderStatus>(json);
- this.colliderStatusList.Add(item2);
- break;
- }
- case NativeColliderStatus.ColliderType.Sphere:
- {
- NativeSphereColliderStatus item3 = JsonUtility.FromJson<NativeSphereColliderStatus>(json);
- this.colliderStatusList.Add(item3);
- break;
- }
- case NativeColliderStatus.ColliderType.MaidPropCol:
- {
- NativeMaidPropColliderStatus item4 = JsonUtility.FromJson<NativeMaidPropColliderStatus>(json);
- this.colliderStatusList.Add(item4);
- break;
- }
- }
- }
- this.StatusJsonStrList.Clear();
- this.StatusJsonStrList = null;
- }
- public FullBodyIKMgr.IKEffectorType effectorType;
- [NonSerialized]
- public List<NativeColliderStatus> colliderStatusList = new List<NativeColliderStatus>();
- [SerializeField]
- private List<string> StatusJsonStrList;
- }
- [Serializable]
- public class IKColliderDataPackage : ASerializationVersionControl
- {
- public override int FixVersion
- {
- get
- {
- return 1000;
- }
- }
- public List<IKColliderSaveLoader.IKColliderData> dataList = new List<IKColliderSaveLoader.IKColliderData>();
- }
- }
- }
|