Thomas ICHÉ
3 年前
当前提交
f072314e
共有 14 个文件被更改,包括 308 次插入 和 6 次删除
-
10Editor/PropertyDrawers/ReflectedMemberyPropertyDrawer.cs
-
26Runtime/Ingredients/Rigs/GenericAnimation/GenericFloatAnimationRig.cs
-
106Editor/PropertyDrawers/AnimationHandlerPropertyDrawer.cs
-
11Editor/PropertyDrawers/AnimationHandlerPropertyDrawer.cs.meta
-
8Runtime/Ingredients/Rigs/GenericAnimation/Handlers.meta
-
19Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandler.cs
-
11Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandler.cs.meta
-
19Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandlerAttribute.cs
-
11Runtime/Ingredients/Rigs/GenericAnimation/Handlers/AnimationHandlerAttribute.cs.meta
-
56Runtime/Ingredients/Rigs/GenericAnimation/Handlers/FloatAnimationHandler.cs
-
11Runtime/Ingredients/Rigs/GenericAnimation/Handlers/FloatAnimationHandler.cs.meta
-
15Runtime/Ingredients/Rigs/GenericAnimation/Handlers/HandlerTypeAttribute.cs
-
11Runtime/Ingredients/Rigs/GenericAnimation/Handlers/HandlerTypeAttribute.cs.meta
|
|||
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); |
|||
} |
|||
} |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b9dae9fe60237924199a1ede12aab6a8 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 39f767ee22f05fa40ba6e9376674e2da |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; } |
|||
} |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: bab759fa7ad0bf84a858be6d7d08d364 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: 27f72e7c25503e54c931c85ecf203e2e |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f4df285f285c57f479a58a7d5c99c4fc |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using UnityEngine; |
|||
|
|||
namespace GameplayIngredients.Rigs |
|||
{ |
|||
public class HandlerTypeAttribute : PropertyAttribute |
|||
{ |
|||
public Type type; |
|||
public HandlerTypeAttribute(Type type) |
|||
{ |
|||
this.type = type; |
|||
} |
|||
} |
|||
} |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: a7e904f72ff82344e80c1413c3f14d29 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue