Skip to content

Commit 94a0217

Browse files
committed
剔除 OnStart ,没有存在的道理
简化 示例,感觉 oneshot 与否是不是应该变成可选项
1 parent f5c3a81 commit 94a0217

File tree

12 files changed

+145
-135
lines changed

12 files changed

+145
-135
lines changed

Assets/MecanimEventSystem/Editor/com.zframework.events.editor.asmdef

Lines changed: 0 additions & 18 deletions
This file was deleted.

Assets/MecanimEventSystem/Editor/com.zframework.events.editor.asmdef.meta

Lines changed: 0 additions & 7 deletions
This file was deleted.

Assets/MecanimEventSystem/Example/AnimClips/EventListener.cs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,39 @@ namespace zFrame.Event.Example
44
public class EventListener : MonoBehaviour
55
{
66
Animator animator;
7-
bool ifFire = true;
87
private void Start()
98
{
109
animator = GameObject.Find("Cube").GetComponent<Animator>();
1110
animator.SetTarget("Left", 55).OnProcess((v) =>
1211
{
1312
string clipname = v.animatorClipInfo.clip.name; //无差别触发下面的逻辑,因为没有对(Layer)层进行层别,如果这个AnimationClip在这个动画机中被多次复用,请务必层别层信息(如下面示例)
14-
if (!ifFire) return;
1513
Debug.Log("55帧到了:" + clipname + ":" + v.time * v.animatorClipInfo.clip.frameRate + "搞事情!");
16-
Debug.Log("55帧到了:参数:" + v.stringParameter + ":" + v.objectReferenceParameter.name);
17-
});
18-
19-
animator.SetTarget("Left").OnStart((v) =>
20-
{
21-
if (!ifFire) return;
22-
string clipname = v.animatorClipInfo.clip.name;
23-
Debug.Log("第一帧到了:" + clipname + ":" + v.time * v.animatorClipInfo.clip.frameRate + v.stringParameter);
2414
});
2515
}
2616
private void OnGUI()
2717
{
28-
if (GUILayout.Button("Rotate动态添加回调"))
18+
if (GUILayout.Button("Rotate动态添加回调 (oneshot)"))
2919
{
3020
animator.SetTarget("Rotate").OnCompleted((v) =>
3121
{
32-
if (!ifFire) return;
3322
string clipname = v.animatorClipInfo.clip.name;
3423
if (v.animatorStateInfo.IsName("Base Layer.Rotate"))//演示Base Layer的事件接受 ---因为AnimationClip在这个动画机中被多次复用,层别Layer信息是非常必要的(区别于上面的示例)
35-
{
24+
{
3625
Debug.Log("结束时Base Layer:" + clipname + ":" + v.time * v.animatorClipInfo.clip.frameRate);
3726
}
27+
3828
if (v.animatorStateInfo.IsName("New Layer.Rotate1212"))//演示其它层的事件接受
39-
{
29+
{
4030
Debug.Log("结束时New Layer:" + clipname + ":" + v.time * v.animatorClipInfo.clip.frameRate);
4131
}
4232
});
4333
}
44-
if (GUILayout.Button("Rotate动态叠加回调"))
45-
{
46-
animator.SetTarget("Rotate").OnCompleted((v) =>
47-
{
48-
if (!ifFire) return;
49-
string clipname = v.animatorClipInfo.clip.name;
50-
if (v.animatorStateInfo.IsName("Base Layer.Rotate"))//演示Base Layer的事件接受
51-
{
52-
Debug.Log("结束时Base Layer:" + clipname + ":" + v.time * v.animatorClipInfo.clip.frameRate + "又搞一次!");
53-
}
54-
if (v.animatorStateInfo.IsName("New Layer.Rotate1212"))//演示其它层的事件接受
55-
{
56-
Debug.Log("结束时New Layer:" + clipname + ":" + v.time * v.animatorClipInfo.clip.frameRate + "又搞一次!");
57-
}
58-
});
59-
}
6034

61-
if (GUILayout.Button("触发baseLayer.Left"))
35+
if (GUILayout.Button("触发baseLayer.Left (oneshot)"))
6236
{
6337
animator.SetTrigger("Left");
6438
}
6539

66-
if (GUILayout.Button("触发baseLayer.Rotate,基于step1、2"))
67-
{
68-
animator.SetTrigger("Rotate");
69-
}
70-
71-
if (GUILayout.Button(ifFire ? "暂停所有事件响应" : "开始所有事件响应"))
72-
{
73-
ifFire = !ifFire;
74-
}
7540
}
7641
}
7742
}

Assets/MecanimEventSystem/Example/TestForAwait/Collapse.anim

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,4 @@ AnimationClip:
166166
m_EulerEditorCurves: []
167167
m_HasGenericRootTransform: 1
168168
m_HasMotionFloatCurves: 0
169-
m_Events:
170-
- time: 2
171-
functionName: AnimatorEventCallBack
172-
data:
173-
objectReferenceParameter: {fileID: 0}
174-
floatParameter: 0
175-
intParameter: 0
176-
messageOptions: 0
169+
m_Events: []

Assets/MecanimEventSystem/Example/TestForAwait/Expand.anim

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,4 @@ AnimationClip:
166166
m_EulerEditorCurves: []
167167
m_HasGenericRootTransform: 1
168168
m_HasMotionFloatCurves: 0
169-
m_Events:
170-
- time: 2
171-
functionName: AnimatorEventCallBack
172-
data:
173-
objectReferenceParameter: {fileID: 0}
174-
floatParameter: 0
175-
intParameter: 0
176-
messageOptions: 0
169+
m_Events: []

Assets/MecanimEventSystem/Example/TestForAwait/Test.cs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ public class Test : MonoBehaviour
88
public Animator animator;
99
public Button button;
1010

11-
public Text text;
11+
public Text text, text2;
12+
public int delay = 5;
13+
float countcached = 0;
14+
private bool isCounting = false;
1215

1316
EventState callbackExp, callbackClps;
1417

@@ -17,32 +20,54 @@ void Start()
1720
{
1821
button.onClick.AddListener(OnButtonClicked);
1922
text.text = "expand";
23+
}
2024

21-
// 运行时为动画片段绑定事件, 提前绑定以避免Animator运行过程中被重新绑定
22-
// 发现 ReBind 会导致第二个 settarget 延迟到最后,此为异常
23-
// 解决方案是编辑器下提前安插事件
24-
callbackExp = animator.SetTarget("Expand");
25-
callbackClps = animator.SetTarget("Collapse");
25+
private void Update()
26+
{
27+
if (isCounting)
28+
{
29+
countcached += Time.deltaTime;
30+
text2.text = countcached.ToString("f0");
31+
}
2632
}
2733

2834
private async void OnButtonClicked()
2935
{
30-
button.interactable = false;
31-
Debug.Log($"{nameof(Test)}: Default is expand , Now start collapse!");
32-
33-
await callbackClps.SetBoolAsync("Expand",false);
34-
35-
Debug.Log($"{nameof(Test)}: collapse Completed !");
36-
text.text = "collapsed";
37-
38-
Debug.Log($"{nameof(Test)}: wait for 5 second!");
39-
await Task.Delay(5000);
40-
Debug.Log($"{nameof(Test)}: waiting finish!");
41-
42-
Debug.Log($"{nameof(Test)}: Now is collapse , expanding !");
43-
await callbackExp.SetBoolAsync("Expand", true);
44-
text.text = "expand";
45-
Debug.Log($"{nameof(Test)}: expand Completed!");
46-
button.interactable = true;
36+
// 为 2 个动画片的绑定事件
37+
{
38+
// SetTarget 务必提前统一操作
39+
// 原因是该操作会触发 Animator Rebind 并导致动画重置。
40+
callbackExp = animator.SetTarget("Expand");
41+
callbackClps = animator.SetTarget("Collapse");
42+
}
43+
44+
// 等待折叠
45+
{
46+
button.interactable = false;
47+
Debug.Log($"{nameof(Test)}: Default is expand , Now start collapse!");
48+
await callbackClps.SetBoolAsync("Expand", false);
49+
Debug.Log($"{nameof(Test)}: collapse Completed !");
50+
text.text = "collapsed";
51+
}
52+
53+
//做一个延迟
54+
{
55+
Debug.Log($"{nameof(Test)}: wait for 5 second!");
56+
isCounting = true;
57+
await Task.Delay(delay * 1000);
58+
isCounting = false;
59+
countcached = 0;
60+
text2.text = string.Empty;
61+
Debug.Log($"{nameof(Test)}: waiting finish!");
62+
}
63+
64+
// 等待展开
65+
{
66+
Debug.Log($"{nameof(Test)}: Now is collapse , expanding !");
67+
await callbackExp.SetBoolAsync("Expand", true);
68+
text.text = "expand";
69+
Debug.Log($"{nameof(Test)}: expand Completed!");
70+
button.interactable = true;
71+
}
4772
}
4873
}

