UnityAnimationClip动画事件修改
1、修改动画事件属性。
private static void Clone(AnimationEvent l, AnimationEvent r,float length)
{
r.functionName = l.functionName;
r.floatParameter = l.floatParameter;
r.intParameter = l.intParameter;
r.objectReferenceParameter = l.objectReferenceParameter;
r.stringParameter = l.stringParameter;
r.time = l.time / length;
}
[MenuItem("Kit/Asset/修改AnimationEvent ObjParam的链接")]
static void SetAnimClipEventObjectPath()
{
UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets); if (selObjs == null || selObjs.Length == 0)
{
Debug.LogError("请选择需要添加帧事件的动画!");
return;
}
foreach (UnityEngine.Object obj in selObjs)
{
if (obj.GetType() != typeof(GameObject))
continue;
GameObject fbx = (GameObject)obj;
string fbxPath = AssetDatabase.GetAssetPath(fbx);
UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
foreach (UnityEngine.Object objGo in assets)
{
if (objGo.GetType() != typeof(AnimationClip))
continue;
AnimationClip clipGo = (AnimationClip)objGo;
AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clipGo);
if (events != null && events.Length != 0)
{
List<AnimationEvent> events_clone = new List<AnimationEvent>();
for (int i = 0; i < events.Length;++i)
{
AnimationEvent newEvent = new AnimationEvent();
Clone(events[i], newEvent, clipGo.length); //传⼊长度。事件time 设置的长度的⽐例值
// 我这⾥实现 将object 链接,打断并把路径设置到stringparam内;
if (newEvent.objectReferenceParameter != null)
{
string str = newEvent.stringParameter;
string path = AssetDatabase.GetAssetPath(newEvent.objectReferenceParameter);
int idx = path.LastIndexOf('.');
string assets_path = "Assets/Resources/";
path = path.Substring(0, idx);
path = path.Substring(assets_path.Length);
int index = str.LastIndexOf('}');
if (index < 0)
{
newEvent.stringParameter = "{\"Prefab\":\"" + path + "\"}";
}
else
{
newEvent.stringParameter = newEvent.stringParameter.Substring(0, index);
newEvent.stringParameter += ",\"Prefab\":\"" + path + "\"}";
}
newEvent.objectReferenceParameter = null;
}
events_clone.Add(newEvent);
}
ModelImporter modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(clipGo)) as ModelImporter;
if (modelImporter == null)
return;
SerializedObject serializedObject = new SerializedObject(modelImporter);
SerializedProperty clipAnimations = serializedObject.FindProperty("m_ClipAnimations");
for (int i = 0; i < modelImporter.clipAnimations.Length; i++)
{
if (modelImporter.clipAnimations[i].name == clipGo.name)
{
SerializedProperty m_clip = clipAnimations.GetArrayElementAtIndex(i);
SerializedProperty m_events = m_clip.FindPropertyRelative("events");
m_events.ClearArray();
foreach (AnimationEvent evt in events_clone)
{
m_events.InsertArrayElementAtIndex(m_events.arraySize);
SetEvent(m_events, m_events.arraySize - 1, evt);
}
break;
}
}
serializedObject.ApplyModifiedProperties();
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(clipGo));
AssetDatabase.Refresh();
}
}
}
}
static void SetEvent(SerializedProperty events, int index, AnimationEvent animationEvent)
{
//Debug.Log("set event");
if (events != null && events.isArray)
{
if (index < events.arraySize)
{
events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue =
animationEvent.floatParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue =
animationEvent.functionName;
typeof arrayevents.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue =
animationEvent.intParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue =
animationEvent.stringParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue = animationEvent.time;
}
else
{
Debug.LogWarning("Invalid Event Index");
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论