Unity中如何制作动态跑马灯抽奖动画效果?
- 内容介绍
- 文章标签
- 相关推荐
本文共计853个文字,预计阅读时间需要4分钟。
Unity跑马灯抽奖效果实现代码,供大家参考。内容如下:
使用NGUI和Dotween插件,思路简单说明:
1. 首先排版,通过移动图片蒙版来实现跑马灯效果。
2.使用NGUI进行界面布局,Dotween进行动画控制。
以下是代码部分:
csharp
using UnityEngine;using UnityEngine.UI;using DG.Tweening;public class MarqueeEffect : MonoBehaviour{ public Image marqueeImage; // 跑马灯图片 public float speed=10f; // 跑马灯速度 public float width=100f; // 跑马灯宽度
private void Start() { StartCoroutine(MarqueeEffectCoroutine()); }
private IEnumerator MarqueeEffectCoroutine() { while (true) { // 移动图片 Vector3 targetPosition=new Vector3(-width, 0, 0); marqueeImage.DOAnchorPos(targetPosition, 1f / speed).SetEase(Ease.Linear);
// 等待图片回到起始位置 yield return new WaitForSeconds(1f / speed);
// 重置图片位置 marqueeImage.rectTransform.localPosition=Vector3.zero; } }}
文件目录:- MarqueeEffect.cs (放置在脚本文件夹中)- MarqueeImage.png (跑马灯图片)
代码部分结束。
Unity 跑马灯抽奖效果实现代码,供大家参考,具体内容如下
这边用到插件是NGUI+Dotween,思路简单说下:先排版,通过移动图片蒙版来实现效果。
下面是排版和文件目录。
代码部分是通过余数去确认停的位置,boxlist通过unity拖拉加入数据,chooseBoxList直接通过余数判断添加。
代码量很少,稍微看下就明白了。
直接上代码了:
using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; public class goLuckyDraw : MonoBehaviour { private int index = 23; //index%12 来判断停在哪个位置 // Use this for initialization private UISprite prize1,prize2,prize3,prize4,prize5,prize6,prize7,prize8,prize9,prize10,prize11,prize12, prize13, prize14; private UISprite transparentBox; public List<Transform> boxList = new List<Transform>(); public List<Transform> chooseBoxList = new List<Transform>(); void Start () { prize1 = GameObject.Find("Lucky/Bg/prize1").GetComponent<UISprite>(); prize2 = GameObject.Find("Lucky/Bg/prize2").GetComponent<UISprite>(); prize3 = GameObject.Find("Lucky/Bg/prize3").GetComponent<UISprite>(); prize4 = GameObject.Find("Lucky/Bg/prize4").GetComponent<UISprite>(); prize5 = GameObject.Find("Lucky/Bg/prize5").GetComponent<UISprite>(); prize6 = GameObject.Find("Lucky/Bg/prize6").GetComponent<UISprite>(); prize7 = GameObject.Find("Lucky/Bg/prize7").GetComponent<UISprite>(); prize8 = GameObject.Find("Lucky/Bg/prize8").GetComponent<UISprite>(); prize9 = GameObject.Find("Lucky/Bg/prize9").GetComponent<UISprite>(); prize10 = GameObject.Find("Lucky/Bg/prize10").GetComponent<UISprite>(); prize11 = GameObject.Find("Lucky/Bg/prize11").GetComponent<UISprite>(); prize12 = GameObject.Find("Lucky/Bg/prize12").GetComponent<UISprite>(); prize13 = GameObject.Find("Lucky/Bg/prize13").GetComponent<UISprite>(); prize14 = GameObject.Find("Lucky/Bg/prize14").GetComponent<UISprite>(); transparentBox = GameObject.Find("Bg/Transparentbox").GetComponent<UISprite>(); transparentBox.gameObject.SetActive(false); //获取需要监听的按钮对象 GameObject button = GameObject.Find("Lucky/Bg/start"); //设置这个按钮的监听,指向本类的ButtonClick方法中。 UIEventListener.Get(button).onClick = StartLuckyDraw; chooseIndex(index); // StartLuckyDraw(); } IEnumerator Move( ) { float time; for (int i = 0; i < boxList.Count; i++) { time = Mathf.Lerp(0.04f, 0.05f, 0.05f*i); Debug.Log("---time----="+time); transparentBox.transform.DOLocalMove(boxList[i].localPosition, time); yield return new WaitForSeconds(0.05f); } for (int i = 0; i < boxList.Count; i++) { time = Mathf.Lerp(0.05f, 0.065f, 0.05f * i); Debug.Log("---time3----=" + time); transparentBox.transform.DOLocalMove(boxList[i].localPosition, time); yield return new WaitForSeconds(0.05f); } for (int i = 0; i < boxList.Count; i++) { time = Mathf.Lerp(0.065f, 0.08f, 0.05f * i); Debug.Log("---time2----=" + time); transparentBox.transform.DOLocalMove(boxList[i].localPosition, time); yield return new WaitForSeconds(0.05f); } for (int i = 0; i < chooseBoxList.Count; i++) { transparentBox.transform.DOLocalMove(chooseBoxList[i].localPosition,0.1f); yield return new WaitForSeconds(0.1f); } yield return new WaitForSeconds(2f); transparentBox.gameObject.SetActive(false); } private void chooseIndex(int index) { chooseBoxList.Clear(); for (int i = 0; i < index % 12; i++) { chooseBoxList.Add(boxList[i]); } } private void StartLuckyDraw(GameObject go) { transparentBox.gameObject.SetActive(true); StartCoroutine(Move()); } void Update () { } }
代码很糙,大家自己完善吧!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计853个文字,预计阅读时间需要4分钟。
Unity跑马灯抽奖效果实现代码,供大家参考。内容如下:
使用NGUI和Dotween插件,思路简单说明:
1. 首先排版,通过移动图片蒙版来实现跑马灯效果。
2.使用NGUI进行界面布局,Dotween进行动画控制。
以下是代码部分:
csharp
using UnityEngine;using UnityEngine.UI;using DG.Tweening;public class MarqueeEffect : MonoBehaviour{ public Image marqueeImage; // 跑马灯图片 public float speed=10f; // 跑马灯速度 public float width=100f; // 跑马灯宽度
private void Start() { StartCoroutine(MarqueeEffectCoroutine()); }
private IEnumerator MarqueeEffectCoroutine() { while (true) { // 移动图片 Vector3 targetPosition=new Vector3(-width, 0, 0); marqueeImage.DOAnchorPos(targetPosition, 1f / speed).SetEase(Ease.Linear);
// 等待图片回到起始位置 yield return new WaitForSeconds(1f / speed);
// 重置图片位置 marqueeImage.rectTransform.localPosition=Vector3.zero; } }}
文件目录:- MarqueeEffect.cs (放置在脚本文件夹中)- MarqueeImage.png (跑马灯图片)
代码部分结束。
Unity 跑马灯抽奖效果实现代码,供大家参考,具体内容如下
这边用到插件是NGUI+Dotween,思路简单说下:先排版,通过移动图片蒙版来实现效果。
下面是排版和文件目录。
代码部分是通过余数去确认停的位置,boxlist通过unity拖拉加入数据,chooseBoxList直接通过余数判断添加。
代码量很少,稍微看下就明白了。
直接上代码了:
using System; using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; public class goLuckyDraw : MonoBehaviour { private int index = 23; //index%12 来判断停在哪个位置 // Use this for initialization private UISprite prize1,prize2,prize3,prize4,prize5,prize6,prize7,prize8,prize9,prize10,prize11,prize12, prize13, prize14; private UISprite transparentBox; public List<Transform> boxList = new List<Transform>(); public List<Transform> chooseBoxList = new List<Transform>(); void Start () { prize1 = GameObject.Find("Lucky/Bg/prize1").GetComponent<UISprite>(); prize2 = GameObject.Find("Lucky/Bg/prize2").GetComponent<UISprite>(); prize3 = GameObject.Find("Lucky/Bg/prize3").GetComponent<UISprite>(); prize4 = GameObject.Find("Lucky/Bg/prize4").GetComponent<UISprite>(); prize5 = GameObject.Find("Lucky/Bg/prize5").GetComponent<UISprite>(); prize6 = GameObject.Find("Lucky/Bg/prize6").GetComponent<UISprite>(); prize7 = GameObject.Find("Lucky/Bg/prize7").GetComponent<UISprite>(); prize8 = GameObject.Find("Lucky/Bg/prize8").GetComponent<UISprite>(); prize9 = GameObject.Find("Lucky/Bg/prize9").GetComponent<UISprite>(); prize10 = GameObject.Find("Lucky/Bg/prize10").GetComponent<UISprite>(); prize11 = GameObject.Find("Lucky/Bg/prize11").GetComponent<UISprite>(); prize12 = GameObject.Find("Lucky/Bg/prize12").GetComponent<UISprite>(); prize13 = GameObject.Find("Lucky/Bg/prize13").GetComponent<UISprite>(); prize14 = GameObject.Find("Lucky/Bg/prize14").GetComponent<UISprite>(); transparentBox = GameObject.Find("Bg/Transparentbox").GetComponent<UISprite>(); transparentBox.gameObject.SetActive(false); //获取需要监听的按钮对象 GameObject button = GameObject.Find("Lucky/Bg/start"); //设置这个按钮的监听,指向本类的ButtonClick方法中。 UIEventListener.Get(button).onClick = StartLuckyDraw; chooseIndex(index); // StartLuckyDraw(); } IEnumerator Move( ) { float time; for (int i = 0; i < boxList.Count; i++) { time = Mathf.Lerp(0.04f, 0.05f, 0.05f*i); Debug.Log("---time----="+time); transparentBox.transform.DOLocalMove(boxList[i].localPosition, time); yield return new WaitForSeconds(0.05f); } for (int i = 0; i < boxList.Count; i++) { time = Mathf.Lerp(0.05f, 0.065f, 0.05f * i); Debug.Log("---time3----=" + time); transparentBox.transform.DOLocalMove(boxList[i].localPosition, time); yield return new WaitForSeconds(0.05f); } for (int i = 0; i < boxList.Count; i++) { time = Mathf.Lerp(0.065f, 0.08f, 0.05f * i); Debug.Log("---time2----=" + time); transparentBox.transform.DOLocalMove(boxList[i].localPosition, time); yield return new WaitForSeconds(0.05f); } for (int i = 0; i < chooseBoxList.Count; i++) { transparentBox.transform.DOLocalMove(chooseBoxList[i].localPosition,0.1f); yield return new WaitForSeconds(0.1f); } yield return new WaitForSeconds(2f); transparentBox.gameObject.SetActive(false); } private void chooseIndex(int index) { chooseBoxList.Clear(); for (int i = 0; i < index % 12; i++) { chooseBoxList.Add(boxList[i]); } } private void StartLuckyDraw(GameObject go) { transparentBox.gameObject.SetActive(true); StartCoroutine(Move()); } void Update () { } }
代码很糙,大家自己完善吧!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

