Unity翻牌效果 翻卡效果 for NGUI

效果图如下:

111

 

思路:两个对象。默认选中角度一个旋转角度为0 。一个为90。点击旋转 角度为0的至90度。然后回调。让90度的另外一张旋转至0度。既实现翻牌效果。同时两张内部的显示类型不受相互影响

 

代码如下:

using UnityEngine;
using System.Collections;

public class TweenFlipCARDS : MonoBehaviour {

    public GameObject positive;
    public GameObject reverse;
    private TweenRotation mPositiveTween;
    private TweenRotation mReverseTween;
    /// <summary> 半圈时间 </summary>
    public float duration = 1;

    void Start()
    {
        mPositiveTween = positive.GetComponent<TweenRotation>();
        if (mPositiveTween == null)mPositiveTween = positive.AddComponent<TweenRotation>();

        mReverseTween = reverse.GetComponent<TweenRotation>();
        if (mReverseTween == null) mReverseTween = reverse.AddComponent<TweenRotation>();
        mPositiveTween.enabled = false;
        mReverseTween.enabled = false;
        reverse.gameObject.transform.localEulerAngles = new Vector3(0, 90, 0);

        mPositiveTween.from = Vector3.zero;
        mPositiveTween.to = new Vector3 (0, 90, 0);
        mPositiveTween.duration = duration;

        mReverseTween.from = new Vector3(0, 90, 0);
        mReverseTween.to = Vector3.zero;
        mReverseTween.duration = duration;

        UIEventListener listener = UIEventListener.Get(positive.gameObject);
        listener.onClick = ClickUIButton;
        listener = UIEventListener.Get(reverse.gameObject);
        listener.onClick = ClickUIButton;

        mPositiveTween.onFinished.Add(new EventDelegate(PositiveEventDelegate));
        mReverseTween.onFinished.Add(new EventDelegate(ReverseEventDelegate));

    }

    GameObject mNowDown;
    void ClickUIButton(GameObject click)
    {
        mNowDown = click;
        if(click == positive)
        {
            PlayPositive();
        }
        else if(click == reverse)
        {
            PlayReverse();
        }
    }

    /// <summary>
    /// 翻牌
    /// </summary>
    public void PlayPositive()
    {
        mPositiveTween.Play(true);
    }
    /// <summary>
    /// 复位
    /// </summary>
    public void PlayReverse()
    {
        mReverseTween.Play(false);
    }


    /// <summary>
    /// 翻牌回调
    /// </summary>
    void PositiveEventDelegate()
    {
        if (mNowDown == mPositiveTween.gameObject) mReverseTween.Play(true);
    }
    /// <summary>
    /// 复位回调
    /// </summary>
    void ReverseEventDelegate()
    {
        if (mNowDown == mReverseTween.gameObject) mPositiveTween.Play(false);
    }
}

直接将代码绑定在父物体上。下级两个GameObject即可。

本文链接:

https://bobsong.cn/486.html
1 + 7 =
快来做第一个评论的人吧~