Files
Main/Assets/Launcher/ExternalLibs/PrefabAssetData/AnimationAssetData.cs

272 lines
8.4 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Thousandto.Launcher.ExternalLibs
{
/// <summary>
/// 动作资源信息
/// </summary>
[Serializable]
public class AnimationAssetData
{
[NonSerialized]
//临时存储Animation的组件
private AnimListScript _tempAnim = null;
//路径
public string Path;
//动作片段
public AnimationClip[] ClipArray;
//自身动作片段
public AnimationClip[] SelfClipArray;
//插槽数据
public string[] SlotTrans;
//buildin数据
public string[] BuildinLevel1 = null;
public string[] BuildinLevel2 = null;
public string[] BuildinLevel3 = null;
//默认的动作片段名字
public int DefaultClipIndex = -1;
//编辑器时使用
[NonSerialized]
public Transform TheTransform;
#region//运行时执行
//卸载处理
public void Unload()
{
if (ClipArray != null)
{
for (int i = 0; i < ClipArray.Length; i++)
{
if (ClipArray[i] != null)
{
Resources.UnloadAsset(ClipArray[i]);
}
}
}
if (SelfClipArray != null)
{
for (int i = 0; i < SelfClipArray.Length; i++)
{
if (SelfClipArray[i] != null)
{
Resources.UnloadAsset(SelfClipArray[i]);
}
}
}
if (_tempAnim != null)
{
GameObject.Destroy(_tempAnim);
}
}
//设置Animation
public void ToAnimation(Transform tran)
{
var anim = tran.GetComponent<AnimListScript>();
if (anim == null)
{
anim = tran.gameObject.AddComponent<AnimListScript>();
}
if (anim != null)
{
if(ClipArray != null)
{
for (int i = 0; i < ClipArray.Length; i++)
{
if (ClipArray[i] != null)
{
anim.AddClip(ClipArray[i]);
}
}
}
if(SelfClipArray != null)
{
var list = new List<AnimationClip>();
for (int i = 0; i < SelfClipArray.Length; i++)
{
if (SelfClipArray[i] != null)
{
list.Add(SelfClipArray[i]);
}
}
anim.SelfAnimList = list;
}
}
if(SlotTrans != null)
{
anim.SlotTrans = new Transform[SlotTrans.Length];
for (int i = 0; i < SlotTrans.Length; ++i)
{
if(!string.IsNullOrEmpty(SlotTrans[i]))
{
anim.SlotTrans[i] = tran.Find(SlotTrans[i]);
}
}
}
if (BuildinLevel1 != null)
{
anim.BuildinLevel1 = new GameObject[BuildinLevel1.Length];
for (int i = 0; i < BuildinLevel1.Length; ++i)
{
if (!string.IsNullOrEmpty(BuildinLevel1[i]))
{
anim.BuildinLevel1[i] = tran.Find(BuildinLevel1[i]).gameObject;
}
}
}
if (BuildinLevel2 != null)
{
anim.BuildinLevel2 = new GameObject[BuildinLevel2.Length];
for (int i = 0; i < BuildinLevel2.Length; ++i)
{
if (!string.IsNullOrEmpty(BuildinLevel2[i]))
{
anim.BuildinLevel2[i] = tran.Find(BuildinLevel2[i]).gameObject;
}
}
}
if (BuildinLevel3 != null)
{
anim.BuildinLevel3 = new GameObject[BuildinLevel3.Length];
for (int i = 0; i < BuildinLevel3.Length; ++i)
{
if (!string.IsNullOrEmpty(BuildinLevel3[i]))
{
anim.BuildinLevel3[i] = tran.Find(BuildinLevel3[i]).gameObject;
}
}
}
_tempAnim = anim;
}
#endregion
#region//编辑器时执行--打包之前数据准备用
#if UNITY_EDITOR
//从Animation中获取信息
public bool FromAnimation(AnimListScript anim,bool removeFormAnimation)
{
if (anim != null)
{
var _alist = anim.AnimList;
List<AnimationClip> list = new List<AnimationClip>();
for (int i = 0; i < _alist.Count; i++)
{
if (_alist[i] != null)
{
list.Add(_alist[i]);
}
}
ClipArray = list.ToArray();
list.Clear();
_alist = anim.SelfAnimList;
for (int i = 0; i < _alist.Count; i++)
{
if (_alist[i] != null)
{
list.Add(_alist[i]);
}
}
SelfClipArray = list.ToArray();
if (anim.SlotTrans != null)
{
SlotTrans = new string[anim.SlotTrans.Length];
for(int i = 0; i < SlotTrans.Length; ++i)
{
SlotTrans[i] = FormatPath(anim.SlotTrans[i], anim.transform);
}
}
if (anim.BuildinLevel1 != null)
{
BuildinLevel1 = new string[anim.BuildinLevel1.Length];
for (int i = 0; i < BuildinLevel1.Length; ++i)
{
BuildinLevel1[i] = FormatPath(anim.BuildinLevel1[i], anim.transform);
}
}
if (anim.BuildinLevel2 != null)
{
BuildinLevel2 = new string[anim.BuildinLevel2.Length];
for (int i = 0; i < BuildinLevel2.Length; ++i)
{
BuildinLevel2[i] = FormatPath(anim.BuildinLevel2[i], anim.transform);
}
}
if (anim.BuildinLevel3 != null)
{
BuildinLevel3 = new string[anim.BuildinLevel3.Length];
for (int i = 0; i < BuildinLevel3.Length; ++i)
{
BuildinLevel3[i] = FormatPath(anim.BuildinLevel3[i], anim.transform);
}
}
if (removeFormAnimation)
{
GameObject.DestroyImmediate(anim);
}
return true;
}
return false;
}
#endif
#endregion
#region//私有函数
private static StringBuilder _tmp = new StringBuilder(1024);
private string FormatPath(Transform trans, Transform root)
{
_tmp.Length = 0;
if(trans != null)
{
var tmpTrans = trans;
while (tmpTrans != root)
{
_tmp.Insert(0, "/" + tmpTrans.name);
tmpTrans = tmpTrans.parent;
}
if(_tmp.Length > 0)
{
_tmp.Remove(0, 1);
}
}
return _tmp.ToString();
}
private string FormatPath(GameObject go, Transform root)
{
_tmp.Length = 0;
if (go != null)
{
var tmpTrans = go.transform;
while (tmpTrans != root)
{
_tmp.Insert(0, "/" + tmpTrans.name);
tmpTrans = tmpTrans.parent;
}
if(_tmp.Length > 0)
{
_tmp.Remove(0, 1);
}
}
return _tmp.ToString();
}
#endregion
}
}