浏览代码

Added Animation Handlers

/feature-generic-animation-rigs
Thomas ICHÉ 4 年前
当前提交
f072314e
共有 14 个文件被更改,包括 308 次插入6 次删除
  1. 10
      Editor/PropertyDrawers/ReflectedMemberyPropertyDrawer.cs
  2. 26
      Runtime/Ingredients/Rigs/GenericAnimation/GenericFloatAnimationRig.cs
  3. 106
      Editor/PropertyDrawers/AnimationHandlerPropertyDrawer.cs
  4. 11
      Editor/PropertyDrawers/AnimationHandlerPropertyDrawer.cs.meta
  5. 8
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers.meta
  6. 19
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandler.cs
  7. 11
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandler.cs.meta
  8. 19
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandlerAttribute.cs
  9. 11
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandlerAttribute.cs.meta
  10. 56
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/FloatAnimationHandler.cs
  11. 11
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/FloatAnimationHandler.cs.meta
  12. 15
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/HandlerTypeAttribute.cs
  13. 11
      Runtime/Ingredients/Rigs/GenericAnimation/Handlers/HandlerTypeAttribute.cs.meta

10
Editor/PropertyDrawers/ReflectedMemberyPropertyDrawer.cs


}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight + 8;
}
GUI.Box(position, GUIContent.none, EditorStyles.helpBox);
position = new RectOffset(4, 4, 4, 4).Remove(position);
SerializedProperty obj = property.FindPropertyRelative("m_TargetObject");
SerializedProperty propName = property.FindPropertyRelative("m_MemberName");

var p = property.serializedObject.targetObject.GetType().GetMember(property.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField).First();
var attr = p.GetCustomAttribute<ReflectedMemberAttribute>();
if (attr != null) // If using a ReflectedMemberAttribute for filtering type

26
Runtime/Ingredients/Rigs/GenericAnimation/GenericFloatAnimationRig.cs


bool useStoredValueAsBase = true;
[SerializeField, DisableIf("useStoredValueAsBase")]
float baseValue = 1.0f;
[Header("Animation")]
[SerializeReference, HandlerType(typeof(float))]
public FloatAnimationHandler animationHandler = new FloatContinuousAnimationHandler();
[Header("Animation")]
public float frequency = 1.0f;
public float amplitude = 1.0f;
public override Type animatedType => typeof(float);

baseValue = (float)property.GetValue();
baseValue = (float)property.GetValue();
}
protected override void OnEnable()
{
base.OnEnable();
animationHandler?.OnStart(baseValue);
return Mathf.Sin(Time.time * frequency * Mathf.PI) * amplitude + baseValue;
if (animationHandler != null)
{
return animationHandler.OnUpdate(deltaTime);
}
else
{
Debug.LogWarning("Float Animation Rig has no Animation Handler", this);
return baseValue;
}
}
}
}

106
Editor/PropertyDrawers/AnimationHandlerPropertyDrawer.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using GameplayIngredients.Rigs;
using System.Reflection;
using System;
using System.Linq;
namespace GameplayIngredients.Editor
{
[CustomPropertyDrawer(typeof(AnimationHandler), true)]
public class AnimationHandlerPropertyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true) + 8;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var targetObj = property.serializedObject.targetObject;
MemberInfo selfInfo = targetObj.GetType().GetMember(property.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField).First();
if (typeToAdd != null)
{
if (selfInfo.MemberType == MemberTypes.Property)
{
Undo.RecordObject(targetObj, "Change Animation Handler");
(selfInfo as PropertyInfo).SetValue(targetObj, Activator.CreateInstance(typeToAdd));
typeToAdd = null;
}
else if (selfInfo.MemberType == MemberTypes.Field)
{
Undo.RecordObject(targetObj, "Change Animation Handler");
(selfInfo as FieldInfo).SetValue(targetObj, Activator.CreateInstance(typeToAdd));
typeToAdd = null;
}
else
throw new Exception($"Could not find field/property of name {property.name} on object {targetObj.name}");
}
else
{
GUI.Box(position, GUIContent.none, EditorStyles.helpBox);
position = new RectOffset(4, 4, 4, 4).Remove(position);
using (new EditorGUI.IndentLevelScope(1))
{
Rect r = position;
r.xMin += EditorGUIUtility.labelWidth;
r.height = EditorGUIUtility.singleLineHeight;
var p = property.serializedObject.targetObject.GetType().GetMember(property.name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.GetField).First();
var attr = p.GetCustomAttribute<HandlerTypeAttribute>();
if (attr != null)
{
var type = attr.type;
Type curType = typeof(object);
if (selfInfo.MemberType == MemberTypes.Property)
curType = (selfInfo as PropertyInfo).GetValue(targetObj).GetType();
else if (selfInfo.MemberType == MemberTypes.Field)
curType = (selfInfo as FieldInfo).GetValue(targetObj).GetType();
string name = curType.Name;
var typeAttr = curType.GetCustomAttribute<AnimationHandlerAttribute>();
if (typeAttr != null)
name = typeAttr.menuPath;
if (EditorGUI.DropdownButton(r, new GUIContent(name), FocusType.Passive))
{
PromptMenuFor(r, type, curType);
}
}
else
{
using (new EditorGUI.DisabledGroupScope(true))
EditorGUI.DropdownButton(r, new GUIContent("(Property does not implement [HandlerType] attribute)"), FocusType.Passive);
}
EditorGUI.PropertyField(position, property, true);
}
}
}
Type typeToAdd = null;
void PromptMenuFor(Rect position, Type filterType, Type currentType)
{
var allHandlers = TypeUtility.GetConcreteTypes<AnimationHandler>();
GenericMenu m = new GenericMenu();
foreach(var handlerType in allHandlers)
{
var attr = handlerType.GetCustomAttribute<AnimationHandlerAttribute>();
if (attr != null && attr.type == filterType)
{
m.AddItem(new GUIContent(attr.menuPath), handlerType == currentType, () => { typeToAdd = handlerType; });
}
}
m.DropDown(position);
}
}
}

