zoukankan      html  css  js  c++  java
  • Unity该插件NGUI得知(9)—— Tween并转换成世界坐标系的大小NGUI尺寸

    在游戏中,还有一种比较常见的动画,这是进球后产生,分数将被显示在游戏,而快速移动,使其失去位置加入。就打算使用NGUI的Tween来制作这样的分数动画效果。


    依据 Unity插件之NGUI学习(2),创建一个UI Root。然后使用NGUI创建一个Label和一个Button。


    在Project窗体中,在Resources/Prefabs目录中创建一个Prefab。该Prefab就是一个NGUI的Label,然后在菜单中选择NGUI->Tween->Rotation和NGUI->Tween->Position



    Play Style表示动画播放方式。

    Animation Curve动画速度曲线。点击后能够自己定义。

    Duration指定一次动画的时长。Start Delay进行延迟play。秒为单位。

    Tween Group表示渐变动画组ID。

    Ignore TimeScale是否忽略TimeScale。


    Tween Rotation

    FromTo,分别设置该GameObject開始和结束时在x,y,z上的旋转角度,如今我在To的z轴上设置了-720,表示该物体会在z轴上按顺时针旋转2圈。


    Tween Position

    FromTo,分别设置该GameObject開始和结束时在x,y,z上的坐标,该值为NGUI下的坐标系,这里暂且不设置To的坐标值,后面会在代码中进行设置。



    在Project窗体中创建一个TweenTest的脚本文件。代码例如以下:

    using UnityEngine;
    using System.Collections;


    public class TweenTest : MonoBehaviour {


    private GameObject prefab;
    private UIButton btn;
    private UILabel scoreLabel;
    private int score;
    private int add;
    private GameObject addscore;


    void Awake() {

    // 预先创建好经常使用的得分Prefab
    prefab = (GameObject)Resources.Load("Prefabs/AddScore");
    }


    // Use this for initialization
    void Start () {
    score = 1000;
    btn = GameObject.Find("Button").GetComponent<UIButton>();
    scoreLabel = GameObject.Find("Score").GetComponent<UILabel>();
    scoreLabel.text = "" + score;

    // 设置button响应函数
    EventDelegate.Add(btn.onClick, AddScore);
    }

    // Update is called once per frame
    void Update () {

    }


    void AddScore() {

    // 克隆得分GameObject
    addscore = (GameObject)Instantiate(prefab, new Vector3(0, 0, 0), transform.rotation);
    UILabel addlabel = addscore.GetComponent<UILabel>();
    System.Random random = new System.Random();

    // 随机得分数
    add = random.Next(50, 100);
    addlabel.text = "" + add;

    // 获取TweenPosition对象
    TweenPosition tweenP = addscore.GetComponent<TweenPosition>();

    // 设置To的坐标值,该值为NGUI的坐标系的值,所以须要逆转world坐标值transform.InverseTransformPoint
    tweenP.to = transform.InverseTransformPoint(scoreLabel.transform.position);
    Debug.Log(tweenP.to.ToString());

    // 设置动画播放结束后的回调函数
    EventDelegate.Add(tweenP.onFinished, ScoreMoveFinished);

    // 在Inspector窗体Tween Position选去掉了脚本名字那里的复选框,所以Tween Position不会运行,须要手动Play
    tweenP.Play();
    }


    void ScoreMoveFinished() {
    score += add;
    scoreLabel.text = "" + score;
    Destroy(addscore);
    }
    }

    当中关键的代码就是tweenP.to = transform.InverseTransformPoint(scoreLabel.transform.position);由于Tween Position的To,From的值是基于NGUI的坐标系,而我能眼下能取得的是scoreLabel.transform.position,它是世界坐标系的值,所以须要转换为NGUI坐标系的值。

    这正好是(8)中坐标系的逆转。


    然后将脚本加入到UI Root下,执行后。点击button就会在中间随机产生一个分数,然后会高速旋转并移动到总分的位置,并使总分累加。




    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    3年A班,从现在起大家都是人质-观后感
    深入浅出的Object.defineProperty()
    在Vue中使用插槽(solt)
    非父子组件间的传值
    给组件绑定原生事件
    组件参数校验与非props特性
    Vue父子组件的数据传递
    Vue组件使用中的细节点
    vue中set基本用法
    vue中的列表渲染
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4842225.html
Copyright © 2011-2022 走看看