using System; using System.Collections.Generic; using UnityEngine; namespace Thousandto.Launcher.ExternalLibs { [Serializable] public class AnimInfo { [SerializeField] private string _name; [SerializeField] private string _path; [SerializeField] private AnimationClip _clip; [SerializeField] private bool _isSaveClip = true; private AnimationClip _tmpclip; public AnimationClip clip { get { if (_tmpclip == null) { if (_clip != null) { _tmpclip = _clip; } else { _tmpclip = GetClip(_path); } } return _tmpclip; } } public string path { get { if (string.IsNullOrEmpty(_path)) { return GetPath(clip); } return _path; } } public string name { get { if (string.IsNullOrEmpty(_name)) { return clip.name; } return _name; } } public bool IsSaveClip { get { return _isSaveClip; } set { if (_isSaveClip != value) { _isSaveClip = value; Reset(); } } } public override string ToString() { return name; } private void Reset() { if (_isSaveClip) { _clip = clip; _name = String.Empty; _path = String.Empty; } else { _name = clip.name; _path = GetPath(clip); _clip = null; } } public static implicit operator AnimInfo(AnimationClip clip) { return AnimInfo.Create(clip); } public static AnimInfo Create(AnimationClip clip, bool isSaveClip = true) { var ai = new AnimInfo(); ai._isSaveClip = isSaveClip; ai._tmpclip = clip; ai.Reset(); return ai; } public static List Convert(List clips, bool isSaveClip = true) { return clips.ConvertAll(x => { return Create(x, isSaveClip); }); } public static List Convert(List clips) { if (clips == null) return new List(); return clips.ConvertAll(x => { return x.clip; }); } public static void ChangedSaveClipFlag(List clips, bool isSaveClip) { if (clips == null) return; for (int i = 0; i < clips.Count; i++) { clips[i].IsSaveClip = isSaveClip; } } #region //编辑器使用 //编辑器使用 #if UNITY_EDITOR [NonSerialized] public float playPos = 0; [NonSerialized] public bool isPlaying = false; public static Dictionary _playerClips = new Dictionary(); #endif public static string GetPath(AnimationClip ac) { #if UNITY_EDITOR if (ac != null) { return UnityEditor.AssetDatabase.GetAssetPath(ac); } #endif return string.Empty; } public static AnimationClip GetClip(string path) { #if UNITY_EDITOR if (!string.IsNullOrEmpty(path)) { if (!_playerClips.ContainsKey(path)) { Debug.LogError("GetClip,LoadFromAsset:"+path); _playerClips[path] = UnityEditor.AssetDatabase.LoadAssetAtPath(path); } return _playerClips[path]; } #endif return null; } public static List GetClips(IEnumerable paths) { List result = new List(); #if UNITY_EDITOR var e = paths.GetEnumerator(); while (e.MoveNext()) { Debug.LogError("GetClip:::" + e.Current); var c = GetClip(e.Current); if (c != null) { result.Add(c); } } #endif return result; } public static void ClearClipsByEditor() { #if UNITY_EDITOR if (_playerClips.Count > 300) { string[] keys = new string[_playerClips.Count]; _playerClips.Keys.CopyTo(keys, 0); for (int i = 0; i < keys.Length; i++) { if (keys[i].ToLower().IndexOf("player") < 0) { _playerClips.Remove(keys[i]); } } Debug.LogError("清理Clips......"); } Resources.UnloadUnusedAssets(); #endif } #endregion } }