In game projects, we often see billboards in malls, with several advertising images scrolling in loops, similar to marquee *s. Now I will discuss an implementation method and provide a management class that everyone can use directly.
Implementation principle:The principle of cyclic scrolling of background pictures is very simple: two pictures move in one direction, and when a certain picture reaches the critical area, put the picture behind and loop in turn.
In actual projects, the number of pictures displayed on the billboard is uncertain. For example, there will be many new products on a certain holiday event. At this time, we need to dynamically create the displayed pictures (usually reading data in the configuration table). If we need to display the classification tag, we must dynamically generate the classification tag.
To sum up, a complete billboard component should have the following functions:
- Infinite loop scroll background picture
- Dynamically generate pictures based on the read data
- Has classification tags, dynamically generated based on displayed pictures
- *Make a management class for easy use (it may be used in multiple places in a project)
Let’s talk about my method below:
first step:Create a scrolling component AdvertisementScroll, which is hung on GameObject, accepts the passed data, generates displayed pictures and classification tags, and implements infinite loop scrolling in Update.
using ; using UnityEngine; namespace { public class AdvertisementScroll : MonoBehaviour { private float _timer; private bool _isStart; private Vector3 _toggleCenter; private int delDistance; private int _currentPage; //Current page private _itemData; private List<ToggleData> _toggleList = new List<ToggleData>(); private Vector3 _toLeftPosition;//Swipe left to a certain position public class ToggleData { public string name; public GameObject go; } public ItemData { get { return _itemData; } set { _itemData = value; } } public void StartScroll(bool createToggle) { if (!_isStart) { CreateAdvertiseBg(); CreateAdvertiseToggle(createToggle); if (createToggle) { if ( != null) { <UIToggle>().value = true; } } _currentPage = 1; _isStart = true; } } /// <summary> /// Create the picture to be displayed (the number of pictures required, the name of the picture sprite, the width of the picture, the display position on the left and right of the picture) /// </summary> private void CreateAdvertiseBg() { _toLeftPosition = new Vector3( - , , ); for (int i = 0; i < ; i++) { GameObject firstSpriteItem; GameObject secondSpriteItem; if (i == 0) { firstSpriteItem = secondSpriteItem = ; } else { firstSpriteItem = (); (false); secondSpriteItem = (); (false); } = = ; = = (i + 1).ToString(); <UISprite>().spriteName = <UISprite>().spriteName = [i]; } } /// <summary> /// Create paging pictures, not created by default (paging pictures are required, the middle position of the paging, and the interval of each paging) /// </summary> /// <param name="create"></param> private void CreateAdvertiseToggle(bool create = false) { if (create) { _toggleCenter = ; delDistance = ; Vector3 firstpoint = _toggleCenter - new Vector3(( / 2f - 0.5f) * delDistance, 0f, 0f); for (int i = 0; i < ; i++) { GameObject item; ToggleData toggleData = new ToggleData(); if (i == 0) { item = ; } else { item = (); } = firstpoint + new Vector3(i*delDistance, 0f, 0f); = "toggle" + i; = item; = ; _toggleList.Add(toggleData); } } } void Update() { if (!_isStart) { return; } if ( % (30 * ) == 0 && > 1) { if ( < - 0.5) { = ; } if ( < - 0.5) { = ; } Transform leftTran = < ? : ; Transform rightTran = < ? : ; (, 0.5f, , false); (, 0.5f, _toLeftPosition, false); _currentPage = FixPage(_currentPage); SetPic(rightTran, _currentPage); //SetBtn(leftTran,false); //SetBtn(rightTran,true); _toggleList[_currentPage - 1].<UIToggle>().value = true; } } private void SetBtn(Transform tran, bool state) { Transform tf = ("Icon"); if (tf != null) { (state); } } private void SetPic(Transform tran, int _currentPage) { foreach (Transform t in tran) { if ( == _currentPage.ToString()) { (true); } else { (false); } } } private int FixPage(int page) { page++; if (page > ) { page = 1; } return page; } } }
Step 2:Create the AdvertisementScrollManager management class, make it into a singleton, call the AdvertisementScroll method, and start scrolling.
using UnityEngine; using ; namespace { public class AdvertisementScrollManager : Singleton<AdvertisementScrollManager> { public struct AdvertisementScrollData { public bool IsCreateToggle; //Whether to create a pagination tag public Transform ToggleItem; //Pagination tag public Transform ToggleContainer; //The Panel where the pagination tag is located public Vector3 ToggleCenterPos; //The middle position of the pagination tag public int ToggleDistance; //The interval between pagination tags public Transform FirstMoveGo; //Moving object public Transform SecondMoveGo; //Moving object public Vector3 LeftPosition; //The left position of the moving object public Vector3 RightPosition; //The right position of the moving object public Transform SpriteItem; //The picture displayed public int SpriteWidth; //The width of the picture public string[] SpriteName; //The names of all pictures displayed in the picture gallery public int MaxPage; //The largest page public int MoveTime; //How many seconds will it move every }; public void StartAdvertisementScroll(Transform parentTf, AdvertisementScrollData data,bool createToggle = false) { if (parentTf != null) { UIPanel panel = <UIPanel>(); if (panel == null) { return; } AdvertisementScroll scroll = null; Transform tf = ("AdvertisementScroll"); if (tf == null) { GameObject go = new GameObject(); = "AdvertisementScroll"; = parentTf; = ; = new Vector3(1, 1, 1); // = ; tf = ; scroll = <AdvertisementScroll>(); } else { scroll = <AdvertisementScroll>(); } = data; = tf; = tf; (createToggle); } } } }
Step 3:use. I won’t talk about the production method of the prefabricated body. It will be easy to understand the code and will also attach engineering files later. When you need to use billboard components in any interface, you only need to set up the data first, and then call the StartAdvertisementScroll method in the AdvertisementScrollManager.
using ; 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 = ("Panel/Container/First"); _secondMoveGo = ("Panel/Container/Second"); _container = ("Panel/Container"); _toggleContainer = ("Panel/ToggleContainer"); _spriteItem = ("Panel/Container/First/Item"); _toggleItem = ("Panel/ToggleContainer/ToggleItem"); OnRefreshData(); } void OnRefreshData() { data = CreateAdvertisementScrollData(); (_container,data,); } private CreateAdvertisementScrollData() { data = new (); //Set the number of pictures displayed and the time interval for sliding = 10; = 3; //Set the location information of the picture = _firstMoveGo; = _secondMoveGo; = _spriteItem; = 884; = new string[] { "1", "2", "3" }; = ; = new Vector3(800, 0, 0); //Set the paging label information (if you do not need paging labels, you can do not need to assign values) = true; = _toggleItem; = _toggleContainer; = new Vector3(0,-200,0); = 30; return data; } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.