本來針對個(gè)人應(yīng)用修改較多,請閱讀參考鏈接:孫廣東 http://blog.csdn.net/u010019717/article/details/51902837
簡介: 現(xiàn)如今很多的游戲都有幸運(yùn)轉(zhuǎn)輪活動(dòng),有的是特效按順序一次播放形成旋轉(zhuǎn)效果(如王者榮耀),有的是轉(zhuǎn)動(dòng)輪盤或者指針,這里講的是后一種(如圖)。 
1.場景設(shè)置 轉(zhuǎn)輪: -美術(shù)做好UI界面,確保幸運(yùn)輪的每個(gè)部分大小一致。 -在幸運(yùn)輪邊緣建立圓形的小碰撞器并將它們作為幸運(yùn)輪的子游戲物體。
箭頭: 給箭頭添加BoxCollider2D和HingeJoint2D組件,和下面的腳本。
注:Hinge Joint鉸鏈 官方文檔https://docs.unity3d.com/Manual/class-HingeJoint.html
2.編碼:
using UnityEngine; using System.Collections; using System.Collections.Generic; public class SpinWheel : MonoBehaviour { public List<AnimationCurve> animationCurves; //動(dòng)畫曲線列表 PRivate bool spinning; //是否在旋轉(zhuǎn)中 private float anglePerItem; //每個(gè)item角度(360/item個(gè)數(shù)) private int randomTime; //旋轉(zhuǎn)時(shí)間 private int itemNumber; //item個(gè)數(shù) private bool rotateCommand = false; //旋轉(zhuǎn)命令 private int targetItemIndex; //目標(biāo)item索引(從0開始) private bool CW = true; //是否順時(shí)針 private System.Action EndCallBack; //旋轉(zhuǎn)結(jié)束回調(diào) void Start(){ spinning = false; //避免沒有預(yù)設(shè)曲線報(bào)錯(cuò)(這里建一條先慢再快再慢的動(dòng)畫曲線) if (animationCurves== null) { Keyframe[] ks = new Keyframe[3]; ks[0] = new Keyframe(0, 0); ks[0].inTangent = 0; ks[0].outTangent = 0; ks[1] = new Keyframe(0.5f,0.5f); ks[1].inTangent = 1; ks[1].outTangent = 1; ks[2] = new Keyframe(1,1); ks[2].inTangent =0; ks[2].outTangent =0; AnimationCurve animationCurve = new AnimationCurve(ks); animationCurves.Add(animationCurve); } } /// <summary> /// 開啟旋轉(zhuǎn)調(diào)用(外部調(diào)用) /// </summary> /// <param name="itemNum">item總個(gè)數(shù)</param> /// <param name="itemIndex">目標(biāo)item索引,從0開始</param> /// <param name="cw">是否順時(shí)針</param> /// <param name="callback">結(jié)束回調(diào)</param> public void RotateUp(int itemNum, int itemIndex, bool cw, System.Action callback) { itemNumber = itemNum; anglePerItem = 360 / itemNumber; targetItemIndex = itemIndex; CW = cw; EndCallBack = callback; rotateCommand = true; } void Update () { if (rotateCommand && !spinning) { randomTime = Random.Range (6,8); //隨機(jī)獲取旋轉(zhuǎn)全角的次數(shù) float maxAngle = 360 * randomTime + (targetItemIndex * anglePerItem); //需要旋轉(zhuǎn)的角度 rotateCommand = false; StartCoroutine (SpinTheWheel (randomTime, maxAngle)); } } IEnumerator SpinTheWheel (float time, float maxAngle) { spinning = true; float timer = 0.0f; float startAngle = transform.eulerAngles.z; //減去相對于0位置的偏移角度 maxAngle = maxAngle - GetFitAngle(startAngle); //根據(jù)順時(shí)針逆時(shí)針不同,不同處理 int cw_value = 1; if (CW) { cw_value = -1; } int animationCurveNumber = Random.Range (0, animationCurves.Count); //獲取一個(gè)隨機(jī)索引 while (timer < time) { //計(jì)算旋轉(zhuǎn),動(dòng)畫曲線的Evaluate函數(shù)返回了給定時(shí)間下曲線上的值:從0到1逐漸變化,速度又每個(gè)位置的切線斜率決定。 float angle = maxAngle * animationCurves [animationCurveNumber].Evaluate (timer / time) ; //得到的angle從0到最大角度逐漸變化 速度可變,讓給加到旋轉(zhuǎn)物角度上實(shí)現(xiàn)逐漸旋轉(zhuǎn) 速度可變 transform.eulerAngles = new Vector3 (0.0f, 0.0f, cw_value *angle + startAngle); timer += Time.deltaTime; yield return 0; } //避免旋轉(zhuǎn)有誤,最終確保其在該在的位置 transform.eulerAngles = new Vector3 (0.0f, 0.0f, cw_value *maxAngle + startAngle); //執(zhí)行回調(diào) if (EndCallBack != null) { EndCallBack(); EndCallBack = null; } spinning = false; } //獲取相對角度 private float GetFitAngle(float angle) { if (angle > 0) { if (angle - 360 > 0) { return GetFitAngle(angle - 360); } else { return angle; } } else { if (angle + 360 < 0) { return GetFitAngle(angle + 360); } else { return angle; } } }}注解: 這里使用AnimationCurve動(dòng)畫曲線來控制輪子轉(zhuǎn)動(dòng),設(shè)置不同的曲線類型可以實(shí)現(xiàn)不同的效果。 1/ 可以先前把腳本放到物體上,預(yù)設(shè)好曲線 
2/ 也可以代碼動(dòng)態(tài)創(chuàng)建曲線 因?yàn)楣俜經(jīng)]有預(yù)設(shè)曲線demo,所以要自己創(chuàng)建,常用3種如下:
No.1 先慢再快再慢
Keyframe[] ks = new Keyframe[3];ks[0] = new Keyframe(0, 0);ks[0].inTangent = 0;ks[0].outTangent = 0;ks[1] = new Keyframe(0.5f,0.5f);ks[1].inTangent = 1;ks[1].outTangent = 1;ks[2] = new Keyframe(1,1);ks[2].inTangent =0;ks[2].outTangent =0;AnimationCurve animationCurve = new AnimationCurve(ks);得到曲線: 
No.2 先慢再快
Keyframe[] ks = new Keyframe[2];ks[0] = new Keyframe(0, 0);ks[0].inTangent = 0;ks[0].outTangent = 0;ks[1] = new Keyframe(1,1);ks[1].inTangent = 2;ks[1].outTangent = 2;AnimationCurve animationCurve = new AnimationCurve(ks);得到曲線: 
No.3 先快再慢
Keyframe[] ks = new Keyframe[2];ks[0] = new Keyframe(0, 0);ks[0].inTangent = 2;ks[0].outTangent = 2;ks[1] = new Keyframe(1,1);ks[1].inTangent = 0;ks[1].outTangent = 0;AnimationCurve animationCurve = new AnimationCurve(ks);得到曲線: 
這里簡單介紹一下Unity動(dòng)畫曲線AnimationCurve,詳細(xì)內(nèi)容參考AnimationCurve: http://blog.csdn.net/QQ_33337811/article/details/54914905
新聞熱點(diǎn)
疑難解答