2
2
using System . Collections . Generic ;
3
3
using UnityEngine ;
4
4
5
+ [ Flags ]
6
+ public enum LogLevel
7
+ {
8
+ None = 1 ,
9
+ Error = 2 ,
10
+ Warning = 4 ,
11
+ Info = 8 ,
12
+ All = Error | Warning | Info
13
+ }
14
+
5
15
namespace zFrame . Event
6
16
{
7
17
public static class EventHandler
8
18
{
9
19
/// <summary>动画机及其事件信息Pairs</summary>
10
20
private static List < EventInfo > eventContainer = new List < EventInfo > ( ) ;
21
+ public static LogLevel LogLevels = LogLevel . All ;
11
22
12
23
private const string func = "AnimatorEventCallBack" ;
13
24
@@ -17,12 +28,12 @@ public static class EventHandler
17
28
/// <param name="animator">动画机</param>
18
29
/// <param name="clipName">动画片段名称</param>
19
30
/// <param name="frame">指定帧</param>
20
- public static EventInfo GenerAnimationInfo ( Animator animator , string clipName )
31
+ public static EventInfo GetAnimationInfo ( Animator animator , string clipName , bool createIfNotFound = true )
21
32
{
22
33
AnimationClip clip = GetAnimationClip ( animator , clipName ) ;
23
34
if ( clip )
24
35
{
25
- return GetEventInfo ( animator , clip ) ; //获取指定事件信息类
36
+ return GetEventInfo ( animator , clip , createIfNotFound ) ; //获取指定事件信息类
26
37
}
27
38
else
28
39
{
@@ -39,16 +50,22 @@ public static void GenerateAnimationEvent(EventInfo eventInfo, int frame)
39
50
{
40
51
if ( frame < 0 || frame > eventInfo . totalFrames )
41
52
{
42
- Debug . LogError ( $ "AnimatorEventSystem[紧急]:【{ eventInfo . animator . name } 】所在的动画机【{ eventInfo . animationClip . name } 】片段帧数设置错误【{ frame } 】!") ;
53
+ if ( LogLevels . HasFlag ( LogLevel . Error ) )
54
+ {
55
+ Debug . LogError ( $ "AnimatorEventSystem[紧急]:【{ eventInfo . animator . name } 】所在的动画机【{ eventInfo . animationClip . name } 】片段帧数设置错误【{ frame } 】!") ;
56
+ }
43
57
return ;
44
58
}
45
59
float _time = frame / eventInfo . animationClip . frameRate ;
46
60
AnimationEvent [ ] events = eventInfo . animationClip . events ;
47
- AnimationEvent varEvent = Array . Find ( events , v => Mathf . Approximately ( v . time , _time ) ) ;
61
+ AnimationEvent varEvent = Array . Find ( events , v => Mathf . Approximately ( v . time , _time ) ) ;
48
62
if ( null != varEvent )
49
63
{
50
64
if ( varEvent . functionName == func ) return ;
51
- Debug . Log ( $ "AnimatorEventSystem[一般]:【{ eventInfo . animator . name } 】所在的动画机【{ eventInfo . animationClip . name } 】片段【{ frame } 】帧已存在回调方法【{ varEvent . functionName } 】,将自动覆盖!") ;
65
+ if ( LogLevels . HasFlag ( LogLevel . Info ) )
66
+ {
67
+ Debug . Log ( $ "AnimatorEventSystem[一般]:【{ eventInfo . animator . name } 】所在的动画机【{ eventInfo . animationClip . name } 】片段【{ frame } 】帧已存在回调方法【{ varEvent . functionName } 】,将自动覆盖!") ;
68
+ }
52
69
}
53
70
varEvent = new ( )
54
71
{
@@ -58,7 +75,10 @@ public static void GenerateAnimationEvent(EventInfo eventInfo, int frame)
58
75
} ;
59
76
eventInfo . animationClip . AddEvent ( varEvent ) ; //绑定事件
60
77
eventInfo . animator . Rebind ( ) ; //重新绑定动画器的所有动画的属性和网格数据。
61
- Debug . Log ( $ "{ nameof ( EventHandler ) } :完成 AnimationEvent 添加, 建议优先在编辑器下就把事件安插OK以避免动画机的重新绑定,see more ↓ \n Clip Name = { eventInfo . animationClip . name } , frame = { frame } , time = { _time } ,Function Name = { func } ") ;
78
+ if ( LogLevels . HasFlag ( LogLevel . Info ) )
79
+ {
80
+ Debug . Log ( $ "{ nameof ( EventHandler ) } :完成 AnimationEvent 添加, 建议优先在编辑器下就把事件安插OK以避免动画机的重新绑定,see more ↓ \n Clip Name = { eventInfo . animationClip . name } , frame = { frame } , time = { _time } ,Function Name = { func } ") ;
81
+ }
62
82
}
63
83
64
84
/// <summary>数据重置,用于总管理类清理数据用</summary>
@@ -78,10 +98,10 @@ public static void Clear()
78
98
/// <param name="animator">动画机</param>
79
99
/// <param name="clip">动画片段</param>
80
100
/// <returns>事件信息类</returns>
81
- private static EventInfo GetEventInfo ( Animator animator , AnimationClip clip )
101
+ private static EventInfo GetEventInfo ( Animator animator , AnimationClip clip , bool createIfNotFound = true )
82
102
{
83
103
EventInfo a_EventInfo = eventContainer . Find ( v => v . animator == animator && v . animationClip == clip ) ;
84
- if ( null == a_EventInfo )
104
+ if ( null == a_EventInfo && createIfNotFound )
85
105
{
86
106
a_EventInfo = new EventInfo ( animator , clip ) ;
87
107
eventContainer . Add ( a_EventInfo ) ;
@@ -100,25 +120,33 @@ public static AnimationClip GetAnimationClip(Animator animator, string name)
100
120
#region 异常提示
101
121
if ( null == animator )
102
122
{
103
- Debug . LogError ( "AnimatorEventSystem[紧急]:指定Animator不得为空!" ) ;
123
+ if ( LogLevels . HasFlag ( LogLevel . Error ) )
124
+ {
125
+ Debug . LogError ( "AnimatorEventSystem[紧急]:指定Animator不得为空!" ) ;
126
+ }
104
127
return null ;
105
128
}
106
129
RuntimeAnimatorController runtimeAnimatorController = animator . runtimeAnimatorController ;
107
130
if ( null == runtimeAnimatorController )
108
131
{
109
- Debug . LogError ( "AnimatorEventSystem[紧急]:指定【" + animator . name + "】Animator未挂载Controller!" ) ;
132
+ if ( LogLevels . HasFlag ( LogLevel . Error ) )
133
+ {
134
+ Debug . LogError ( "AnimatorEventSystem[紧急]:指定【" + animator . name + "】Animator未挂载Controller!" ) ;
135
+ }
110
136
return null ;
111
137
}
112
138
AnimationClip [ ] clips = runtimeAnimatorController . animationClips ;
113
139
AnimationClip [ ] varclip = Array . FindAll ( clips , v => v . name == name ) ;
114
140
if ( null == varclip || varclip . Length == 0 )
115
141
{
116
- Debug . LogError ( "AnimatorEventSystem[紧急]:指定【" + animator . name + "】Animator不存在名为【" + name + "】的动画片段!" ) ;
117
- return null ;
142
+ throw new InvalidOperationException ( "AnimatorEventSystem[紧急]:指定【" + animator . name + "】Animator不存在名为【" + name + "】的动画片段!" ) ;
118
143
}
119
144
if ( varclip . Length >= 2 )
120
145
{
121
- Debug . LogWarningFormat ( "AnimatorEventSystem[一般]:指定【{0}】Animator存在【{1}】个名为【{2}】的动画片段!\n 建议:若非复用导致的重名,请务必修正!否则,事件将绑定在找的第一个Clip上。" , animator . name , varclip . Length , name ) ;
146
+ if ( LogLevels . HasFlag ( LogLevel . Warning ) )
147
+ {
148
+ Debug . LogWarning ( $ "AnimatorEventSystem[一般]:指定【{ animator . name } 】Animator存在【{ varclip . Length } 】个名为【{ name } 】的动画片段!\n 建议:若非复用导致的重名,请务必修正!否则,事件将绑定在找的第一个Clip上。") ;
149
+ }
122
150
}
123
151
#endregion
124
152
return varclip [ 0 ] ;
@@ -136,14 +164,13 @@ public static List<Action<AnimationEvent>> GetAction(Animator animator, Animatio
136
164
EventInfo a_EventInfo = eventContainer . Find ( v => v . animator == animator && v . animationClip == clip ) ;
137
165
if ( null != a_EventInfo )
138
166
{
139
- if ( a_EventInfo . frameCallBackPairs . ContainsKey ( frame ) )
140
- {
141
- actions = new ( a_EventInfo . frameCallBackPairs [ frame ] ) ;
142
- a_EventInfo . frameCallBackPairs [ frame ] = new ( ) ; // just one shot
143
- }
144
- else
167
+ if ( ! a_EventInfo . frameCallBackPairs . TryGetValue ( frame , out actions ) )
145
168
{
146
- Debug . LogWarning ( $ "{ nameof ( EventHandler ) } : Key [frame = { frame } ] dose not exsit! \n { animator . name } - { clip . name } ") ;
169
+ actions = new ( ) ;
170
+ if ( LogLevels . HasFlag ( LogLevel . Warning ) )
171
+ {
172
+ Debug . LogWarning ( $ "{ nameof ( EventHandler ) } : Key [frame = { frame } ] dose not exsit! \n { animator . name } - { clip . name } ") ;
173
+ }
147
174
}
148
175
}
149
176
return actions ;
0 commit comments