Unity3d实现⽆限循环滚动背景
在游戏项⽬中我们常常看到商城的⼴告牌,⼏张⼴告图⽚循环滚动,类似跑马灯,现在我将讨论⼀种实现⽅法,并提供⼀个管理类,⼤家可以直接使⽤。
实现原理:背景图⽚循环滚动的原理很简单:两张图⽚向⼀个⽅向移动,当达某张图⽚到临界区域时将图⽚放在后⾯,依次循环。
在实际项⽬中,⼴告牌显⽰的图⽚数量不确定,例如某个假期活动会上很多新品,此时我们需要动态的创建显⽰的图⽚(⼀般在配置表读取数据),如果需要显⽰分类标签还得动态⽣成分类标签。
综上所述,⼀个完整的⼴告牌组件应该具有以下功能:
- ⽆限循环的滚动背景图⽚
- 根据读取的数据动态⽣成图⽚
- 具有分类标签,根据显⽰图⽚动态⽣成
- *做成⼀个管理类,⽅便使⽤(⼀个项⽬中可能多处会⽤到)
下⾯谈谈我的⽅法:
第⼀步:创建滚动组件AdvertisementScroll,这个组件挂在GameObject上⾯,接受传递过来的数据,⽣成显⽰的图⽚和分类标签,在Update中实现⽆限循环滚动。
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; //当前页⾯
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>
/
// 创建需要显⽰的图⽚(需要图⽚数量,图⽚精灵名称,图⽚的宽度,图⽚左边和右边的显⽰位置)
/// </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.name = secondSpriteItem.name = (i + 1).ToString();
firstSpriteItem.GetComponent<UISprite>().spriteName = secondSpriteItem.GetComponent<UISprite>().spriteName = ItemData.SpriteName[i];
}
}
/// <summary>
/// 创建分页图⽚,默认不创建(需要分页图⽚,分页的中间位置,每个分页的间隔)
/// </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.name = "toggle" + i;
< = 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;
}
}
}
第⼆步:创建管理类AdvertisementScrollManager,将它做成⼀个单例,调⽤AdvertisementScroll的⽅法,开始滚动。using UnityEngine;
using System.Collections.Generic;
namespace Assets.Scripts.Client
{
public class AdvertisementScrollManager : Singleton<AdvertisementScrollManager>
{
public struct AdvertisementScrollData
{
public bool IsCreateToggle; //是否创建分页标签
public Transform ToggleItem; //分页标签
public Transform ToggleContainer; //分页标签所在的Panel
public Vector3 ToggleCenterPos; //分页标签的中间位置
public int ToggleDistance; //分页标签之间的间隔
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.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);
}
}
}
}
第三步:使⽤。预制体的制作⽅法就不说了,代码看懂了⾃然好弄,后⾯也会附上⼯程⽂件。你在任何⼀个界⾯需要使⽤⼴告牌组件时只需要先设置好数据,然后调⽤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();
//设置显⽰图⽚的数量和滑动的时间间隔
data.MoveTime = 10;
data.MaxPage = 3;
//设置图⽚的位置信息
data.FirstMoveGo = _firstMoveGo;
data.SecondMoveGo = _secondMoveGo;
data.SpriteItem = _spriteItem;
data.SpriteWidth = 884;
data.SpriteName = new string[] { "1", "2", "3" };
data.LeftPosition = ;
data.RightPosition = new Vector3(800, 0, 0);
//设置分页标签信息(如果不需要分页标签,可以不⽤赋值)
data.IsCreateToggle = true;
data.ToggleItem = _toggleItem;
data.ToggleContainer = _toggleContainer;
data.ToggleCenterPos = new Vector3(0,-200,0);
data.ToggleDistance = 30;
return data;
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论