Unity3d實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)背景
在游戲項(xiàng)目中我們常??吹缴坛堑膹V告牌,幾張廣告圖片循環(huán)滾動(dòng),類似跑馬燈,現(xiàn)在我將討論一種實(shí)現(xiàn)方法,并提供一個(gè)管理類,大家可以直接使用。
實(shí)現(xiàn)原理:背景圖片循環(huán)滾動(dòng)的原理很簡(jiǎn)單:兩張圖片向一個(gè)方向移動(dòng),當(dāng)達(dá)某張圖片到臨界區(qū)域時(shí)將圖片放在后面,依次循環(huán)。
在實(shí)際項(xiàng)目中,廣告牌顯示的圖片數(shù)量不確定,例如某個(gè)假期活動(dòng)會(huì)上很多新品,此時(shí)我們需要?jiǎng)討B(tài)的創(chuàng)建顯示的圖片(一般在配置表讀取數(shù)據(jù)),如果需要顯示分類標(biāo)簽還得動(dòng)態(tài)生成分類標(biāo)簽。
綜上所述,一個(gè)完整的廣告牌組件應(yīng)該具有以下功能:
- 無(wú)限循環(huán)的滾動(dòng)背景圖片
- 根據(jù)讀取的數(shù)據(jù)動(dòng)態(tài)生成圖片
- 具有分類標(biāo)簽,根據(jù)顯示圖片動(dòng)態(tài)生成
- *做成一個(gè)管理類,方便使用(一個(gè)項(xiàng)目中可能多處會(huì)用到)
下面談?wù)勎业姆椒ǎ?/p>
第一步:創(chuàng)建滾動(dòng)組件AdvertisementScroll,這個(gè)組件掛在GameObject上面,接受傳遞過(guò)來(lái)的數(shù)據(jù),生成顯示的圖片和分類標(biāo)簽,在Update中實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)。
using System.Collections.Generic;
using UnityEngine;
namespace Assets.Scripts.Client
{?
? ? public class AdvertisementScroll : MonoBehaviour
? ? { ??
? ? ? ? ?private float _timer;
? ? ? ? ?private bool _isStart;
? ? ? ? ?private Vector3 _toggleCenter;
? ? ? ? ?private int delDistance;
? ? ? ? ?private int _currentPage; //當(dāng)前頁(yè)面 ? ?
? ? ? ? ?private AdvertisementScrollManager.AdvertisementScrollData _itemData; ? ? ??
? ? ? ? ?private List<ToggleData> _toggleList = new List<ToggleData>(); ? ? ? ?
? ? ? ? ?private Vector3 _toLeftPosition;//向左滑動(dòng)到某個(gè)位置
? ? ? ? ?public class ToggleData
? ? ? ? ?{
? ? ? ? ? ? public string name;
? ? ? ? ? ? public GameObject go;
? ? ? ? ?}
? ? ? ? public AdvertisementScrollManager.AdvertisementScrollData ItemData
? ? ? ? {
? ? ? ? ? ? get { return _itemData; }
? ? ? ? ? ? set { _itemData = value; }
? ? ? ? }
? ? public void StartScroll(bool createToggle)
? ? {
? ? ? ? if (!_isStart)
? ? ? ? {
? ? ? ? ? ? CreateAdvertiseBg();
? ? ? ? ? ? CreateAdvertiseToggle(createToggle);
? ? ? ? ? ? if (createToggle)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (ItemData.ToggleItem != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ItemData.ToggleItem.GetComponent<UIToggle>().value = true;
? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? } ? ? ? ? ? ? ??
? ? ? ? ? ? _currentPage = 1;
? ? ? ? ? ? _isStart = true;
? ? ? ? } ? ? ? ?
? ? }
? ? /// <summary>
? ? /// 創(chuàng)建需要顯示的圖片(需要圖片數(shù)量,圖片精靈名稱,圖片的寬度,圖片左邊和右邊的顯示位置)
? ? /// </summary>
? ? private void CreateAdvertiseBg()
? ? {
? ? ? ? _toLeftPosition = new Vector3(ItemData.LeftPosition.x - ItemData.SpriteWidth, ItemData.LeftPosition.y, ItemData.LeftPosition.z);
? ? ? ? for (int i = 0; i < ItemData.MaxPage; i++)
? ? ? ? {
? ? ? ? ? ? GameObject firstSpriteItem;
? ? ? ? ? ? GameObject secondSpriteItem;
? ? ? ? ? ? if (i == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? firstSpriteItem = secondSpriteItem = ItemData.SpriteItem.gameObject;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? firstSpriteItem = ItemData.FirstMoveGo.gameObject.AddChild(ItemData.SpriteItem.gameObject);
? ? ? ? ? ? ? ? firstSpriteItem.SetActive(false);
? ? ? ? ? ? ? ? secondSpriteItem = ItemData.SecondMoveGo.gameObject.AddChild(ItemData.SpriteItem.gameObject);
? ? ? ? ? ? ? ? secondSpriteItem.SetActive(false);
? ? ? ? ? ? }
? ? ? ? ? ? firstSpriteItem.transform.localPosition = secondSpriteItem.transform.localPosition = Vector3.zero;
? ? ? ? ? ? firstSpriteItem.name = secondSpriteItem.name = (i + 1).ToString();
? ? ? ? ? ? firstSpriteItem.GetComponent<UISprite>().spriteName = secondSpriteItem.GetComponent<UISprite>().spriteName = ItemData.SpriteName[i];
? ? ? ? }
? ? }
? ? /// <summary>
? ? /// 創(chuàng)建分頁(yè)圖片,默認(rèn)不創(chuàng)建(需要分頁(yè)圖片,分頁(yè)的中間位置,每個(gè)分頁(yè)的間隔)
? ? /// </summary>
? ? /// <param name="create"></param>
? ? private void CreateAdvertiseToggle(bool create = false)
? ? {
? ? ? ? if (create)
? ? ? ? {
? ? ? ? ? ? _toggleCenter = ItemData.ToggleCenterPos;
? ? ? ? ? ? delDistance = ItemData.ToggleDistance; ? ? ??
? ? ? ? ? ? Vector3 firstpoint = _toggleCenter - new Vector3((ItemData.MaxPage / 2f - 0.5f) * delDistance, 0f, 0f);
? ? ? ? ? ? for (int i = 0; i < ItemData.MaxPage; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameObject item;
? ? ? ? ? ? ? ? ToggleData toggleData = new ToggleData();
? ? ? ? ? ? ? ? if (i == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? item = ItemData.ToggleItem.gameObject;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? item = ItemData.ToggleContainer.gameObject.AddChild(ItemData.ToggleItem.gameObject);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? item.transform.localPosition = firstpoint + new Vector3(i*delDistance, 0f, 0f);
? ? ? ? ? ? ? ? item.name = "toggle" + i;
? ? ? ? ? ? ? ? toggleData.go = item;
? ? ? ? ? ? ? ? toggleData.name = item.name;
? ? ? ? ? ? ? ? _toggleList.Add(toggleData);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? void Update()
? ? {
? ? ? ? if (!_isStart)
? ? ? ? {
? ? ? ? ? ? return; ? ? ? ? ? ? ?
? ? ? ? }
? ? ? ? if (Time.frameCount % (30 * ItemData.MoveTime) == 0 && ItemData.MaxPage > 1)
? ? ? ? {
? ? ? ? ? ? if (ItemData.FirstMoveGo.localPosition.x < ItemData.LeftPosition.x - 0.5)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ItemData.FirstMoveGo.localPosition = ItemData.RightPosition;
? ? ? ? ? ? }
? ? ? ? ? ? if (ItemData.SecondMoveGo.localPosition.x < ItemData.LeftPosition.x - 0.5)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ItemData.SecondMoveGo.localPosition = ItemData.RightPosition;
? ? ? ? ? ? }
? ? ? ? ? ? Transform leftTran = ItemData.FirstMoveGo.localPosition.x < ItemData.SecondMoveGo.localPosition.x ? ItemData.FirstMoveGo : ItemData.SecondMoveGo;
? ? ? ? ? ? Transform rightTran = ItemData.FirstMoveGo.localPosition.x < ItemData.SecondMoveGo.localPosition.x ? ItemData.SecondMoveGo : ItemData.FirstMoveGo;
? ? ? ? ? ? TweenPosition.Begin(rightTran.gameObject, 0.5f, ItemData.LeftPosition, false);
? ? ? ? ? ? TweenPosition.Begin(leftTran.gameObject, 0.5f, _toLeftPosition, false);
? ? ? ? ? ? _currentPage = FixPage(_currentPage);
? ? ? ? ? ? SetPic(rightTran, _currentPage);
? ? ? ? ? ? //SetBtn(leftTran,false);
? ? ? ? ? ? //SetBtn(rightTran,true); ? ? ? ? ? ? ??
? ? ? ? ? ? _toggleList[_currentPage - 1].go.GetComponent<UIToggle>().value = true;
? ? ? ? }
? ? }
? ? private void SetBtn(Transform tran, bool state)
? ? {
? ? ? ? Transform tf = tran.Find("Icon");
? ? ? ? if (tf != null)
? ? ? ? {
? ? ? ? ? ? tf.gameObject.SetActive(state);
? ? ? ? } ? ? ? ? ??
? ? }
? ? private void SetPic(Transform tran, int _currentPage)
? ? {
? ? ? ? foreach (Transform t in tran)
? ? ? ? {
? ? ? ? ? ? if (t.name == _currentPage.ToString())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? t.gameObject.SetActive(true);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? t.gameObject.SetActive(false);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? private int FixPage(int page)
? ? {
? ? ? ? page++;
? ? ? ? if (page > ItemData.MaxPage)
? ? ? ? {
? ? ? ? ? ? page = 1;
? ? ? ? }
? ? ? ? return page;
? ? }
} ? ?
}第二步:創(chuàng)建管理類AdvertisementScrollManager,將它做成一個(gè)單例,調(diào)用AdvertisementScroll的方法,開始滾動(dòng)。
using UnityEngine;
using System.Collections.Generic;
namespace Assets.Scripts.Client
{
public class AdvertisementScrollManager : Singleton<AdvertisementScrollManager>
{ ?
? ? public struct AdvertisementScrollData
? ? {
? ? ? ? public bool IsCreateToggle; ? ? ? //是否創(chuàng)建分頁(yè)標(biāo)簽
? ? ? ? public Transform ToggleItem; ? ? ?//分頁(yè)標(biāo)簽
? ? ? ? public Transform ToggleContainer; //分頁(yè)標(biāo)簽所在的Panel
? ? ? ? public Vector3 ToggleCenterPos; ? //分頁(yè)標(biāo)簽的中間位置
? ? ? ? public int ToggleDistance; ? ? ? ?//分頁(yè)標(biāo)簽之間的間隔
? ? ? ? public Transform FirstMoveGo; ? ? //移動(dòng)的物體
? ? ? ? public Transform SecondMoveGo; ? ?//移動(dòng)的物體
? ? ? ? public Vector3 LeftPosition; ? ? ?//移動(dòng)物體的左邊位置
? ? ? ? public Vector3 RightPosition; ? ? //移動(dòng)物體的右邊位置
? ? ? ? public Transform SpriteItem; ? ? ?//顯示的圖片
? ? ? ? public int SpriteWidth; ? ? ? ? ? //圖片的寬度
? ? ? ? public string[] SpriteName; ? ? ? //顯示的所有圖片在圖集中的名稱
? ? ? ? public int MaxPage; ? ? ? ? ? ? ? //最大的頁(yè)面
? ? ? ? public int MoveTime; ? ? ? ? ? ? ?//每隔多少秒移動(dòng)一次
? ? };
? ? public void StartAdvertisementScroll(Transform parentTf, AdvertisementScrollData data,bool createToggle = false)
? ? {
? ? ? ? if (parentTf != null)
? ? ? ? {
? ? ? ? ? ? UIPanel panel = parentTf.GetComponent<UIPanel>();
? ? ? ? ? ? if (panel == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? AdvertisementScroll scroll = null;
? ? ? ? ? ? Transform tf = parentTf.Find("AdvertisementScroll");
? ? ? ? ? ? if (tf == null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameObject go = new GameObject();
? ? ? ? ? ? ? ? go.name = "AdvertisementScroll";
? ? ? ? ? ? ? ? go.transform.parent = parentTf;
? ? ? ? ? ? ? ? go.transform.localPosition = Vector3.zero;
? ? ? ? ? ? ? ? go.transform.localScale = new Vector3(1, 1, 1);
? ? ? ? ? ? ? ? //go.layer = LayerModel.UILayer;
? ? ? ? ? ? ? ? tf = go.transform;
? ? ? ? ? ? ? ? scroll = tf.gameObject.AddComponent<AdvertisementScroll>(); ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? scroll = tf.gameObject.GetComponent<AdvertisementScroll>();
? ? ? ? ? ? }
? ? ? ? ? ? scroll.ItemData = data;
? ? ? ? ? ? scroll.ItemData.FirstMoveGo.parent = tf;
? ? ? ? ? ? scroll.ItemData.SecondMoveGo.parent = tf;
? ? ? ? ? ? scroll.StartScroll(createToggle);
? ? ? ? }
? ? }
}
}第三步:使用。預(yù)制體的制作方法就不說(shuō)了,代碼看懂了自然好弄,后面也會(huì)附上工程文件。你在任何一個(gè)界面需要使用廣告牌組件時(shí)只需要先設(shè)置好數(shù)據(jù),然后調(diào)用AdvertisementScrollManager中的StartAdvertisementScroll方法就可以了。
using Assets.Scripts.Client;
using UnityEngine;
namespace Assets
{
public class AdvertiseScrollSample : MonoBehaviour
{
? ? private Transform _firstMoveGo;
? ? private Transform _secondMoveGo;
? ? private Transform _container;
? ? private Transform _toggleContainer;
? ? private Transform _spriteItem;
? ? private Transform _toggleItem; ? ? ?
? ? void Start ()
? ? {
? ? ? ? _firstMoveGo = transform.Find("Panel/Container/First");
? ? ? ? _secondMoveGo = transform.Find("Panel/Container/Second");
? ? ? ? _container = transform.Find("Panel/Container");
? ? ? ? _toggleContainer = transform.Find("Panel/ToggleContainer");
? ? ? ? _spriteItem = transform.Find("Panel/Container/First/Item");
? ? ? ? _toggleItem = transform.Find("Panel/ToggleContainer/ToggleItem");
? ? ? ? OnRefreshData();
? ? }
? ? void OnRefreshData()
? ? {
? ? ? ? AdvertisementScrollManager.AdvertisementScrollData data = CreateAdvertisementScrollData();
? ? ? ? AdvertisementScrollManager.Instance.StartAdvertisementScroll(_container,data,data.ToggleContainer);
? ? }
? ? private AdvertisementScrollManager.AdvertisementScrollData CreateAdvertisementScrollData()
? ? {
? ? ? ? AdvertisementScrollManager.AdvertisementScrollData data = new AdvertisementScrollManager.AdvertisementScrollData();
? ? ? ? //設(shè)置顯示圖片的數(shù)量和滑動(dòng)的時(shí)間間隔
? ? ? ? data.MoveTime = 10;
? ? ? ? data.MaxPage = 3;
? ? ? ? //設(shè)置圖片的位置信息
? ? ? ? data.FirstMoveGo = _firstMoveGo;
? ? ? ? data.SecondMoveGo = _secondMoveGo;
? ? ? ? data.SpriteItem = _spriteItem;
? ? ? ? data.SpriteWidth = 884;
? ? ? ? data.SpriteName = new string[] { "1", "2", "3" };
? ? ? ? data.LeftPosition = Vector3.zero;
? ? ? ? data.RightPosition = new Vector3(800, 0, 0);
? ? ? ? //設(shè)置分頁(yè)標(biāo)簽信息(如果不需要分頁(yè)標(biāo)簽,可以不用賦值)
? ? ? ? data.IsCreateToggle = true;
? ? ? ? data.ToggleItem = _toggleItem;
? ? ? ? data.ToggleContainer = _toggleContainer; ?
? ? ? ? data.ToggleCenterPos = new Vector3(0,-200,0);
? ? ? ? data.ToggleDistance = 30; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? return data;
? ? }
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實(shí)例,本文思路同樣適用必應(yīng)、搜狗、搜搜、360等搜索引擎,需要的朋友可以參考下2015-01-01
newtonsoft.json解析天氣數(shù)據(jù)出錯(cuò)解決方法
這篇文章主要介紹了NewtonSoft.JSon解析天氣數(shù)據(jù)時(shí)出錯(cuò)的解決方法,需要的朋友可以參考下2014-02-02
C#語(yǔ)法相比其它語(yǔ)言比較獨(dú)特的地方(三)
這篇文章主要介紹了C#語(yǔ)法相比其它語(yǔ)言比較獨(dú)特的地方(三),本文講解了在C++中允許從一個(gè)case貫穿到另一個(gè)case標(biāo)簽、as和is只會(huì)檢測(cè)待轉(zhuǎn)化類型的類型,而不會(huì)進(jìn)行其它操作等內(nèi)容,需要的朋友可以參考下2015-04-04

