码迷,mamicode.com
首页 > 其他好文 > 详细

NGUI Tween动画Scale与Transform冲突

时间:2016-05-03 12:32:51      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

NGUI中我们要同时完成Scale与Transform的效果,会发现动画并不是同我们想的那样运行的。

原因就是Tween Scale与Tween Transform的冲突调用。

Tween Scale中

技术分享

用来设置localScale

Tween Transform中

技术分享

也用来设置localScale。

这就产生冲突了。。。这里最简单的方法就是把两个脚本合并一下删掉Tween Transform中的localScale,因为还是得用Tween Scale来控制Scale。

using UnityEngine;
using System.Collections;

public class TweenScaleTransform : UITweener
{

    public Vector3 from = Vector3.one;
    public Vector3 to = Vector3.one;
    public bool updateTable = false;
    public Transform fromt;
    public Transform tot;
    public bool parentWhenFinished = false;

    Transform mTrans;
    Vector3 mPos;
    Quaternion mRot;
    Vector3 mScale;

    UITable mTable;

    public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }

    public Vector3 value { get { return cachedTransform.localScale; } set { cachedTransform.localScale = value; } }

    [System.Obsolete("Use ‘value‘ instead")]
    public Vector3 scale { get { return this.value; } set { this.value = value; } }

    /// <summary>
    /// Tween the value.
    /// </summary>

    protected override void OnUpdate(float factor, bool isFinished)
    {
        value = from * (1f - factor) + to * factor;

        if (updateTable)
        {
            if (mTable == null)
            {
                mTable = NGUITools.FindInParents<UITable>(gameObject);
                if (mTable == null) { updateTable = false; return; }
            }
            mTable.repositionNow = true;
        }

        if (tot != null)
        {
            if (mTrans == null)
            {
                mTrans = transform;
                mPos = mTrans.position;
                mRot = mTrans.rotation;
                mScale = mTrans.localScale;
            }

            if (fromt != null)
            {
                mTrans.position = fromt.position * (1f - factor) + tot.position * factor;
                mTrans.rotation = Quaternion.Slerp(fromt.rotation, tot.rotation, factor);
            }
            else
            {
                mTrans.position = mPos * (1f - factor) + tot.position * factor;
                mTrans.rotation = Quaternion.Slerp(mRot, tot.rotation, factor);
            }

            // Change the parent when finished, if requested
            if (parentWhenFinished && isFinished) mTrans.parent = tot;
        }
    }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenScaleTransform Begin(GameObject go, float duration, Vector3 scale, Transform from, Transform to)
    {
        TweenScaleTransform comp = UITweener.Begin<TweenScaleTransform>(go, duration);
        comp.from = comp.value;
        comp.to = scale;
        comp.fromt = from;
        comp.tot = to;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }

    [ContextMenu("Set ‘From‘ to current value")]
    public override void SetStartToCurrentValue() { from = value; }

    [ContextMenu("Set ‘To‘ to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of ‘From‘")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of ‘To‘")]
    void SetCurrentValueToEnd() { value = to; }

}

 

NGUI Tween动画Scale与Transform冲突

标签:

原文地址:http://www.cnblogs.com/SHOR/p/5454253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!