3种转盘抽奖动画效果实现
程序员文章站
2022-07-12 12:51:18
...
很多游戏里面会涉及到各种抽奖系统(基本都是假随机,都懂的),这里实现了3种转盘相关的简单抽奖,希望对你有帮助!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
public class LuckDraw : MonoBehaviour
{
//箭头,物品节点, 框选
public Transform _Arrow, _Awards, _Kuang;
public Transform[] _AwardList;//物品列表
public InputField _InputField;//输入框,输入0~7
Tweener _tweener = null;//动画
int _rollTimes = 5;//圈数
int _index_1, index_2 = 0;//下标1(结果), 下标2(物品下标)
bool _playKuang = false;//框动画播放标志
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//转盘转动的时候,让物品保持原来的角度
for (int i = 0; i < _AwardList.Length; ++i)
{
_AwardList[i].eulerAngles = Vector3.zero;
}
if (_playKuang)
{
PlayKuang();
}
}
bool GetIndex()
{
if (!int.TryParse(_InputField.text, out _index_1))
{
return false;
}
_index_1 = int.Parse(_InputField.text);//0~7;
if (_index_1 < 0 || _index_1 > 7)
{
return false;
}
return true;
}
/// <summary>
/// 箭头转动
/// </summary>
public void ArrowRoll()
{
if (!GetIndex())
return;
float angle = -90 - _rollTimes * 360 - _index_1 * 360 / 8.0f;//角度
//RotateMode.FastBeyond360第一圈不计算在_rollTimes中
_tweener = _Arrow.DORotate(new Vector3(0, 0, angle), 2.0f, RotateMode.FastBeyond360);
//_tweener.SetEase(Ease.Flash);//这里可以设置动画效果
}
public void BtnEvent_0()
{
ArrowRoll();
}
/// <summary>
/// 转盘转动
/// </summary>
public void BtnEvent_1()
{
if (!GetIndex())
return;
float angle = 5 * 360 + _index_1 * 360 / 8.0f;//角度
_tweener = _Awards.DORotate(new Vector3(0, 0, angle), 2.0f, RotateMode.FastBeyond360);
}
public void BtnEvent_2()
{
if (!GetIndex())
return;
_Kuang.gameObject.SetActive(true);
_playKuang = true;
//InvokeRepeating("Kuang", 0.01f, 0.5f);
}
int kuang_rollTimes = 0;
void PlayKuang()
{
//转满动画圈数后
if (kuang_rollTimes == _rollTimes)
{
if (index_2 < _index_1)
{
index_2++;
_Kuang.position = _AwardList[index_2].position;
Debug.Log("框下标2:" + index_2);
}
else
{
_playKuang = false;
}
}
else//前几圈只是动画
{
if (index_2 == 8)
{
index_2 = 0;
kuang_rollTimes++;
}
_Kuang.position = _AwardList[index_2].position;
index_2++;
if (kuang_rollTimes == _rollTimes)
index_2 = 0;
}
}
}
特别说明:没有写初始化,请每次测试的时候重新运行!!!!!!