11
Editor/PropertyDrawers/AnimationHandlerPropertyDrawer.cs.meta


fileFormatVersion: 2
guid: b9dae9fe60237924199a1ede12aab6a8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Runtime/Ingredients/Rigs/GenericAnimation/Handlers.meta


fileFormatVersion: 2
guid: 39f767ee22f05fa40ba6e9376674e2da
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

19
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandler.cs


using System;
namespace GameplayIngredients.Rigs
{
public abstract class AnimationHandler<T> : AnimationHandler
{
public sealed override Type animatedType => typeof(T);
public abstract void OnStart(T defaultValue);
public abstract T OnUpdate(float deltaTime);
}
public abstract class AnimationHandler
{
public abstract Type animatedType { get; }
}
}

11
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandler.cs.meta


fileFormatVersion: 2
guid: bab759fa7ad0bf84a858be6d7d08d364
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

19
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandlerAttribute.cs


using System;
namespace GameplayIngredients.Rigs
{
[AttributeUsage(AttributeTargets.Class)]
public class AnimationHandlerAttribute : Attribute
{
public string menuPath;
public Type type;
public AnimationHandlerAttribute(string menuPath, Type type)
{
this.type = type;
this.menuPath = menuPath;
}
}
}

11
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandlerAttribute.cs.meta


fileFormatVersion: 2
guid: 27f72e7c25503e54c931c85ecf203e2e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

56
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/FloatAnimationHandler.cs


using System;
using UnityEngine;
namespace GameplayIngredients.Rigs
{
[Serializable]
public abstract class FloatAnimationHandler : AnimationHandler<float> { }
[Serializable, AnimationHandler("Continuous Float", typeof(float))]
public class FloatContinuousAnimationHandler : FloatAnimationHandler
{
[SerializeField]
float Rate = 1.0f;
float m_Base;
float m_Time;
public override void OnStart(float defaultValue)
{
m_Base = defaultValue;
m_Time = 0;
}
public override float OnUpdate(float deltaTime)
{
m_Time += deltaTime;
return m_Base + m_Time * Rate;
}
}
[Serializable, AnimationHandler("Sine Float", typeof(float))]
public class FloatSineAnimationHandler : FloatAnimationHandler
{
[SerializeField]
float frequency = 1.0f;
[SerializeField]
public float amplitude = 1.0f;
float m_Base;
float m_Time;
public override void OnStart(float defaultValue)
{
m_Base = defaultValue;
m_Time = 0;
}
public override float OnUpdate(float deltaTime)
{
m_Time += deltaTime;
return m_Base + Mathf.Sin(m_Time * frequency * Mathf.PI) * amplitude;
}
}
}

11
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/FloatAnimationHandler.cs.meta


fileFormatVersion: 2
guid: f4df285f285c57f479a58a7d5c99c4fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

15
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/HandlerTypeAttribute.cs


using System;
using UnityEngine;
namespace GameplayIngredients.Rigs
{
public class HandlerTypeAttribute : PropertyAttribute
{
public Type type;
public HandlerTypeAttribute(Type type)
{
this.type = type;
}
}
}

11
Runtime/Ingredients/Rigs/GenericAnimation/Handlers/HandlerTypeAttribute.cs.meta


fileFormatVersion: 2
guid: a7e904f72ff82344e80c1413c3f14d29
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存