【Unity NGUI遊戲開發之二】TweenPosition位移動畫(一):不相對於Anchor的位移動畫

來源:互聯網
上載者:User

標籤:ngui   tweenposition   位移動畫   tween   unity   

下面學些下NGUI的TweenPosition位移動畫,一般的位移動畫需求分為不想對於Anchor錨點的位移動畫和相對於Anchor的位移動畫(主要看是否考慮螢幕解析度),下面介紹兩種遊戲中最常用的不相對於Anchor的TweenPosition用法:

用法1.NGUI的控制項從PosA位置移動到PosB位置,播放動畫

用法2.在遊戲中需要動態建立帶有TweenPosition組件動畫的對象,對象建立、移動、到達指定位置、銷毀的過程。eg.遊戲中玩家吃金幣,遲到金幣後轉換為分數,分數播放一個TweenPosition組件動畫,從玩家位置移動到分數標籤位置,到達分數後,銷毀。


用法1.NGUI的控制項從PosA位置移動到PosB位置,播放動畫


這種用法最簡單,設定連個位置,播放動畫即可



如下:


可以把控制項隱藏,在程式中開啟動畫:

TweenPosition tp = tweenObject.GetComponent<TweenPosition>();tp.PlayForward();


用法2.在遊戲中需要動態建立帶有TweenPosition組件動畫的對象,對象建立、移動、到達指定位置、銷毀的過程。eg.遊戲中玩家吃金幣,遲到金幣後轉換為分數,分數播放一個TweenPosition組件動畫,從玩家位置移動到分數標籤位置,到達分數後,銷毀。


如下:



需要注意幾點:

1.要動態產生的分數對象,需要做成預製體,放到Resources/Prefabs目錄下,Scene情境中沒有備份

2.TweenPositon不要開啟,在程式中來控制播放動畫的時機



建立一個PlayerEatScore.cs,綁定按鈕上或UIRoot上都可以,代碼如下:

using UnityEngine;using System;using System.Collections;public class PlayerEatCoin : MonoBehaviour {private GameObject addScorePrefab; //Add score prefabprivate UILabel totalScoreLabel;  // 總分數Labelprivate UIButton btn;private int totalScore;   // 總分數private int score;   // 添加的分數void Awake() {// 預先建立好常用的得分PrefabaddScorePrefab = Resources.Load("Prefabs/AddScore") as GameObject;}void Start () {totalScore = 10000;totalScoreLabel = GameObject.Find("TotalScore").GetComponent<UILabel>();totalScoreLabel.text = totalScore.ToString();btn = GameObject.Find("BtnPlayerEatScore").GetComponent<UIButton>();// 設定按鈕響應函數EventDelegate.Add(btn.onClick, AddScore);}void AddScore() {// 複製得分GameObjectGameObject addScoreClone = (GameObject)Instantiate(addScorePrefab, new Vector3(0, 0, 0), transform.rotation);UILabel label = addScoreClone.GetComponent<UILabel>();// 隨機得分數score = UnityEngine.Random.Range(100, 300);label.text = score.ToString();// 擷取TweenPosition對象TweenPosition tp = addScoreClone.GetComponent<TweenPosition>();// 設定To的座標值,該值為NGUI的座標系的值,所以需要逆轉world座標值transform.InverseTransformPointtp.to = transform.InverseTransformPoint(totalScoreLabel.transform.position);Debug.Log(tp.to.ToString());// 設定動畫播放結束後的回呼函數EventDelegate.Add(tp.onFinished, ()=> {ScoreMoveFinished(addScoreClone, score);});// 在Inspector視窗Tween Position勾選去掉了指令碼名字那裡的複選框,所以Tween Position不會執行,需要手動Playtp.PlayForward();}void ScoreMoveFinished(GameObject label, int sc) {Debug.Log ("-----callback score:" + sc);totalScore += sc;totalScoreLabel.text = totalScore.ToString();// 使用傳參 和TweenPosition.current的功能一樣if (label != TweenPosition.current.gameObject) {Debug.Log("Param object not equal TweenPosition.current!");}// 銷毀播放動畫的對象Destroy(TweenPosition.current.gameObject);//Destroy(label.gameObject);}}


上面的代碼有幾點需要注意:

1.預製體動態產生,預製體的目錄位置

2.TweenPositon動畫的回調傳參數可以用一個lambda函數來解決

<span style="white-space:pre"></span>EventDelegate.Add(tp.onFinished, ()=> {ScoreMoveFinished(addScoreClone, score);});

3.回調中的TweenPosition.current對象就是當前回調的TweenPosition對象,不通過參數傳遞也可以,這是新NGUI的通常用法。

<span style="white-space:pre"></span>void ScoreMoveFinished(GameObject label, int sc) {// 使用傳參 和TweenPosition.current的功能一樣if (label != TweenPosition.current.gameObject) {Debug.Log("Param object not equal TweenPosition.current!");}// 銷毀播放動畫的對象Destroy(TweenPosition.current.gameObject);//Destroy(label.gameObject);}




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

【Unity NGUI遊戲開發之二】TweenPosition位移動畫(一):不相對於Anchor的位移動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.