2021年9月30日 星期四

Unity UI 基本跑馬燈

 文字跑馬燈,所先要了解,怎麼顯示模式。

大致上作法 可以用兩種方式來使用。

  • 時間方式計算
  • 數字累加

本文採取第二種數字累加的方式來呈現。

邏輯:   數字: 1 代表1個字,表示說 數字累加的方式來顯示文字數量。

-------------------------------------------

本文都採取使用C# 方式來進行編排,不太用到Unity設計界面來操作設定,

但是要先建立需要使用的遊戲物件即可。此篇介面採取比例 (16:9)

使用的遊戲物件:

  • Canvas (Unity UI)
  • Text (Unity UI)
  • EventSystem (Unity UI)
  • Camera
完成結果後

image























-----------------------------------

程式碼部分:   

腳本名稱: InterfaceObj

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 介面
public class InterfaceObj
{
    // image Obj 
    public Image imageObj(Image image, Color color , float pointX, float pointY, float sizeX, float sizeY)
    {
        image.transform.position = new Vector2(Screen.width / 2 * pointX, Screen.height / 2 * pointY);
        image.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);

        image.color = color;

        return image;
    }

    // text Obj
    public Text textObj(Text text, string message, FontStyle fontStyle, Color color ,int textSize, float pointX, float pointY, float sizeX, float sizeY)
    {
        text.text = message;
        text.fontStyle = fontStyle;
        text.color = color;
        text.fontSize = textSize;

        text.transform.position = new Vector2(Screen.width / 2 * pointX, Screen.height / 2 * pointY);
        text.rectTransform.sizeDelta = new Vector2(Screen.width / 2 * sizeX, Screen.height / 2 * sizeY);

        return text;
    }
}

// 介面效果類別
public class interfaceAfterEffects
{
    /*--------------------------
        * 文字效果
    --------------------------*/

    private float marquee_startTime = 0;  // 跑馬燈開始時間

    // 文字跑馬燈
    public void textEffects(TextEffects effects, Text text, string message, float speed, bool start)
    {
     text.text = text_Marquee(message, speed, start);   
    }

    // 跑馬燈
    private string text_Marquee(string message, float speed, bool start)
    {
        return message.Substring(0, (int)text_marquee_timeData(message.Length, speed, start));
    }

    // 計數器(跑馬燈顯示)
    private float text_marquee_timeData(float textSize, float speed, bool start)
    {
        float time = (marquee_startTime < textSize && start.Equals(true)) ? marquee_startTime += speed :
            (start.Equals(false)) ? marquee_startTime = 0 : marquee_startTime = textSize;

        return time;
    }
}

腳本名稱: UI_Layout

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_Layout : MonoBehaviour
{
    InterfaceObj interfaceObj = new InterfaceObj();

    // 文字訊息(系統)
    public void system_mesage_text(Text cameraModel_Text, Text recording_Text, Text InternalLab_Text, Text section8_Text, Text scanning_Text, Text activated, float pointX, float pointY, float sizeX, float sizeY)
    {
        interfaceObj.textObj(cameraModel_Text, "CAMERA 05s", FontStyle.Normal, new Color(0, 0.9f, 1, 1), (Screen.width / 2) / 10, 0.43f, 1.7f, 0.66f, 0.21f);
        interfaceObj.textObj(recording_Text, "REC.", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 16, 0.19f, 1.4f, 0.15f, 0.13f);
        interfaceObj.textObj(InternalLab_Text, "INTERNAL LAB 13", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 27, 0.55f, 1.44f, 0.32f, 0.1f);
        interfaceObj.textObj(section8_Text, "SECTION 8 - LEVEL 06", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 29, 0.58f, 1.33f, 0.38f, 0.08f);
        interfaceObj.textObj(scanning_Text, "SCANNING", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 12, 0.34f, 0.85f, 0.47f, 0.18f);
        interfaceObj.textObj(activated, "SYSTEM ACTIVATED", FontStyle.Normal, new Color(1, 1, 1, 1), (Screen.width / 2) / 18, 0.4f, 0.7f, 0.6f, 0.12f);
    }
}

腳本名稱: Layout_Run

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Layout_Run : UI_Layout
{
    public Text cameraModel_Text;
    public Text recording_Text;
    public Text internalLab_Text;
    public Text section8_Text;
    public Text scanning_Text;
    public Text activated_Text;

    // 跑馬燈
    interfaceAfterEffects scanning_Text_marquess = new interfaceAfterEffects();
    interfaceAfterEffects activated_Text_marquess = new interfaceAfterEffects();

    public bool sw = false;

    private void Update()
    {
        systemLayoutMessge();

        if (Input.GetKeyDown(KeyCode.W))
            sw = !sw;
    }

    private void systemLayoutMessge()
    {
   
        system_mesage_text(cameraModel_Text, recording_Text, internalLab_Text, section8_Text, scanning_Text, activated_Text, pointX, pointY, sizeX, sizeY);

        // 文字跑馬燈
        scanning_Text_marquess.textEffects(scanning_Text, "SCANNING", 0.6f, sw);
        activated_Text_marquess.textEffects(activated_Text, "SYSTEM ACTIVATED", 0.4f, sw);

    }
}