更新
This commit is contained in:
157
Assets/Scripts/Game/Animations/Custom/PropertyAnimationColor.cs
Normal file
157
Assets/Scripts/Game/Animations/Custom/PropertyAnimationColor.cs
Normal file
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Ichni.RhythmGame.Beatmap;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ichni.RhythmGame
|
||||
{
|
||||
public class PropertyAnimationColor : AnimationBase
|
||||
{
|
||||
#region [暴露属性字段与关联] Exposed Fields & References
|
||||
public string componentName;
|
||||
public string propertyName;
|
||||
public FlexibleFloat propertyValueR, propertyValueG, propertyValueB, propertyValueA;
|
||||
|
||||
private Component targetComponent;
|
||||
private FieldInfo targetField;
|
||||
private PropertyInfo targetProperty;
|
||||
|
||||
// 高性能赋值委托
|
||||
private Action<Color> colorSetterDelegate;
|
||||
#endregion
|
||||
|
||||
#region [生命周期与工厂] Lifecycle & Factory
|
||||
public static PropertyAnimationColor GenerateElement(string elementName, Guid id, List<string> tags, bool isFirstGenerated,
|
||||
GameElement animatedObject, string componentName, string propertyName,
|
||||
FlexibleFloat propertyValueR, FlexibleFloat propertyValueG, FlexibleFloat propertyValueB, FlexibleFloat propertyValueA)
|
||||
{
|
||||
PropertyAnimationColor animation = Instantiate(GameManager.Instance.basePrefabs.emptyObject)
|
||||
.AddComponent<PropertyAnimationColor>();
|
||||
|
||||
animation.Initialize(elementName, id, tags, isFirstGenerated, animatedObject);
|
||||
|
||||
animation.animatedObject = animatedObject;
|
||||
|
||||
animation.componentName = componentName;
|
||||
animation.propertyName = propertyName;
|
||||
animation.propertyValueR = propertyValueR;
|
||||
animation.propertyValueG = propertyValueG;
|
||||
animation.propertyValueB = propertyValueB;
|
||||
animation.propertyValueA = propertyValueA;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public override void AfterInitialize()
|
||||
{
|
||||
if (animatedObject == null || string.IsNullOrEmpty(componentName)) return;
|
||||
|
||||
Type componentType = GetTypeFromAllAssemblies(componentName);
|
||||
if (componentType != null)
|
||||
{
|
||||
targetComponent = animatedObject.GetComponentInChildren(componentType);
|
||||
}
|
||||
|
||||
if (targetComponent != null)
|
||||
{
|
||||
InitializeReflection();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[PropertyAnimationColor] Cannot find Component '{componentName}' strictly on '{animatedObject.name}'.");
|
||||
}
|
||||
|
||||
base.AfterInitialize();
|
||||
}
|
||||
|
||||
private Type GetTypeFromAllAssemblies(string typeName)
|
||||
{
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
Type type = assembly.GetType(typeName);
|
||||
if (type != null) return type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void InitializeReflection()
|
||||
{
|
||||
Type type = targetComponent.GetType();
|
||||
|
||||
targetProperty = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (targetProperty != null && targetProperty.CanWrite)
|
||||
{
|
||||
MethodInfo setMethod = targetProperty.GetSetMethod(nonPublic: true);
|
||||
if (setMethod != null && targetProperty.PropertyType == typeof(Color))
|
||||
{
|
||||
try
|
||||
{
|
||||
colorSetterDelegate = (Action<Color>)Delegate.CreateDelegate(typeof(Action<Color>), targetComponent, setMethod);
|
||||
return;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
targetField = type.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (targetField == null && targetProperty == null)
|
||||
{
|
||||
Debug.LogWarning($"[PropertyAnimationColor] Cannot find target Property or Field '{propertyName}' strictly on '{componentName}'.");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region [核心动画逻辑] Core Animation Logic
|
||||
protected override void UpdateAnimation(float songTime)
|
||||
{
|
||||
if (targetComponent == null) return;
|
||||
|
||||
propertyValueR?.UpdateFlexibleFloat(songTime);
|
||||
propertyValueG?.UpdateFlexibleFloat(songTime);
|
||||
propertyValueB?.UpdateFlexibleFloat(songTime);
|
||||
propertyValueA?.UpdateFlexibleFloat(songTime);
|
||||
|
||||
// 获取最新颜色(如果没有对应颜色参数或者为空则默认 1 避免黑屏,但这依赖具体业务传值安全度)
|
||||
float r = propertyValueR != null ? propertyValueR.value : 1f;
|
||||
float g = propertyValueG != null ? propertyValueG.value : 1f;
|
||||
float b = propertyValueB != null ? propertyValueB.value : 1f;
|
||||
float a = propertyValueA != null ? propertyValueA.value : 1f;
|
||||
|
||||
Color targetColor = new Color(r, g, b, a);
|
||||
|
||||
if (colorSetterDelegate != null)
|
||||
{
|
||||
colorSetterDelegate(targetColor);
|
||||
}
|
||||
else if (targetProperty != null)
|
||||
{
|
||||
targetProperty.SetValue(targetComponent, targetColor);
|
||||
}
|
||||
else if (targetField != null)
|
||||
{
|
||||
targetField.SetValue(targetComponent, targetColor);
|
||||
}
|
||||
|
||||
if (animatedObject is IHaveDirtyMarkSubmodule dirtyTarget)
|
||||
{
|
||||
dirtyTarget.dirtyMarkSubmodule.MarkDirty(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ApplyTimeOffset(float offset)
|
||||
{
|
||||
base.ApplyTimeOffset(offset);
|
||||
void ApplyOffset(FlexibleFloat ff)
|
||||
{
|
||||
if (ff == null || ff.animations == null) return;
|
||||
foreach(var a in ff.animations) { a.startTime += offset; a.endTime += offset; }
|
||||
}
|
||||
ApplyOffset(propertyValueR);
|
||||
ApplyOffset(propertyValueG);
|
||||
ApplyOffset(propertyValueB);
|
||||
ApplyOffset(propertyValueA);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7532e5b8b30e2842ba4f4a638277c64
|
||||
145
Assets/Scripts/Game/Animations/Custom/PropertyAnimationFloat.cs
Normal file
145
Assets/Scripts/Game/Animations/Custom/PropertyAnimationFloat.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Ichni.RhythmGame.Beatmap;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ichni.RhythmGame
|
||||
{
|
||||
public class PropertyAnimationFloat : AnimationBase
|
||||
{
|
||||
#region [暴露属性字段与关联] Exposed Fields & References
|
||||
public string componentName;
|
||||
public string propertyName;
|
||||
public FlexibleFloat propertyValue;
|
||||
|
||||
private Component targetComponent;
|
||||
private FieldInfo targetField;
|
||||
private PropertyInfo targetProperty;
|
||||
|
||||
// 我们尝试通过原生的 Action 来降低反射 SetValue 带来的性能损耗(装箱与GC)
|
||||
private Action<float> floatSetterDelegate;
|
||||
#endregion
|
||||
|
||||
#region [生命周期与工厂] Lifecycle & Factory
|
||||
public static PropertyAnimationFloat GenerateElement(string elementName, Guid id, List<string> tags, bool isFirstGenerated,
|
||||
GameElement animatedObject, string componentName, string propertyName, FlexibleFloat propertyValue)
|
||||
{
|
||||
PropertyAnimationFloat animation = Instantiate(GameManager.Instance.basePrefabs.emptyObject)
|
||||
.AddComponent<PropertyAnimationFloat>();
|
||||
|
||||
animation.Initialize(elementName, id, tags, isFirstGenerated, animatedObject);
|
||||
|
||||
animation.animatedObject = animatedObject;
|
||||
|
||||
animation.componentName = componentName;
|
||||
animation.propertyName = propertyName;
|
||||
animation.propertyValue = propertyValue;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public override void AfterInitialize()
|
||||
{
|
||||
if (animatedObject == null || string.IsNullOrEmpty(componentName)) return;
|
||||
|
||||
Type componentType = GetTypeFromAllAssemblies(componentName);
|
||||
if (componentType != null)
|
||||
{
|
||||
targetComponent = animatedObject.GetComponentInChildren(componentType);
|
||||
}
|
||||
|
||||
if (targetComponent != null)
|
||||
{
|
||||
InitializeReflection();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[PropertyAnimationFloat] Cannot find Component '{componentName}' strictly on '{animatedObject.name}'.");
|
||||
}
|
||||
|
||||
base.AfterInitialize();
|
||||
}
|
||||
|
||||
private Type GetTypeFromAllAssemblies(string typeName)
|
||||
{
|
||||
// 对于跨程序集的搜索
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
Type type = assembly.GetType(typeName);
|
||||
if (type != null) return type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void InitializeReflection()
|
||||
{
|
||||
Type type = targetComponent.GetType();
|
||||
|
||||
// 1. 尝试寻找 Property
|
||||
targetProperty = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (targetProperty != null && targetProperty.CanWrite)
|
||||
{
|
||||
MethodInfo setMethod = targetProperty.GetSetMethod(nonPublic: true);
|
||||
if (setMethod != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
floatSetterDelegate = (Action<float>)Delegate.CreateDelegate(typeof(Action<float>), targetComponent, setMethod);
|
||||
return; // 建立快速委托成功!
|
||||
}
|
||||
catch { /* 回落 */ }
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 尝试寻找 Field (直接包含公有/私有)
|
||||
targetField = type.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (targetField == null && targetProperty == null)
|
||||
{
|
||||
Debug.LogWarning($"[PropertyAnimationFloat] Cannot find target Property or Field '{propertyName}' strictly on '{componentName}'.");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region [核心动画逻辑] Core Animation Logic
|
||||
protected override void UpdateAnimation(float songTime)
|
||||
{
|
||||
if (targetComponent == null || propertyValue == null || propertyValue.animations.Count == 0) return;
|
||||
|
||||
propertyValue.UpdateFlexibleFloat(songTime);
|
||||
float value = propertyValue.value;
|
||||
|
||||
if (floatSetterDelegate != null)
|
||||
{
|
||||
floatSetterDelegate(value);
|
||||
}
|
||||
else if (targetProperty != null)
|
||||
{
|
||||
targetProperty.SetValue(targetComponent, value);
|
||||
}
|
||||
else if (targetField != null)
|
||||
{
|
||||
targetField.SetValue(targetComponent, value);
|
||||
}
|
||||
|
||||
if (animatedObject is IHaveDirtyMarkSubmodule dirtyTarget)
|
||||
{
|
||||
dirtyTarget.dirtyMarkSubmodule.MarkDirty(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ApplyTimeOffset(float offset)
|
||||
{
|
||||
base.ApplyTimeOffset(offset);
|
||||
if (propertyValue != null && propertyValue.animations != null)
|
||||
{
|
||||
foreach(var a in propertyValue.animations)
|
||||
{
|
||||
a.startTime += offset;
|
||||
a.endTime += offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94ee23661f2295c4c96f77067b411969
|
||||
@@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Ichni.RhythmGame.Beatmap;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Ichni.RhythmGame
|
||||
{
|
||||
public class PropertyAnimationVector3 : AnimationBase
|
||||
{
|
||||
#region [暴露属性字段与关联] Exposed Fields & References
|
||||
public string componentName;
|
||||
public string propertyName;
|
||||
public FlexibleFloat propertyValueX, propertyValueY, propertyValueZ;
|
||||
|
||||
private Component targetComponent;
|
||||
private FieldInfo targetField;
|
||||
private PropertyInfo targetProperty;
|
||||
|
||||
// 高性能赋值委托
|
||||
private Action<Vector3> vectorSetterDelegate;
|
||||
#endregion
|
||||
|
||||
#region [生命周期与工厂] Lifecycle & Factory
|
||||
public static PropertyAnimationVector3 GenerateElement(string elementName, Guid id, List<string> tags, bool isFirstGenerated,
|
||||
GameElement animatedObject, string componentName, string propertyName,
|
||||
FlexibleFloat propertyValueX, FlexibleFloat propertyValueY, FlexibleFloat propertyValueZ)
|
||||
{
|
||||
PropertyAnimationVector3 animation = Instantiate(GameManager.Instance.basePrefabs.emptyObject)
|
||||
.AddComponent<PropertyAnimationVector3>();
|
||||
|
||||
animation.Initialize(elementName, id, tags, isFirstGenerated, animatedObject);
|
||||
|
||||
animation.animatedObject = animatedObject;
|
||||
|
||||
animation.componentName = componentName;
|
||||
animation.propertyName = propertyName;
|
||||
animation.propertyValueX = propertyValueX;
|
||||
animation.propertyValueY = propertyValueY;
|
||||
animation.propertyValueZ = propertyValueZ;
|
||||
|
||||
return animation;
|
||||
}
|
||||
|
||||
public override void AfterInitialize()
|
||||
{
|
||||
if (animatedObject == null || string.IsNullOrEmpty(componentName)) return;
|
||||
|
||||
Type componentType = GetTypeFromAllAssemblies(componentName);
|
||||
if (componentType != null)
|
||||
{
|
||||
targetComponent = animatedObject.GetComponentInChildren(componentType);
|
||||
}
|
||||
|
||||
if (targetComponent != null)
|
||||
{
|
||||
InitializeReflection();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[PropertyAnimationVector3] Cannot find Component '{componentName}' strictly on '{animatedObject.name}'.");
|
||||
}
|
||||
|
||||
base.AfterInitialize();
|
||||
}
|
||||
|
||||
private Type GetTypeFromAllAssemblies(string typeName)
|
||||
{
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
Type type = assembly.GetType(typeName);
|
||||
if (type != null) return type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void InitializeReflection()
|
||||
{
|
||||
Type type = targetComponent.GetType();
|
||||
|
||||
targetProperty = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (targetProperty != null && targetProperty.CanWrite)
|
||||
{
|
||||
MethodInfo setMethod = targetProperty.GetSetMethod(nonPublic: true);
|
||||
if (setMethod != null && targetProperty.PropertyType == typeof(Vector3))
|
||||
{
|
||||
try
|
||||
{
|
||||
vectorSetterDelegate = (Action<Vector3>)Delegate.CreateDelegate(typeof(Action<Vector3>), targetComponent, setMethod);
|
||||
return;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
targetField = type.GetField(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (targetField == null && targetProperty == null)
|
||||
{
|
||||
Debug.LogWarning($"[PropertyAnimationVector3] Cannot find target Property or Field '{propertyName}' strictly on '{componentName}'.");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region [核心动画逻辑] Core Animation Logic
|
||||
protected override void UpdateAnimation(float songTime)
|
||||
{
|
||||
if (targetComponent == null) return;
|
||||
|
||||
propertyValueX?.UpdateFlexibleFloat(songTime);
|
||||
propertyValueY?.UpdateFlexibleFloat(songTime);
|
||||
propertyValueZ?.UpdateFlexibleFloat(songTime);
|
||||
|
||||
float x = propertyValueX != null ? propertyValueX.value : 0f;
|
||||
float y = propertyValueY != null ? propertyValueY.value : 0f;
|
||||
float z = propertyValueZ != null ? propertyValueZ.value : 0f;
|
||||
|
||||
Vector3 targetVector = new Vector3(x, y, z);
|
||||
|
||||
if (vectorSetterDelegate != null)
|
||||
{
|
||||
vectorSetterDelegate(targetVector);
|
||||
}
|
||||
else if (targetProperty != null)
|
||||
{
|
||||
targetProperty.SetValue(targetComponent, targetVector);
|
||||
}
|
||||
else if (targetField != null)
|
||||
{
|
||||
targetField.SetValue(targetComponent, targetVector);
|
||||
}
|
||||
|
||||
if (animatedObject is IHaveDirtyMarkSubmodule dirtyTarget)
|
||||
{
|
||||
dirtyTarget.dirtyMarkSubmodule.MarkDirty(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ApplyTimeOffset(float offset)
|
||||
{
|
||||
base.ApplyTimeOffset(offset);
|
||||
void ApplyOffset(FlexibleFloat ff)
|
||||
{
|
||||
if (ff == null || ff.animations == null) return;
|
||||
foreach(var a in ff.animations) { a.startTime += offset; a.endTime += offset; }
|
||||
}
|
||||
ApplyOffset(propertyValueX);
|
||||
ApplyOffset(propertyValueY);
|
||||
ApplyOffset(propertyValueZ);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c2a33e8c63667d47967d086d0a0c2d8
|
||||
Reference in New Issue
Block a user