欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity3d實(shí)現(xiàn)無限循環(huán)滾動背景

 更新時間:2022年01月05日 11:29:58   作者:WanZiCong  
這篇文章主要為大家詳細(xì)介紹了Unity3d實(shí)現(xiàn)無限循環(huán)滾動背景,一個完整的商店廣告牌組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

在游戲項(xiàng)目中我們常??吹缴坛堑膹V告牌,幾張廣告圖片循環(huán)滾動,類似跑馬燈,現(xiàn)在我將討論一種實(shí)現(xiàn)方法,并提供一個管理類,大家可以直接使用。

實(shí)現(xiàn)原理:背景圖片循環(huán)滾動的原理很簡單:兩張圖片向一個方向移動,當(dāng)達(dá)某張圖片到臨界區(qū)域時將圖片放在后面,依次循環(huán)。

在實(shí)際項(xiàng)目中,廣告牌顯示的圖片數(shù)量不確定,例如某個假期活動會上很多新品,此時我們需要動態(tài)的創(chuàng)建顯示的圖片(一般在配置表讀取數(shù)據(jù)),如果需要顯示分類標(biāo)簽還得動態(tài)生成分類標(biāo)簽。

綜上所述,一個完整的廣告牌組件應(yīng)該具有以下功能:

- 無限循環(huán)的滾動背景圖片
- 根據(jù)讀取的數(shù)據(jù)動態(tài)生成圖片
- 具有分類標(biāo)簽,根據(jù)顯示圖片動態(tài)生成
- *做成一個管理類,方便使用(一個項(xiàng)目中可能多處會用到)

下面談?wù)勎业姆椒ǎ?/p>

第一步:創(chuàng)建滾動組件AdvertisementScroll,這個組件掛在GameObject上面,接受傳遞過來的數(shù)據(jù),生成顯示的圖片和分類標(biāo)簽,在Update中實(shí)現(xiàn)無限循環(huán)滾動。

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)前頁面 ? ?
? ? ? ? ?private AdvertisementScrollManager.AdvertisementScrollData _itemData; ? ? ??
? ? ? ? ?private List<ToggleData> _toggleList = new List<ToggleData>(); ? ? ? ?
? ? ? ? ?private Vector3 _toLeftPosition;//向左滑動到某個位置
? ? ? ? ?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)建分頁圖片,默認(rèn)不創(chuàng)建(需要分頁圖片,分頁的中間位置,每個分頁的間隔)
? ? /// </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,將它做成一個單例,調(diào)用AdvertisementScroll的方法,開始滾動。

using UnityEngine;
using System.Collections.Generic;
namespace Assets.Scripts.Client
{
public class AdvertisementScrollManager : Singleton<AdvertisementScrollManager>
{ ?
? ? public struct AdvertisementScrollData
? ? {
? ? ? ? public bool IsCreateToggle; ? ? ? //是否創(chuàng)建分頁標(biāo)簽
? ? ? ? public Transform ToggleItem; ? ? ?//分頁標(biāo)簽
? ? ? ? public Transform ToggleContainer; //分頁標(biāo)簽所在的Panel
? ? ? ? public Vector3 ToggleCenterPos; ? //分頁標(biāo)簽的中間位置
? ? ? ? public int ToggleDistance; ? ? ? ?//分頁標(biāo)簽之間的間隔
? ? ? ? public Transform FirstMoveGo; ? ? //移動的物體
? ? ? ? public Transform SecondMoveGo; ? ?//移動的物體
? ? ? ? public Vector3 LeftPosition; ? ? ?//移動物體的左邊位置
? ? ? ? public Vector3 RightPosition; ? ? //移動物體的右邊位置
? ? ? ? public Transform SpriteItem; ? ? ?//顯示的圖片
? ? ? ? public int SpriteWidth; ? ? ? ? ? //圖片的寬度
? ? ? ? public string[] SpriteName; ? ? ? //顯示的所有圖片在圖集中的名稱
? ? ? ? public int MaxPage; ? ? ? ? ? ? ? //最大的頁面
? ? ? ? public int MoveTime; ? ? ? ? ? ? ?//每隔多少秒移動一次

? ? };

? ? 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ù)制體的制作方法就不說了,代碼看懂了自然好弄,后面也會附上工程文件。你在任何一個界面需要使用廣告牌組件時只需要先設(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ù)量和滑動的時間間隔
? ? ? ? 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è)置分頁標(biāo)簽信息(如果不需要分頁標(biāo)簽,可以不用賦值)
? ? ? ? data.IsCreateToggle = true;
? ? ? ? data.ToggleItem = _toggleItem;
? ? ? ? data.ToggleContainer = _toggleContainer; ?
? ? ? ? data.ToggleCenterPos = new Vector3(0,-200,0);
? ? ? ? data.ToggleDistance = 30; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? return data;
? ? }
}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論