Skip to content

Commit d5d3ce9

Browse files
committed
refactor: MilHandleFunction
1 parent 26c863d commit d5d3ce9

9 files changed

+88
-52
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ var animation =
6464
Text.Milease(nameof(Text.text), "Start!", "Finish!", 1f)
6565
)
6666
.Then(
67-
Text.Milease((o, p) =>
67+
Text.Milease((e) =>
6868
{
69-
var text = o as TMP_Text;
70-
text.text = $"Hide after {((1f - p) * 2f):F1}s...";
71-
}, null,2f, 0f, EaseFunction.Linear)
69+
var text = e.GetTarget<TMP_Text>();
70+
text.text = $"Hide after {((1f - e.Progress) * 2f):F1}s...";
71+
}, null, 2f, 0f, EaseFunction.Linear)
7272
)
7373
.Then(
7474
Text.gameObject.Milease(HandleFunction.Hide, HandleFunction.AutoActiveReset(Text.gameObject), 0f)

Scripts/Milease/Core/Animation/MilAnimation.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Milease.Core.Animation
88
{
9-
public delegate void MileaseHandleFunction(object target, float progress);
109
public class MilAnimation : ScriptableObject
1110
{
1211
public enum BlendingMode
@@ -107,7 +106,7 @@ public static AnimationPart SimplePart(object startValue, object toValue, float
107106
};
108107
}
109108

110-
internal static AnimationPart SimplePart(MileaseHandleFunction handleFunction, MileaseHandleFunction resetFunction, float duration, float delay = 0f,
109+
internal static AnimationPart SimplePart(float duration, float delay = 0f,
111110
EaseFunction easeFunction = EaseFunction.Quad, EaseType easeType = EaseType.In, BlendingMode blendingMode = BlendingMode.Default)
112111
{
113112
return new AnimationPart()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Milease.Core.Animator;
2+
3+
namespace Milease.Core.Animation
4+
{
5+
public struct MilHandleFunctionArgs
6+
{
7+
internal object target;
8+
public float Progress { get; internal set; }
9+
public RuntimeAnimationPart Animation { get; internal set; }
10+
public MilInstantAnimator Animator { get; internal set; }
11+
12+
public T GetTarget<T>()
13+
{
14+
return (T)target;
15+
}
16+
}
17+
public delegate void MileaseHandleFunction(MilHandleFunctionArgs e);
18+
}

Scripts/Milease/Core/Animation/MilHandleFunctionArgs.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Milease/Core/RuntimeAnimationPart.cs

+20-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Reflection;
44
using System.Runtime.CompilerServices;
55
using Milease.Core.Animation;
6+
using Milease.Core.Animator;
67
using Milease.Enums;
78
using Milease.Milease.Exception;
89
using UnityEngine;
@@ -34,19 +35,21 @@ public enum AnimationResetMode
3435
public object StartValue, ToValue, OriginalValue;
3536
public readonly object Target;
3637
public readonly MilAnimation.AnimationPart Source;
38+
public readonly MilInstantAnimator ParentAnimator;
3739
public bool IsPrepared { get; private set; }
3840
public string MemberPath { get; private set; }
3941

4042
private float lastProgress = -1f;
4143

42-
public RuntimeAnimationPart(object target, MilAnimation.AnimationPart animation, MileaseHandleFunction handleFunction, MileaseHandleFunction resetFunction = null)
44+
public RuntimeAnimationPart(object target, MilInstantAnimator animator, MilAnimation.AnimationPart animation, MileaseHandleFunction handleFunction, MileaseHandleFunction resetFunction = null)
4345
{
4446
HandleFunction = handleFunction;
4547
Source = animation;
4648
Target = target;
4749
Valid = true;
4850
ResetFunction = resetFunction;
4951
ValueType = ValueTypeEnum.SelfHandle;
52+
ParentAnimator = animator;
5053
MemberPath = handleFunction.GetHashCode().ToString();
5154
}
5255

@@ -71,7 +74,13 @@ public bool Reset(AnimationResetMode resetMode)
7174

7275
if (ResetFunction != null)
7376
{
74-
ResetFunction(Target, 0f);
77+
ResetFunction(new MilHandleFunctionArgs()
78+
{
79+
Animation = this,
80+
target = Target,
81+
Progress = 0f,
82+
Animator = ParentAnimator
83+
});
7584
return true;
7685
}
7786

@@ -118,14 +127,15 @@ public bool Reset(AnimationResetMode resetMode)
118127
}
119128

120129
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121-
public RuntimeAnimationPart(object target, MilAnimation.AnimationPart animation, Type baseType, MemberInfo memberInfo = null)
130+
public RuntimeAnimationPart(object target, MilInstantAnimator animator, MilAnimation.AnimationPart animation, Type baseType, MemberInfo memberInfo = null)
122131
{
123132
if (target == null)
124133
{
125134
throw new MilTargetNotFoundException();
126135
}
127136

128137
Source = animation;
138+
ParentAnimator = animator;
129139

130140
var curType = baseType;
131141
if (memberInfo != null)
@@ -259,7 +269,13 @@ public static void SetValue(RuntimeAnimationPart ani, float pro)
259269

260270
if (ani.ValueType == ValueTypeEnum.SelfHandle)
261271
{
262-
ani.HandleFunction(ani.Target, pro);
272+
ani.HandleFunction(new MilHandleFunctionArgs()
273+
{
274+
Animation = ani,
275+
target = ani.Target,
276+
Progress = pro,
277+
Animator = ani.ParentAnimator
278+
});
263279
return;
264280
}
265281

Scripts/Milease/Extension/MilInstantAnimatorExtension.cs

+22-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Milease.Utils
1717
public static class MilInstantAnimatorExtension
1818
{
1919
public static MilInstantAnimator AsMileaseKeyEvent(this Action action, float delay = 0f)
20-
=> Milease(action, (_, _) => action.Invoke(), null, 0f, delay);
20+
=> Milease(action, (_) => action.Invoke(), null, 0f, delay);
2121

2222
public static MilInstantAnimator AsMileaseHandleFunction(this MileaseHandleFunction func, float duration, float delay = 0f)
2323
=> Milease(func, func, null, duration, delay);
@@ -32,37 +32,37 @@ public static MilInstantAnimator Milease(this object target, MileaseHandleFuncti
3232
EaseFunction easeFunction = EaseFunction.Quad, EaseType easeType = EaseType.In,
3333
MilAnimation.BlendingMode blendingMode = MilAnimation.BlendingMode.Default)
3434
{
35-
var animation = new MilInstantAnimator();
36-
var ani = MilAnimation.SimplePart(handleFunction, resetFunction, duration, delay, easeFunction, easeType, blendingMode);
37-
animation.Collection.Add(new List<RuntimeAnimationPart>()
35+
var animator = new MilInstantAnimator();
36+
var ani = MilAnimation.SimplePart(duration, delay, easeFunction, easeType, blendingMode);
37+
animator.Collection.Add(new List<RuntimeAnimationPart>()
3838
{
39-
new (target, ani, handleFunction, resetFunction)
39+
new (target, animator, ani, handleFunction, resetFunction)
4040
});
41-
return animation;
41+
return animator;
4242
}
4343

4444
public static MilInstantAnimator MileaseTo(this object target, string memberName, object toValue,
4545
float duration, float delay = 0f, EaseFunction easeFunction = EaseFunction.Quad,
4646
EaseType easeType = EaseType.In)
4747
{
48-
var animation = new MilInstantAnimator();
48+
var animator = new MilInstantAnimator();
4949
var type = target.GetType();
5050
var members = type.GetMember(memberName);
5151
if (members.Length == 0)
5252
{
5353
throw new MilMemberNotFoundException(memberName);
5454
}
5555
var info = members[0];
56-
animation.Collection.Add(new List<RuntimeAnimationPart>()
56+
animator.Collection.Add(new List<RuntimeAnimationPart>()
5757
{
58-
new (target, MilAnimation.SimplePartTo(toValue, duration, delay, easeFunction, easeType), info.MemberType switch
58+
new (target, animator, MilAnimation.SimplePartTo(toValue, duration, delay, easeFunction, easeType), info.MemberType switch
5959
{
6060
MemberTypes.Field => ((FieldInfo)info).FieldType,
6161
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
6262
_ => null
6363
}, info)
6464
});
65-
return animation;
65+
return animator;
6666
}
6767

6868
public static MilInstantAnimator MileaseAdditive(this object target, string memberName, object startValue,
@@ -74,24 +74,24 @@ public static MilInstantAnimator Milease(this object target, string memberName,
7474
float delay = 0f, EaseFunction easeFunction = EaseFunction.Quad, EaseType easeType = EaseType.In,
7575
MilAnimation.BlendingMode blendingMode = MilAnimation.BlendingMode.Default)
7676
{
77-
var animation = new MilInstantAnimator();
77+
var animator = new MilInstantAnimator();
7878
var type = target.GetType();
7979
var members = type.GetMember(memberName);
8080
if (members.Length == 0)
8181
{
8282
throw new MilMemberNotFoundException(memberName);
8383
}
8484
var info = members[0];
85-
animation.Collection.Add(new List<RuntimeAnimationPart>()
85+
animator.Collection.Add(new List<RuntimeAnimationPart>()
8686
{
87-
new (target, MilAnimation.SimplePart(startValue, delay, easeFunction, easeType, blendingMode), info.MemberType switch
87+
new (target, animator, MilAnimation.SimplePart(startValue, delay, easeFunction, easeType, blendingMode), info.MemberType switch
8888
{
8989
MemberTypes.Field => ((FieldInfo)info).FieldType,
9090
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
9191
_ => null
9292
}, info)
9393
});
94-
return animation;
94+
return animator;
9595
}
9696

9797
public static MilInstantAnimator MileaseAdditive(this object target, string memberName, object startValue,
@@ -104,47 +104,47 @@ public static MilInstantAnimator Milease(this object target, string memberName,
104104
object toValue, float duration, float delay = 0f, EaseFunction easeFunction = EaseFunction.Quad,
105105
EaseType easeType = EaseType.In, MilAnimation.BlendingMode blendingMode = MilAnimation.BlendingMode.Default)
106106
{
107-
var animation = new MilInstantAnimator();
107+
var animator = new MilInstantAnimator();
108108
var type = target.GetType();
109109
var members = type.GetMember(memberName);
110110
if (members.Length == 0)
111111
{
112112
throw new MilMemberNotFoundException(memberName);
113113
}
114114
var info = members[0];
115-
animation.Collection.Add(new List<RuntimeAnimationPart>()
115+
animator.Collection.Add(new List<RuntimeAnimationPart>()
116116
{
117-
new (target, MilAnimation.SimplePart(startValue, toValue, duration, delay, easeFunction, easeType, blendingMode), info.MemberType switch
117+
new (target, animator, MilAnimation.SimplePart(startValue, toValue, duration, delay, easeFunction, easeType, blendingMode), info.MemberType switch
118118
{
119119
MemberTypes.Field => ((FieldInfo)info).FieldType,
120120
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
121121
_ => null
122122
}, info)
123123
});
124-
return animation;
124+
return animator;
125125
}
126126

127127
public static MilInstantAnimator Milease(this object target, string memberName, params MilAnimation.AnimationPart[] animations)
128128
{
129-
var animation = new MilInstantAnimator();
129+
var animator = new MilInstantAnimator();
130130
var type = target.GetType();
131131
var members = type.GetMember(memberName);
132132
if (members.Length == 0)
133133
{
134134
throw new MilMemberNotFoundException(memberName);
135135
}
136136
var info = members[0];
137-
animation.Collection.Add(
137+
animator.Collection.Add(
138138
animations.Select(x =>
139-
new RuntimeAnimationPart(target, x, info.MemberType switch
139+
new RuntimeAnimationPart(target, animator, x, info.MemberType switch
140140
{
141141
MemberTypes.Field => ((FieldInfo)info).FieldType,
142142
MemberTypes.Property => ((PropertyInfo)info).PropertyType,
143143
_ => null
144144
}, info)
145145
).ToList()
146146
);
147-
return animation;
147+
return animator;
148148
}
149149
}
150150
}

Scripts/Milease/Utils/HandleFunction.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ namespace Milease.Utils
66
{
77
public static class HandleFunction
88
{
9-
public static void Hide(object o, float t)
9+
public static void Hide(MilHandleFunctionArgs e)
1010
{
11-
(o as GameObject)!.SetActive(t < 1f);
11+
e.GetTarget<GameObject>().SetActive(e.Progress < 1f);
1212
}
13-
public static void Show(object o, float t)
13+
public static void Show(MilHandleFunctionArgs e)
1414
{
15-
(o as GameObject)!.SetActive(t >= 1f);
15+
e.GetTarget<GameObject>().SetActive(e.Progress >= 1f);
1616
}
17-
public static void DeativeWhenReset(object o, float t)
17+
public static void DeativeWhenReset(MilHandleFunctionArgs e)
1818
{
19-
(o as GameObject)!.SetActive(false);
19+
e.GetTarget<GameObject>().SetActive(false);
2020
}
21-
public static void ActiveWhenReset(object o, float t)
21+
public static void ActiveWhenReset(MilHandleFunctionArgs e)
2222
{
23-
(o as GameObject)!.SetActive(true);
23+
e.GetTarget<GameObject>().SetActive(true);
2424
}
2525

2626
public static MileaseHandleFunction AutoActiveReset(GameObject go)

~Document/1. Instant Animator.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ animator.Reset(mode);
108108
Implement the following delegate to create custom animations:
109109

110110
```c#
111-
delegate void MileaseHandleFunction(object target, float progress);
111+
delegate void MileaseHandleFunction(MilHandleFunctionArgs e);
112112
```
113113

114-
`target` is the object being animated, and `progress` is the eased progress of the animation, ranging approximately from [0.0, 1.0].
114+
Call `e.GetTarget<T>()` to get the object being animated, and `e.Progress` is the eased progress of the animation, ranging approximately from [0.0, 1.0].
115115

116116
To create a custom animation with `Milease`:
117117

@@ -152,11 +152,11 @@ var animation =
152152
Text.Milease(nameof(Text.text), "Start!", "Finish!", 1f)
153153
)
154154
.Then(
155-
Text.Milease((o, p) =>
155+
Text.Milease((e) =>
156156
{
157-
var text = o as TMP_Text;
158-
text.text = $"Hide after {((1f - p) * 2f):F1}s...";
159-
}, null,2f, 0f, EaseFunction.Linear)
157+
var text = e.GetTarget<TMP_Text>();
158+
text.text = $"Hide after {((1f - e.Progress) * 2f):F1}s...";
159+
}, null, 2f, 0f, EaseFunction.Linear)
160160
)
161161
.Then(
162162
Text.gameObject.Milease(HandleFunction.Hide, HandleFunction.AutoActiveReset(Text.gameObject), 0f)

~Document/1. 即时动画机.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ animator.Reset(mode);
108108
通过实现以下委托来实现:
109109

110110
```c#
111-
delegate void MileaseHandleFunction(object target, float progress);
111+
delegate void MileaseHandleFunction(MilHandleFunctionArgs e);
112112
```
113113

114-
`target` 为目标物体,`progress` 为动画进度(经过缓动处理后的进行),范围大约是[0.0, 1.0]
114+
调用`e.GetTarget<T>()` 获取目标物体,`e.Progress` 为动画进度(经过缓动处理后的进行),范围大约是[0.0, 1.0]
115115

116116
使用`Milease`创建自定义动画:
117117

@@ -152,11 +152,11 @@ var animation =
152152
Text.Milease(nameof(Text.text), "Start!", "Finish!", 1f)
153153
)
154154
.Then(
155-
Text.Milease((o, p) =>
155+
Text.Milease((e) =>
156156
{
157-
var text = o as TMP_Text;
158-
text.text = $"Hide after {((1f - p) * 2f):F1}s...";
159-
}, null,2f, 0f, EaseFunction.Linear)
157+
var text = e.GetTarget<TMP_Text>();
158+
text.text = $"Hide after {((1f - e.Progress) * 2f):F1}s...";
159+
}, null, 2f, 0f, EaseFunction.Linear)
160160
)
161161
.Then(
162162
Text.gameObject.Milease(HandleFunction.Hide, HandleFunction.AutoActiveReset(Text.gameObject), 0f)

0 commit comments

Comments
 (0)