要让一个描点的控件动画移动到一个Position,能够用
TweenPosition。可是这个仅仅能用在Position是固定的情况下。并且不能依据分辨率适配来进行移动。
以NGUI自带的演示样例场景"Example 1 - Anchors"为例。如果要让"Left"移动到"Top Right",且无论随意分辨率都须要达到这个效果。这里的方法是改动TweenPosition代码,让其支持传入目标的Transform,而这个目标即是也被描点的控件。
打开
TweenPosition.cs,加入例如以下代码:
1
2 3 4 5 6 7 |
public Transform sourceFrom;
public Transform targetTo; static public TweenPosition Begin(GameObject go, float duration, Transform targetTo) { return Begin(go, duration, go.transform.parent.InverseTransformPoint(targetTo.position)); } |
改动
OnUpdate函数为例如以下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 |
protected
override
void OnUpdate (
float factor,
bool isFinished)
{ if (sourceFrom != null) { from = cachedTransform.parent.InverseTransformPoint(sourceFrom.position); sourceFrom = null; } if (targetTo != null) { to = cachedTransform.parent.InverseTransformPoint(targetTo.position); targetTo = null; } value = from * (1f - factor) + to * factor; } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class TweenPositionEditor : UITweenerEditor
{ public override void OnInspectorGUI () { GUILayout.Space(6f); NGUIEditorTools.SetLabelWidth(120f); TweenPosition tw = target as TweenPosition; GUI.changed = false; Vector3 from = EditorGUILayout.Vector3Field("From", tw.from); Transform sourceFrom = EditorGUILayout.ObjectField("Or From", tw.sourceFrom, typeof(Transform), true) as Transform; Vector3 to = EditorGUILayout.Vector3Field("To", tw.to); Transform targetTo = EditorGUILayout.ObjectField("Or To", tw.targetTo, typeof(Transform), true) as Transform; if (GUI.changed) { NGUIEditorTools.RegisterUndo("Tween Change", tw); tw.from = from; tw.to = to; tw.sourceFrom = sourceFrom; tw.targetTo = targetTo; NGUITools.SetDirty(tw); } DrawCommonProperties(); } } |
如今在检视器能够看到例如以下配置:
设置其"
Or From"和"
Or To",然后执行。就可以看到位移动画,例如以下所看到的:
平时能够使用
UIPlayTween来控制控件的动画播放。可是其仅支持点击、选中等等须要交互的动作才行,这里为其添加开启时就能够自己主动播放动画。打开
AnimationOrTween.cs,为枚举
Trigger添加一个枚举值,例如以下:
1
2 3 4 5 6 |
public enum Trigger
{ OnClick, …… OnEnableTrue, } |
打开
UIPlayTween.cs,在其
OnEnable函数最后。加入例如以下代码:
1
2 3 4 5 6 7 8 9 |
void OnEnable ()
{ …… if (trigger == Trigger.OnEnableTrue) { Play(true); } } |
这样
UIPlayTween就能在开启时,直接播放动画了。