Assets/MecanimEventSystem/Example/TestForAwait/TestForAwait.unity

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ MonoBehaviour:
325325
animator: {fileID: 230172597}
326326
button: {fileID: 579102279}
327327
text: {fileID: 455942728}
328+
text2: {fileID: 629659807}
329+
delay: 5
328330
--- !u!114 &230172603
329331
MonoBehaviour:
330332
m_ObjectHideFlags: 0
@@ -550,7 +552,7 @@ RectTransform:
550552
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
551553
m_AnchorMin: {x: 0.5, y: 0}
552554
m_AnchorMax: {x: 0.5, y: 0}
553-
m_AnchoredPosition: {x: 0, y: 15}
555+
m_AnchoredPosition: {x: 0, y: 27.7}
554556
m_SizeDelta: {x: 160, y: 50}
555557
m_Pivot: {x: 0.5, y: 0.5}
556558
--- !u!114 &455942728
@@ -580,7 +582,7 @@ MonoBehaviour:
580582
m_BestFit: 0
581583
m_MinSize: 3
582584
m_MaxSize: 40
583-
m_Alignment: 0
585+
m_Alignment: 4
584586
m_AlignByGeometry: 0
585587
m_RichText: 1
586588
m_HorizontalOverflow: 0
@@ -717,6 +719,86 @@ CanvasRenderer:
717719
m_PrefabAsset: {fileID: 0}
718720
m_GameObject: {fileID: 579102277}
719721
m_CullTransparentMesh: 1
722+
--- !u!1 &629659805
723+
GameObject:
724+
m_ObjectHideFlags: 0
725+
m_CorrespondingSourceObject: {fileID: 0}
726+
m_PrefabInstance: {fileID: 0}
727+
m_PrefabAsset: {fileID: 0}
728+
serializedVersion: 6
729+
m_Component:
730+
- component: {fileID: 629659806}
731+
- component: {fileID: 629659808}
732+
- component: {fileID: 629659807}
733+
m_Layer: 5
734+
m_Name: Text (Legacy) (1)
735+
m_TagString: Untagged
736+
m_Icon: {fileID: 0}
737+
m_NavMeshLayer: 0
738+
m_StaticEditorFlags: 0
739+
m_IsActive: 1
740+
--- !u!224 &629659806
741+
RectTransform:
742+
m_ObjectHideFlags: 0
743+
m_CorrespondingSourceObject: {fileID: 0}
744+
m_PrefabInstance: {fileID: 0}
745+
m_PrefabAsset: {fileID: 0}
746+
m_GameObject: {fileID: 629659805}
747+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
748+
m_LocalPosition: {x: 0, y: 0, z: 0}
749+
m_LocalScale: {x: 1, y: 1, z: 1}
750+
m_ConstrainProportionsScale: 0
751+
m_Children: []
752+
m_Father: {fileID: 1272215259}
753+
m_RootOrder: 2
754+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
755+
m_AnchorMin: {x: 0.5, y: 0}
756+
m_AnchorMax: {x: 0.5, y: 0}
757+
m_AnchoredPosition: {x: 0, y: 77.7}
758+
m_SizeDelta: {x: 160, y: 50}
759+
m_Pivot: {x: 0.5, y: 0.5}
760+
--- !u!114 &629659807
761+
MonoBehaviour:
762+
m_ObjectHideFlags: 0
763+
m_CorrespondingSourceObject: {fileID: 0}
764+
m_PrefabInstance: {fileID: 0}
765+
m_PrefabAsset: {fileID: 0}
766+
m_GameObject: {fileID: 629659805}
767+
m_Enabled: 1
768+
m_EditorHideFlags: 0
769+
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
770+
m_Name:
771+
m_EditorClassIdentifier:
772+
m_Material: {fileID: 0}
773+
m_Color: {r: 0, g: 0, b: 0, a: 1}
774+
m_RaycastTarget: 1
775+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
776+
m_Maskable: 1
777+
m_OnCullStateChanged:
778+
m_PersistentCalls:
779+
m_Calls: []
780+
m_FontData:
781+
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
782+
m_FontSize: 36
783+
m_FontStyle: 0
784+
m_BestFit: 0
785+
m_MinSize: 3
786+
m_MaxSize: 40
787+
m_Alignment: 4
788+
m_AlignByGeometry: 0
789+
m_RichText: 1
790+
m_HorizontalOverflow: 0
791+
m_VerticalOverflow: 0
792+
m_LineSpacing: 1
793+
m_Text:
794+
--- !u!222 &629659808
795+
CanvasRenderer:
796+
m_ObjectHideFlags: 0
797+
m_CorrespondingSourceObject: {fileID: 0}
798+
m_PrefabInstance: {fileID: 0}
799+
m_PrefabAsset: {fileID: 0}
800+
m_GameObject: {fileID: 629659805}
801+
m_CullTransparentMesh: 1
720802
--- !u!1 &1272215255
721803
GameObject:
722804
m_ObjectHideFlags: 0
@@ -811,6 +893,7 @@ RectTransform:
811893
m_Children:
812894
- {fileID: 579102278}
813895
- {fileID: 455942727}
896+
- {fileID: 629659806}
814897
m_Father: {fileID: 0}
815898
m_RootOrder: 3
816899
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Assets/MecanimEventSystem/Runtime/Async/MESCustomAwaiter.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,12 @@
33
using System.Threading;
44
using UnityEngine;
55
using zFrame.Event;
6-
public enum Event
7-
{
8-
OnStart,
9-
OnEnd
10-
}
11-
126
public static class MESCustomAwaiter
137
{
14-
public static AnimationAwaiter SetBoolAsync(this EventState state, string paramName, bool value, Event type = Event.OnEnd)
8+
public static AnimationAwaiter SetBoolAsync(this EventState state, string paramName, bool value)
159
{
1610
var awaiter = new AnimationAwaiter();
17-
switch (type)
18-
{
19-
case Event.OnStart:
20-
state.OnStart(v=>awaiter.Complete());
21-
break;
22-
case Event.OnEnd:
23-
state.OnCompleted(v=>awaiter.Complete());
24-
break;
25-
}
11+
state.OnCompleted(v => awaiter.Complete());
2612
state.SetBool(paramName, value);
2713
return awaiter;
2814
}

0 commit comments

Comments
 (0)