Unity3D achieves the bullet screen effect, unity3d bullet Screen

Source: Internet
Author: User

Unity3D achieves the bullet screen effect, unity3d bullet Screen

Sun Guangdong 2015.6.15

For funny games that have no choice but to make everyone vomit, the function of bullet screens is excellent.

The current implementation of U5 and UGUI is simple.

/// <Summary> /// implement the bullet screen effect when watching the video /// </summary> public class DanMu: MonoBehaviour {// public Text [] texts; public GameObject textPrefab; [Tooltip ("number of full screens")] public int num; private Queue <GameObject> Texts = new Queue <GameObject> (); private bool isDanmuOn = true; private IEnumerator coroutine; // Use this for initialization void Start () {for (int I = 0; I <num; I ++) {GameObject obj = Instantiate (tex TPrefab, transform. position, Quaternion. identity) as GameObject; obj. setActive (false); obj. transform. setParent (transform); obj. transform. localScale = Vector3.one; Texts. enqueue (obj);} coroutine = DanmuAnimation (); StartCoroutine (coroutine);} private IEnumerator DanmuAnimation () {while (true) {GameObject obj = Texts. dequeue (); if (obj) {obj. transform. localPosition = new Vector3 (1071, Random. ra Nge (-512,512); obj. getComponent <Text> (). text = DanMuStrings [Random. range (0, DanMuStrings. length)]; obj. getComponent <Text> (). color = TextColors [Random. range (0, TextColors. length)]; obj. setActive (true); obj. getComponent <DanMuText> (). reset (Texts);} yield return new WaitForSeconds (0.2f );}} /// <summary> /// the on/off of the bullet screen /// </summary> public void DanMuToggle (Toggle toggle) {if (toggle. isOn) {isDanmuOn = True; StartCoroutine (coroutine); MyDebugLog. Log ("off-screen !!!!! ");} Else {isDanmuOn = false; DOTween. killAll (); StopCoroutine (coroutine); // Texts. clear (); foreach (var text in transform. getComponentsInChildren <DanMuText> () // only the activated objects can be obtained, but not all of them? Therefore, you do not need to Clear () to directly join the queues. {Texts. enqueue (text. gameObject); text. gameObject. setActive (false);} MyDebugLog. log ("Bullet screen off !!!!! Count: "+ Texts. Count);} [HideInInspector] public string [] DanMuStrings = {" this story is too shocking! "," It's still a Hollywood movie classic, this is too many times "," it's the hero of an electronic saw, Nima "," is this game still very conscientious? "," it's still costly to lie in the slot, this is too difficult. "," This game is amazing. "," it's all silly. "," ask: 13122785566 "," it's so lonely recently. It's still a good game ", "Can I still renew my game?", "License: 010-888888", "Why is the heroine not dead? "," Hao Shuai, you sissy "," Ah, I don't know anyone in Northeast China. "," Who am I going to? Please use civilized language ", "This is good.", "It would be better if my chest is bigger.", "This game must be on top." Why didn't I have any role in Japanese action love movies? "," Okay, this is also drunk! "," He just wants to be a quiet handsome man! "}; Public Color [] TextColors ;}


And the script hanging on the Text premade body:

    /// <summary>    ///     /// </summary>    public class DanMuText : MonoBehaviour    {        public void Reset(Queue<GameObject> Texts)        {            transform.DOLocalMoveX(-1366, 4f).OnComplete(() =>            {                gameObject.SetActive(false);                Texts.Enqueue(gameObject);            });        }    }





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.