为了更方便地为UI视图添加动画,将动画的编辑功能封装在了UI View类中,可以通过编辑器快速的为视图编辑动画。动画分为两种类型,一种是Unity中的Animator动画,该类型直接通过一个字符串类型变量记录动画State状态的名称即可,播放时调用Animator类中的Play方法传入该名称。另一种是DoTween动画,支持视图的移动、旋转、缩放、淡入淡出动画的编辑:
首先看一下动画相关的几个类的数据结构:
usingSystem; usingUnityEngine; usingDG.Tweening; namespaceSK.Framework{ /// <summary>/// UI移动动画/// </summary> [Serializable] publicclassUIMoveAnimation { publicenumMoveMode { MoveIn, MoveOut } /// <summary>/// UI移动动画方向/// </summary>publicenumUIMoveAnimationDirection { Left, Right, Top, Bottom, TopLeft, TopRight, MiddleCenter, BottomLeft, BottomRight, } publicfloatduration=1f; publicfloatdelay; publicEaseease=Ease.Linear; publicUIMoveAnimationDirectiondirection=UIMoveAnimationDirection.Left; publicboolisCustom; publicVector3startValue; publicVector3endValue; publicMoveModemoveMode=MoveMode.MoveIn; publicTweenPlay(RectTransformtarget, boolinstant=false) { Vector3pos=Vector3.zero; floatxOffset=target.rect.width/2+target.rect.width*target.pivot.x; floatyOffset=target.rect.height/2+target.rect.height*target.pivot.y; switch (direction) { caseUIMoveAnimationDirection.Left: pos=newVector3(-xOffset, 0f, 0f); break; caseUIMoveAnimationDirection.Right: pos=newVector3(xOffset, 0f, 0f); break; caseUIMoveAnimationDirection.Top: pos=newVector3(0f, yOffset, 0f); break; caseUIMoveAnimationDirection.Bottom: pos=newVector3(0f, -yOffset, 0f); break; caseUIMoveAnimationDirection.TopLeft: pos=newVector3(-xOffset, yOffset, 0f); break; caseUIMoveAnimationDirection.TopRight: pos=newVector3(xOffset, yOffset, 0f); break; caseUIMoveAnimationDirection.MiddleCenter: pos=Vector3.zero; break; caseUIMoveAnimationDirection.BottomLeft: pos=newVector3(-xOffset, -yOffset, 0f); break; caseUIMoveAnimationDirection.BottomRight: pos=newVector3(xOffset, -yOffset, 0f); break; } switch (moveMode) { caseMoveMode.MoveIn: target.anchoredPosition3D=isCustom?startValue : pos; returntarget.DOAnchorPos3D(endValue, instant?0f : duration).SetDelay(instant?0f : delay).SetEase(ease); caseMoveMode.MoveOut: target.anchoredPosition3D=startValue; returntarget.DOAnchorPos3D(isCustom?endValue : pos, instant?0f : duration).SetDelay(instant?0f : delay).SetEase(ease); default: returnnull; } } } }
usingSystem; usingUnityEngine; usingDG.Tweening; namespaceSK.Framework{ /// <summary>/// UI旋转动画/// </summary> [Serializable] publicclassUIRotateAnimation { publicfloatduration=1f; publicfloatdelay; publicEaseease=Ease.Linear; publicVector3startValue; publicVector3endValue; publicRotateModerotateMode=RotateMode.Fast; publicboolisCustom; publicTweenPlay(RectTransformtarget, boolinstant=false) { if (isCustom) { target.localRotation=Quaternion.Euler(startValue); } returntarget.DORotate(endValue, instant?0f : duration, rotateMode).SetDelay(instant?0f : delay).SetEase(ease); } } }
usingSystem; usingUnityEngine; usingDG.Tweening; namespaceSK.Framework{ /// <summary>/// UI缩放动画/// </summary> [Serializable] publicclassUIScaleAnimation { publicfloatduration=1f; publicfloatdelay; publicEaseease=Ease.Linear; publicVector3startValue=Vector3.zero; publicVector3endValue=Vector3.one; publicboolisCustom; publicTweenPlay(RectTransformtarget, boolinstant=false) { if (isCustom) { target.localScale=startValue; } returntarget.DOScale(endValue, instant?0f : duration).SetDelay(instant?0f : delay).SetEase(ease); } } }
usingSystem; usingDG.Tweening; usingUnityEngine; namespaceSK.Framework{ /// <summary>/// UI淡入淡出动画/// </summary> [Serializable] publicclassUIFadeAnimation { publicfloatduration=1f; publicfloatdelay; publicEaseease=Ease.Linear; publicfloatstartValue; publicfloatendValue=1f; publicboolisCustom; publicTweenPlay(CanvasGrouptarget, boolinstant=false) { if (isCustom) { target.alpha=startValue; } returntarget.DOFade(endValue, instant?0f : duration).SetDelay(instant?0f : delay).SetEase(ease); } } }
namespaceSK.Framework{ /// <summary>/// UI动画类型/// </summary>publicenumUIAnimationType { /// <summary>/// DoTween动画/// </summary>Tween, /// <summary>/// Animator动画/// </summary>Animator, } }
usingSystem; usingUnityEngine; namespaceSK.Framework{ /// <summary>/// UI动画/// </summary> [Serializable] publicclassUIAnimation { publicUIAnimationTypeanimationType=UIAnimationType.Tween; publicstringstateName; publicboolmoveToggle; publicUIMoveAnimationmoveAnimation; publicboolrotateToggle; publicUIRotateAnimationrotateAnimation; publicboolscaleToggle; publicUIScaleAnimationscaleAnimation; publicboolfadeToggle; publicUIFadeAnimationfadeAnimation; publicboolHasTweenAnimation { get { returnmoveToggle||rotateToggle||scaleToggle||fadeToggle; } } publicIChainPlay(UIViewview, boolinstant=false, Actioncallback=null) { switch (animationType) { caseUIAnimationType.Tween: if (HasTweenAnimation) { varchain=view.Sequence(); varcc=newConcurrentChain(); if (moveToggle) cc.Tween(() =>moveAnimation.Play(view.RectTransform, instant)); if (rotateToggle) cc.Tween(() =>rotateAnimation.Play(view.RectTransform, instant)); if (scaleToggle) cc.Tween(() =>scaleAnimation.Play(view.RectTransform, instant)); if (fadeToggle) cc.Tween(() =>fadeAnimation.Play(view.CanvasGroup, instant)); chain.Append(cc).Event(() =>callback?.Invoke()).Begin(); returnchain; } else { callback?.Invoke(); returnnull; } caseUIAnimationType.Animator: returnview.Sequence() .Animation(view.GetComponent<Animator>(), stateName) .Event(() =>callback?.Invoke()) .Begin(); default: returnnull; } } } }
在UI View类中的相关变量如下:
usingSystem; usingUnityEngine.Events; namespaceSK.Framework{ [Serializable] publicclassViewVisibilityChangedEvent { publicUIAnimationanimation=newUIAnimation(); publicUnityEventonBeginEvent; publicUnityEventonEndEvent; } }
为UI View创建Custom Editor:
usingSystem; usingUnityEngine; usingUnityEditor; usingDG.Tweening; usingSystem.Reflection; usingUnityEditor.Animations; usingUnityEditor.AnimatedValues; namespaceSK.Framework{ [CustomEditor(typeof(UIView), true)] publicclassUIViewInspector : Editor { privateenumMenu { Animation, UnityEvent, } privateUIViewTarget; privateSerializedPropertyvariables; privateViewVisibilityChangedEventonShow; privateViewVisibilityChangedEventonHide; privatestaticstringuiViewOnShowFoldout="UIView OnShow Foldout"; privatestaticstringuiViewOnHideFoldout="UIView OnHide Foldout"; privateboolonShowFoldout; privateboolonHideFoldout; privateMenuonShowMenu=Menu.Animation; privateMenuonHideMenu=Menu.Animation; privateSerializedPropertyonShowBeginEvent; privateSerializedPropertyonShowEndEvent; privateSerializedPropertyonHideBeginEvent; privateSerializedPropertyonHideEndEvent; privateAnimBoolonShowMoveAnimBool; privateAnimBoolonShowRotateAnimBool; privateAnimBoolonShowScaleAnimBool; privateAnimBoolonShowFadeAnimBool; privateAnimBoolonHideMoveAnimBool; privateAnimBoolonHideRotateAnimBool; privateAnimBoolonHideScaleAnimBool; privateAnimBoolonHideFadeAnimBool; privateconstfloattitleWidth=80f; privateconstfloatlabelWidth=60f; publicoverridevoidOnInspectorGUI() { if (Target==null) { Target=targetasUIView; onShow=typeof(UIView).GetField("onVisible", BindingFlags.Instance|BindingFlags.NonPublic).GetValue(Target) asViewVisibilityChangedEvent; onHide=typeof(UIView).GetField("onInvisible", BindingFlags.Instance|BindingFlags.NonPublic).GetValue(Target) asViewVisibilityChangedEvent; variables=serializedObject.FindProperty("variables"); onShowFoldout=EditorPrefs.GetBool(uiViewOnShowFoldout); onHideFoldout=EditorPrefs.GetBool(uiViewOnHideFoldout); onShowBeginEvent=serializedObject.FindProperty("onVisible").FindPropertyRelative("onBeginEvent"); onShowEndEvent=serializedObject.FindProperty("onVisible").FindPropertyRelative("onEndEvent"); onHideBeginEvent=serializedObject.FindProperty("onInvisible").FindPropertyRelative("onBeginEvent"); onHideEndEvent=serializedObject.FindProperty("onInvisible").FindPropertyRelative("onEndEvent"); onShowMoveAnimBool=newAnimBool(onShow.animation.moveToggle, Repaint); onShowRotateAnimBool=newAnimBool(onShow.animation.rotateToggle, Repaint); onShowScaleAnimBool=newAnimBool(onShow.animation.scaleToggle, Repaint); onShowFadeAnimBool=newAnimBool(onShow.animation.fadeToggle, Repaint); onHideMoveAnimBool=newAnimBool(onHide.animation.moveToggle, Repaint); onHideRotateAnimBool=newAnimBool(onHide.animation.rotateToggle, Repaint); onHideScaleAnimBool=newAnimBool(onHide.animation.scaleToggle, Repaint); onHideFadeAnimBool=newAnimBool(onHide.animation.fadeToggle, Repaint); } EditorGUILayout.PropertyField(variables); //On Show 折叠栏varnewOnShowFoldout=EditorGUILayout.Foldout(onShowFoldout, "On Visible", true); if (newOnShowFoldout!=onShowFoldout) { onShowFoldout=newOnShowFoldout; EditorPrefs.SetBool(uiViewOnShowFoldout, onShowFoldout); } //Showif (onShowFoldout) { GUILayout.BeginHorizontal(); Colorcolor=GUI.color; GUI.color=onShowMenu==Menu.Animation?Color.cyan : color; if (GUILayout.Button("Animation", "ButtonLeft")) { onShowMenu=Menu.Animation; } GUI.color=onShowMenu==Menu.UnityEvent?Color.cyan : color; if (GUILayout.Button("Event", "ButtonRight")) { onShowMenu=Menu.UnityEvent; } GUI.color=color; GUILayout.EndHorizontal(); switch (onShowMenu) { caseMenu.Animation: //Animation TypeGUILayout.BeginHorizontal(); GUILayout.Label("Mode", GUILayout.Width(titleWidth)); varnewOnShowAnimationType= (UIAnimationType)EditorGUILayout.EnumPopup(onShow.animation.animationType); if (newOnShowAnimationType!=onShow.animation.animationType) { Undo.RecordObject(Target, "On Show Animation Type"); onShow.animation.animationType=newOnShowAnimationType; EditorUtility.SetDirty(Target); } GUILayout.EndHorizontal(); UIAnimationanimation=onShow.animation; switch (animation.animationType) { caseUIAnimationType.Tween: //Move、Rotate、Scale、FadeGUILayout.BeginHorizontal(); { GUI.color=animation.moveToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("MoveTool"), "ButtonLeft", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Show Animation Move Toggle"); animation.moveToggle=!animation.moveToggle; onShowMoveAnimBool.target=animation.moveToggle; EditorUtility.SetDirty(Target); } GUI.color=animation.rotateToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("RotateTool"), "ButtonMid", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Show Animation Rotate Toggle"); animation.rotateToggle=!animation.rotateToggle; onShowRotateAnimBool.target=animation.rotateToggle; EditorUtility.SetDirty(Target); } GUI.color=animation.scaleToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("ScaleTool"), "ButtonMid", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Show Animation Scale Toggle"); animation.scaleToggle=!animation.scaleToggle; onShowScaleAnimBool.target=animation.scaleToggle; EditorUtility.SetDirty(Target); } GUI.color=animation.fadeToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("ViewToolOrbit"), "ButtonRight", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Show Animation Fade Toggle"); animation.fadeToggle=!animation.fadeToggle; onShowFadeAnimBool.target=animation.fadeToggle; EditorUtility.SetDirty(Target); } GUI.color=color; } GUILayout.EndHorizontal(); //MoveAnimationvarmoveAnimation=animation.moveAnimation; if (EditorGUILayout.BeginFadeGroup(onShowMoveAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(40f); GUILayout.Label(EditorGUIUtility.IconContent("MoveTool")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(moveAnimation.duration); if (newDuration!=moveAnimation.duration) { Undo.RecordObject(Target, "On Show Animation Move Duration"); moveAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(moveAnimation.delay); if (newDelay!=moveAnimation.delay) { Undo.RecordObject(Target, "On Show Animation Move Delay"); moveAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(moveAnimation.isCustom?"Custom Position" : "Direction", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Direction"), !moveAnimation.isCustom, () => { moveAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Custom Position"), moveAnimation.isCustom, () => { moveAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); if (moveAnimation.isCustom) { Vector3newStartValue=EditorGUILayout.Vector3Field(GUIContent.none, moveAnimation.startValue); if (newStartValue!=moveAnimation.startValue) { Undo.RecordObject(Target, "On Show Animation Move From"); moveAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } else { varnewMoveDirection= (UIMoveAnimationDirection)EditorGUILayout.EnumPopup(moveAnimation.direction); if (newMoveDirection!=moveAnimation.direction) { Undo.RecordObject(Target, "On Show Animation Move Direction"); moveAnimation.direction=newMoveDirection; EditorUtility.SetDirty(Target); } } } GUILayout.EndHorizontal(); //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); Vector3newEndValue=EditorGUILayout.Vector3Field(GUIContent.none, moveAnimation.endValue); if (newEndValue!=moveAnimation.endValue) { Undo.RecordObject(Target, "On Show Animation Move To"); moveAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(moveAnimation.ease); if (newEase!=moveAnimation.ease) { Undo.RecordObject(Target, "On Show Animation Move Ease"); moveAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); //RotateAnimationvarrotateAnimation=animation.rotateAnimation; if (EditorGUILayout.BeginFadeGroup(onShowRotateAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(rotateAnimation.isCustom?50f : 40f); GUILayout.Label(EditorGUIUtility.IconContent("RotateTool")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(rotateAnimation.duration); if (newDuration!=rotateAnimation.duration) { Undo.RecordObject(Target, "On Show Animation Rotate Duration"); rotateAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(rotateAnimation.delay); if (newDelay!=rotateAnimation.delay) { Undo.RecordObject(Target, "On Show Animation Rotate Delay"); rotateAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(rotateAnimation.isCustom?"Fixed Rotation" : "Current Rotation", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Current Rotation"), !rotateAnimation.isCustom, () => { rotateAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Fixed Rotation"), rotateAnimation.isCustom, () => { rotateAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); if (rotateAnimation.isCustom) { //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); Vector3newStartValue=EditorGUILayout.Vector3Field(GUIContent.none, rotateAnimation.startValue); if (newStartValue!=rotateAnimation.startValue) { Undo.RecordObject(Target, "On Show Animation Rotate From"); rotateAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); Vector3newEndValue=EditorGUILayout.Vector3Field(GUIContent.none, rotateAnimation.endValue); if (newEndValue!=rotateAnimation.endValue) { Undo.RecordObject(Target, "On Show Animation Rotate To"); rotateAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Rotate ModeGUILayout.BeginHorizontal(); { GUILayout.Label("Mode", GUILayout.Width(labelWidth)); varnewRotateMode= (RotateMode)EditorGUILayout.EnumPopup(rotateAnimation.rotateMode); if (newRotateMode!=rotateAnimation.rotateMode) { Undo.RecordObject(Target, "On Show Animation Rotate Mode"); rotateAnimation.rotateMode=newRotateMode; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(rotateAnimation.ease); if (newEase!=rotateAnimation.ease) { Undo.RecordObject(Target, "On Show Animation Rotate Ease"); rotateAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); //ScaleAnimationvarscaleAnimation=animation.scaleAnimation; if (EditorGUILayout.BeginFadeGroup(onShowScaleAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(scaleAnimation.isCustom?40f : 30f); GUILayout.Label(EditorGUIUtility.IconContent("ScaleTool")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(scaleAnimation.duration); if (newDuration!=scaleAnimation.duration) { Undo.RecordObject(Target, "On Show Animation Scale Duration"); scaleAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(scaleAnimation.delay); if (newDelay!=scaleAnimation.delay) { Undo.RecordObject(Target, "On Show Animation Scale Delay"); scaleAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(scaleAnimation.isCustom?"Fixed Scale" : "Current Scale", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Current Scale"), !scaleAnimation.isCustom, () => { scaleAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Fixed Scale"), scaleAnimation.isCustom, () => { scaleAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); if (scaleAnimation.isCustom) { //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); Vector3newStartValue=EditorGUILayout.Vector3Field(GUIContent.none, scaleAnimation.startValue); if (newStartValue!=scaleAnimation.startValue) { Undo.RecordObject(Target, "On Show Animation Scale From"); scaleAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); Vector3newEndValue=EditorGUILayout.Vector3Field(GUIContent.none, scaleAnimation.endValue); if (newEndValue!=scaleAnimation.endValue) { Undo.RecordObject(Target, "On Show Animation Scale To"); scaleAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(scaleAnimation.ease); if (newEase!=scaleAnimation.ease) { Undo.RecordObject(Target, "On Show Animation Scale Ease"); scaleAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); //FadeAnimationvarfadeAnimation=animation.fadeAnimation; if (EditorGUILayout.BeginFadeGroup(onShowFadeAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(fadeAnimation.isCustom?40f : 30f); GUILayout.Label(EditorGUIUtility.IconContent("ViewToolOrbit")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(fadeAnimation.duration); if (newDuration!=fadeAnimation.duration) { Undo.RecordObject(Target, "On Show Animation Fade Duration"); fadeAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(fadeAnimation.delay); if (newDelay!=fadeAnimation.delay) { Undo.RecordObject(Target, "On Show Animation Fade Delay"); fadeAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(fadeAnimation.isCustom?"Fixed Alpha" : "Current Alpha", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Current Alpha"), !fadeAnimation.isCustom, () => { fadeAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Fixed Alpha"), fadeAnimation.isCustom, () => { fadeAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); if (fadeAnimation.isCustom) { //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); floatnewStartValue=EditorGUILayout.FloatField(GUIContent.none, fadeAnimation.startValue); if (newStartValue!=fadeAnimation.startValue) { Undo.RecordObject(Target, "On Show Animation Fade From"); fadeAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); floatnewEndValue=EditorGUILayout.FloatField(GUIContent.none, fadeAnimation.endValue); if (newEndValue!=fadeAnimation.endValue) { Undo.RecordObject(Target, "On Show Animation Fade To"); fadeAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(fadeAnimation.ease); if (newEase!=fadeAnimation.ease) { Undo.RecordObject(Target, "On Show Animation Fade Ease"); fadeAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); break; caseUIAnimationType.Animator: varanimator=Target.GetComponent<Animator>(); if (animator!=null) { varanimatorController=animator.runtimeAnimatorControllerasAnimatorController; varstateMachine=animatorController.layers[0].stateMachine; if (stateMachine.states.Length==0) { EditorGUILayout.HelpBox("no animator state was found.", MessageType.Info); } else { string[] stateNames=newstring[stateMachine.states.Length]; for (inti=0; i<stateNames.Length; i++) { stateNames[i] =stateMachine.states[i].state.name; } varindex=Array.FindIndex(stateNames, m=>m==animation.stateName); GUILayout.BeginHorizontal(); GUILayout.Label("State Name", GUILayout.Width(titleWidth)); varnewIndex=EditorGUILayout.Popup(index, stateNames); if (newIndex!=index) { Undo.RecordObject(Target, "Show Animation State Name"); animation.stateName=stateNames[newIndex]; EditorUtility.SetDirty(Target); } GUILayout.EndHorizontal(); } } else { EditorGUILayout.HelpBox("no animator component on this view.", MessageType.Error); } break; default: break; } break; caseMenu.UnityEvent: //OnBeginEvent、OnEndEventEditorGUILayout.PropertyField(onShowBeginEvent); EditorGUILayout.PropertyField(onShowEndEvent); break; default: break; } } //On Hide 折叠栏varnewOnHideFoldout=EditorGUILayout.Foldout(onHideFoldout, "On Invisible", true); if (newOnHideFoldout!=onHideFoldout) { onHideFoldout=newOnHideFoldout; EditorPrefs.SetBool(uiViewOnHideFoldout, onHideFoldout); } //Hideif (onHideFoldout) { GUILayout.BeginHorizontal(); Colorcolor=GUI.color; GUI.color=onHideMenu==Menu.Animation?Color.cyan : color; if (GUILayout.Button("Animation", "ButtonLeft")) { onHideMenu=Menu.Animation; } GUI.color=onHideMenu==Menu.UnityEvent?Color.cyan : color; if (GUILayout.Button("Event", "ButtonRight")) { onHideMenu=Menu.UnityEvent; } GUI.color=color; GUILayout.EndHorizontal(); switch (onHideMenu) { caseMenu.Animation: //Animation TypeGUILayout.BeginHorizontal(); GUILayout.Label("Mode", GUILayout.Width(titleWidth)); varnewOnHideAnimationType= (UIAnimationType)EditorGUILayout.EnumPopup(onHide.animation.animationType); if (newOnHideAnimationType!=onHide.animation.animationType) { Undo.RecordObject(Target, "On Hide Animation Type"); onHide.animation.animationType=newOnHideAnimationType; } GUILayout.EndHorizontal(); UIAnimationanimation=onHide.animation; switch (animation.animationType) { caseUIAnimationType.Tween: //Move、Rotate、Scale、FadeGUILayout.BeginHorizontal(); { GUI.color=animation.moveToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("MoveTool"), "ButtonLeft", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Hide Animation Move Toggle"); animation.moveToggle=!animation.moveToggle; onHideMoveAnimBool.target=animation.moveToggle; EditorUtility.SetDirty(Target); } GUI.color=animation.rotateToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("RotateTool"), "ButtonMid", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Hide Animation Rotate Toggle"); animation.rotateToggle=!animation.rotateToggle; onHideRotateAnimBool.target=animation.rotateToggle; EditorUtility.SetDirty(Target); } GUI.color=animation.scaleToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("ScaleTool"), "ButtonMid", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Hide Animation Scale Toggle"); animation.scaleToggle=!animation.scaleToggle; onHideScaleAnimBool.target=animation.scaleToggle; EditorUtility.SetDirty(Target); } GUI.color=animation.fadeToggle?color : Color.gray; if (GUILayout.Button(EditorGUIUtility.IconContent("ViewToolOrbit"), "ButtonRight", GUILayout.Width(25f))) { Undo.RecordObject(Target, "On Hide Animation Fade Toggle"); animation.fadeToggle=!animation.fadeToggle; onHideFadeAnimBool.target=animation.fadeToggle; EditorUtility.SetDirty(Target); } GUI.color=color; } GUILayout.EndHorizontal(); //MoveAnimationvarmoveAnimation=animation.moveAnimation; if (EditorGUILayout.BeginFadeGroup(onHideMoveAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(40f); GUILayout.Label(EditorGUIUtility.IconContent("MoveTool")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(moveAnimation.duration); if (newDuration!=moveAnimation.duration) { Undo.RecordObject(Target, "On Hide Animation Move Duration"); moveAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(moveAnimation.delay); if (newDelay!=moveAnimation.delay) { Undo.RecordObject(Target, "On Hide Animation Move Delay"); moveAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //FromGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth)); Vector3newStartValue=EditorGUILayout.Vector3Field(GUIContent.none, moveAnimation.startValue); if (newStartValue!=moveAnimation.startValue) { Undo.RecordObject(Target, "On Hide Animation Move From"); moveAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(moveAnimation.isCustom?"Custom Position" : "Direction", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Direction"), !moveAnimation.isCustom, () => { moveAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Custom Position"), moveAnimation.isCustom, () => { moveAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); //ToGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); if (moveAnimation.isCustom) { Vector3newEndValue=EditorGUILayout.Vector3Field(GUIContent.none, moveAnimation.endValue); if (newEndValue!=moveAnimation.endValue) { Undo.RecordObject(Target, "On Hide Animation Move End"); moveAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } else { varnewMoveDirection= (UIMoveAnimationDirection)EditorGUILayout.EnumPopup(moveAnimation.direction); if (newMoveDirection!=moveAnimation.direction) { Undo.RecordObject(Target, "On Hide Animation Move Direction"); moveAnimation.direction=newMoveDirection; EditorUtility.SetDirty(Target); } } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(moveAnimation.ease); if (newEase!=moveAnimation.ease) { Undo.RecordObject(Target, "On Hide Animation Move Ease"); moveAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); //RotateAnimationvarrotateAnimation=animation.rotateAnimation; if (EditorGUILayout.BeginFadeGroup(onHideRotateAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(rotateAnimation.isCustom?50f : 40f); GUILayout.Label(EditorGUIUtility.IconContent("RotateTool")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(rotateAnimation.duration); if (newDuration!=rotateAnimation.duration) { Undo.RecordObject(Target, "On Hide Animation Rotate Duration"); rotateAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(rotateAnimation.delay); if (newDelay!=rotateAnimation.delay) { Undo.RecordObject(Target, "On Hide Animation Rotate Delay"); rotateAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(rotateAnimation.isCustom?"Fixed Rotation" : "Current Rotation", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Current Rotation"), !rotateAnimation.isCustom, () => { rotateAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Fixed Rotation"), rotateAnimation.isCustom, () => { rotateAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); if (rotateAnimation.isCustom) { //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); Vector3newStartValue=EditorGUILayout.Vector3Field(GUIContent.none, rotateAnimation.startValue); if (newStartValue!=rotateAnimation.startValue) { Undo.RecordObject(Target, "On Hide Animation Rotate From"); rotateAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); Vector3newEndValue=EditorGUILayout.Vector3Field(GUIContent.none, rotateAnimation.endValue); if (newEndValue!=rotateAnimation.endValue) { Undo.RecordObject(Target, "On Hide Animation Rotate To"); rotateAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Rotate ModeGUILayout.BeginHorizontal(); { GUILayout.Label("Mode", GUILayout.Width(labelWidth)); varnewRotateMode= (RotateMode)EditorGUILayout.EnumPopup(rotateAnimation.rotateMode); if (newRotateMode!=rotateAnimation.rotateMode) { Undo.RecordObject(Target, "On Hide Animation Rotate Mode"); rotateAnimation.rotateMode=newRotateMode; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(rotateAnimation.ease); if (newEase!=rotateAnimation.ease) { Undo.RecordObject(Target, "On Hide Animation Rotate Ease"); rotateAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); //ScaleAnimationvarscaleAnimation=animation.scaleAnimation; if (EditorGUILayout.BeginFadeGroup(onHideScaleAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(scaleAnimation.isCustom?40f : 30f); GUILayout.Label(EditorGUIUtility.IconContent("ScaleTool")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(scaleAnimation.duration); if (newDuration!=scaleAnimation.duration) { Undo.RecordObject(Target, "On Hide Animation Scale Duration"); scaleAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(scaleAnimation.delay); if (newDelay!=scaleAnimation.delay) { Undo.RecordObject(Target, "On Hide Animation Scale Delay"); scaleAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(scaleAnimation.isCustom?"Fixed Scale" : "Current Scale", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Current Scale"), !scaleAnimation.isCustom, () => { scaleAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Fixed Scale"), scaleAnimation.isCustom, () => { scaleAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); if (scaleAnimation.isCustom) { //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); Vector3newStartValue=EditorGUILayout.Vector3Field(GUIContent.none, scaleAnimation.startValue); if (newStartValue!=scaleAnimation.startValue) { Undo.RecordObject(Target, "On Hide Animation Scale From"); scaleAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); Vector3newEndValue=EditorGUILayout.Vector3Field(GUIContent.none, scaleAnimation.endValue); if (newEndValue!=scaleAnimation.endValue) { Undo.RecordObject(Target, "On Hide Animation Scale To"); scaleAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(scaleAnimation.ease); if (newEase!=scaleAnimation.ease) { Undo.RecordObject(Target, "On Hide Animation Scale Ease"); scaleAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); //FadeAnimationvarfadeAnimation=animation.fadeAnimation; if (EditorGUILayout.BeginFadeGroup(onHideFadeAnimBool.faded)) { GUILayout.BeginHorizontal("Badge"); { GUILayout.BeginVertical(); { GUILayout.Space(fadeAnimation.isCustom?40f : 30f); GUILayout.Label(EditorGUIUtility.IconContent("ViewToolOrbit")); } GUILayout.EndVertical(); GUILayout.BeginVertical(); { //Duration、DelayGUILayout.BeginHorizontal(); { GUILayout.Label("Duration", GUILayout.Width(labelWidth)); varnewDuration=EditorGUILayout.FloatField(fadeAnimation.duration); if (newDuration!=fadeAnimation.duration) { Undo.RecordObject(Target, "On Hide Animation Fade Duration"); fadeAnimation.duration=newDuration; EditorUtility.SetDirty(Target); } GUILayout.Label("Delay", GUILayout.Width(labelWidth-20f)); varnewDelay=EditorGUILayout.FloatField(fadeAnimation.delay); if (newDelay!=fadeAnimation.delay) { Undo.RecordObject(Target, "On Hide Animation Fade Delay"); fadeAnimation.delay=newDelay; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //Is CustomGUILayout.BeginHorizontal(); { GUILayout.Label("From", GUILayout.Width(labelWidth-2f)); if (GUILayout.Button(fadeAnimation.isCustom?"Fixed Alpha" : "Current Alpha", "DropDownButton")) { GenericMenugm=newGenericMenu(); gm.AddItem(newGUIContent("Current Alpha"), !fadeAnimation.isCustom, () => { fadeAnimation.isCustom=false; EditorUtility.SetDirty(Target); }); gm.AddItem(newGUIContent("Fixed Alpha"), fadeAnimation.isCustom, () => { fadeAnimation.isCustom=true; EditorUtility.SetDirty(Target); }); gm.ShowAsContext(); } } GUILayout.EndHorizontal(); if (fadeAnimation.isCustom) { //FromGUILayout.BeginHorizontal(); { GUILayout.Label(GUIContent.none, GUILayout.Width(labelWidth)); floatnewStartValue=EditorGUILayout.FloatField(GUIContent.none, fadeAnimation.startValue); if (newStartValue!=fadeAnimation.startValue) { Undo.RecordObject(Target, "On Hide Animation Fade From"); fadeAnimation.startValue=newStartValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } //ToGUILayout.BeginHorizontal(); { GUILayout.Label("To", GUILayout.Width(labelWidth)); floatnewEndValue=EditorGUILayout.FloatField(GUIContent.none, fadeAnimation.endValue); if (newEndValue!=fadeAnimation.endValue) { Undo.RecordObject(Target, "On Hide Animation Fade To"); fadeAnimation.endValue=newEndValue; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); //EaseGUILayout.BeginHorizontal(); { GUILayout.Label("Ease", GUILayout.Width(labelWidth)); varnewEase= (Ease)EditorGUILayout.EnumPopup(fadeAnimation.ease); if (newEase!=fadeAnimation.ease) { Undo.RecordObject(Target, "On Hide Animation Fade Ease"); fadeAnimation.ease=newEase; EditorUtility.SetDirty(Target); } } GUILayout.EndHorizontal(); } GUILayout.EndVertical(); } GUILayout.EndHorizontal(); } EditorGUILayout.EndFadeGroup(); break; caseUIAnimationType.Animator: varanimator=Target.GetComponent<Animator>(); if (animator!=null) { varanimatorController=animator.runtimeAnimatorControllerasAnimatorController; varstateMachine=animatorController.layers[0].stateMachine; if (stateMachine.states.Length==0) { EditorGUILayout.HelpBox("no animator state was found.", MessageType.Info); } else { string[] stateNames=newstring[stateMachine.states.Length]; for (inti=0; i<stateNames.Length; i++) { stateNames[i] =stateMachine.states[i].state.name; } varindex=Array.FindIndex(stateNames, m=>m==animation.stateName); GUILayout.BeginHorizontal(); GUILayout.Label("State Name", GUILayout.Width(titleWidth)); varnewIndex=EditorGUILayout.Popup(index, stateNames); if (newIndex!=index) { Undo.RecordObject(Target, "Show Animation State Name"); animation.stateName=stateNames[newIndex]; EditorUtility.SetDirty(Target); } GUILayout.EndHorizontal(); } } else { EditorGUILayout.HelpBox("no animator component on this view.", MessageType.Error); } break; default: break; } break; caseMenu.UnityEvent: //OnBeginEvent、OnEndEventEditorGUILayout.PropertyField(onHideBeginEvent); EditorGUILayout.PropertyField(onHideEndEvent); break; default: break; } } serializedObject.ApplyModifiedProperties(); } } }