1.String参数的获取

今天在弄动画事件的时候需要对动画文件的AnimationEvent进行更新,需要使用SerializedProperty来进行更新,但是遇到个问题就是我在FindstringParameter时报空指针了,于是把stringParameter更换为了被弃用的data就正常了....

2.AnimationUtility.GetAnimationEvents(AnimationClip)Api问题,使用这个api读取的AnimationEvents时,所有读取到的AnimationEvent的time都是错误的,小于你设置的time,推荐使用下面的方式来进行读取

protected AnimationClip CurrentClip;
SerializedObject _clipSerializedObject;
SerializedProperty _eventsProperty;

private SerializedObject ClipSerializedObject
{
	get
	{
		if (_clipSerializedObject == null)
		{
			 ModelImporter modelImporter =
							AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(CurrentClip)) as ModelImporter;
			if (modelImporter == null)
				return null;
			
			_clipSerializedObject = new SerializedObject(modelImporter);
		}
		
		return _clipSerializedObject;
	}
}

private SerializedProperty EventsProperty
{
	get
	{
		if (_eventsProperty == null)
		{
			SerializedProperty clipAnimations = ClipSerializedObject.FindProperty("m_ClipAnimations");

			if (!clipAnimations.isArray)
				return null;

			for (int i = 0; i < clipAnimations.arraySize; i++)
			{
				var animName = clipAnimations.GetArrayElementAtIndex(i).FindPropertyRelative("name").stringValue;

				if (animName == CurrentClip.name)
				{
					_eventsProperty = clipAnimations.GetArrayElementAtIndex(i).FindPropertyRelative("events");

					break;
				}
			}
		}
		
		return _eventsProperty;
	}
}


private void _getEvents()
{
	if (EventsProperty != null && EventsProperty.isArray)
	{
		_events.Clear();
		for (var index = 0; index < EventsProperty.arraySize; index++)
		{
			_events.Add(_getEvent(index));
		}
	}
}

private AnimationEvent _getEvent(int index)
{
	AnimationEvent evt = new AnimationEvent();

	if (EventsProperty != null && EventsProperty.isArray)
	{
		if (index < EventsProperty.arraySize)
		{
			var evtPro = EventsProperty.GetArrayElementAtIndex(index);
			evt.floatParameter = evtPro.FindPropertyRelative("floatParameter").floatValue;
			evt.functionName = evtPro.FindPropertyRelative("functionName").stringValue;
			evt.intParameter = evtPro.FindPropertyRelative("intParameter").intValue;
			evt.objectReferenceParameter = evtPro.FindPropertyRelative("objectReferenceParameter").objectReferenceValue;
			evt.stringParameter = evtPro.FindPropertyRelative("data").stringValue;
			evt.time = evtPro.FindPropertyRelative("time").floatValue;
		}
		else
		{
			Debug.LogWarning("Invalid Event Index");
		}
	}

	return evt;
}

3.更新,添加,删除AnimationEvent注意点. 官方人员的解答

AnimationUtility相关的操作无法更新导入的动画,使用SerializedObject来进行更新

更新后注意记得需要重新导入一下资源,如下代码

void _reImportClip()
{
	ClipSerializedObject.ApplyModifiedProperties();

	EditorUtility.SetDirty(CurrentClip);

	AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(CurrentClip));

	//Todo ArgumentException: GUILayout: Mismatched LayoutGroup.Repaint
	GUIUtility.ExitGUI();
}

当你重更新导入资源后你如果用了GUILayout会得到一个ArgumentException: GUILayout: Mismatched LayoutGroup.Repaint的错误,解决办法是在你导入资源之后执行GUIUtility.ExitGUI()