2017年11月4日 星期六

UI 簡單的跑馬燈

看到電影有一個字一個字在UI顯示,自己做了一個簡單的程式來玩玩 :)
------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;

public class Layout_UI : MonoBehaviour {

    public bool marquee_start; // 跑馬燈開關
    private float marquee_startTime = 0;  // 跑馬燈時間
    public Text text;
 
 // Update is called once per frame
 void Update () {
         
         if (Input.GetKeyDown(KeyCode.W))
            sw = !sw;
       marquee_start = sw;
       
       text.text = system_message_text_Marquee("跑馬燈", 0.4f);
    }

    // 跑馬燈
    private string system_message_text_Marquee(string message, float speed)
    {
        return message.Substring(0, (int)timeData(message.Length, speed));
    }


    // 計數器(跑馬燈用)
    private float timeData(float textSize, float speed)
    {
        float time = (marquee_startTime < textSize && marquee_start.Equals(true)) ? marquee_startTime += speed :
            (marquee_start.Equals(false)) ? marquee_startTime = 0 : marquee_startTime = textSize;
        return time;
    }

}

沒有留言:

張貼留言