Unity手游制作记-制作通用文本提示

xiaoxiao2021-02-28  139

打算这周都要搞(gao)事(shi)情(qing),所以呢,让我们先将游戏的基本模块做出来吧~

今天来做一下通用的文本显示。

文本提示

所谓文本提示,就是在屏幕上的某个位置,显示一段文字,给予玩家提示。

在Unity里文本还分为3dText和UGUI的Text。

功能

在这里,我们设置这样几个功能:

横幅显示提醒显示跟随式(3d世界)浮动式(3d世界)

横幅显示

using UnityEngine; using UnityEngine.UI; using System.Collections; public class AutoTextSize : MonoBehaviour { [SerializeField] float width = 1.0f; [SerializeField] float height = 1.5f; [SerializeField] int lineNumber = 10; // Use this for initialization void Start () { changeSize(); } public void changeSize() { string t = this.GetComponent<Text>().text; int length = t.Length; int high = t.Length / lineNumber + 1; float w; float h; w = Mathf.Min(length, lineNumber) * width; h = high * height; Vector2 vec = new Vector2(w, h); this.GetComponent<RectTransform>().sizeDelta = vec; } } using UnityEngine; using UnityEngine.UI; using System.Collections; [RequireComponent(typeof(RectTransform), typeof(AutoTextSize))] public class NoticeOfMove : MonoBehaviour { Text text; RectTransform rectTransform; [SerializeField] float speed = 500f; [SerializeField] float dist = 945f; ArrayList notice; Vector3 start; Vector3 now; float nowDist = 100000f; static NoticeOfMove ins; void Start () { text = this.GetComponentInChildren<Text>(); rectTransform = text.GetComponent<RectTransform>(); start = rectTransform.position; now = start; notice = new ArrayList(); gameObject.SetActive(false); ins = this; } public static NoticeOfMove getInstance() { return ins; } public void addNotice(string str) { notice.Add(str); gameObject.SetActive(true); } public void addNotice(string str, float speed) { notice.Add(str); this.speed = speed; gameObject.SetActive(true); } private void setText(string str) { gameObject.SetActive(true); text.text = str; nowDist = 0; } public void setDist(float dist) { this.dist = dist; } void FixedUpdate() { if (nowDist >= dist) { rectTransform.position = start; if (notice.ToArray().Length != 0) { setText(notice.ToArray()[0].ToString()); notice.RemoveAt(0); } else { gameObject.SetActive(false); } } else { now.x = start.x + nowDist; nowDist += speed * Time.fixedDeltaTime; rectTransform.position = now; } } } using UnityEngine; using UnityEngine.UI; using System.Collections; [RequireComponent(typeof(Canvas))] public class TextControl : MonoBehaviour { Canvas canvas; int width; int height; [SerializeField] NoticeOfMove notice; void Awake () { canvas = this.GetComponent<Canvas>(); width = Screen.width; height = Screen.height; if (notice == null) { Debug.LogError("未指定Notice"); } notice.setDist(width * 1.25f); } // 设置横幅型提示 public void addNotice(string str) { notice.addNotice(str); } void Update () { } } 效果(注意,这里是移动的)

提醒显示

using UnityEngine; using System.Collections; [RequireComponent(typeof(RectTransform))] public class AutoBeSmall : MonoBehaviour { RectTransform rec; float nowScale = 0.15f; bool flag = true; bool isStart = false; // Use this for initialization void Start () { isStart = true; rec = this.GetComponent<RectTransform>(); } // Update is called once per frame void FixedUpdate () { if (nowScale <= 1.5f && flag) { rec.localScale = new Vector3(nowScale, nowScale, nowScale); nowScale *= 1.05f; } else if (flag){ flag = false; rec.localScale = Vector3.one; } else if (nowScale >= 0.5f && !flag) { rec.localScale = new Vector3(nowScale, nowScale, nowScale); nowScale *= 0.95f; } else if (!flag) { this.gameObject.SetActive(false); } } public void setScale(float scale) { if (isStart) { this.gameObject.SetActive(true); nowScale = scale; rec.localScale = new Vector3(nowScale, nowScale, nowScale); flag = true; } } } using UnityEngine; using UnityEngine.UI; using System.Collections; [RequireComponent(typeof(Canvas))] public class TextControl : MonoBehaviour { [SerializeField] AutoBeSmall error; Text errorText; void Awake () { if (error == null) { Debug.LogError("未指定Error"); } errorText = error.GetComponent<Text>(); if (errorText == null) { Debug.LogError("Error指定错误,未找到Text脚本"); } } // 设置提示 public void setError(string str) { error.setScale(2.5f); errorText.text = str; } }

3d世界的Text

using UnityEngine; using System.Collections; // 此脚本用于自动销毁(悬浮字体等等) public class AutoDestroy : MonoBehaviour { float time; [SerializeField, Range(0.25f, 1.5f)] float autoDestroyTime = 1.0f; public void setMaxTime(float t) { autoDestroyTime = t; } // Update is called once per frame void FixedUpdate () { time += Time.fixedDeltaTime; if (time >= autoDestroyTime) { Destroy(this.gameObject); } } } using UnityEngine; using System.Collections; // 跟随这个物品离它diff的距离 public class AutoFlow : MonoBehaviour { private GameObject flow; private Vector3 diff = Vector3.zero; public void setFlowAndDiff(GameObject o, Vector3 pos) { flow = o; diff = pos; } // Update is called once per frame void FixedUpdate () { Vector3 pos = flow.transform.position; pos += diff; this.transform.position = pos; } } using UnityEngine; using UnityEngine.UI; using System.Collections; public class TextControl : MonoBehaviour { [SerializeField] TextMesh textOf3D; void Awake () { if (textOf3D == null) { Debug.LogError("未指定textOf3D(预设体)"); } } // 设置3dText public void set3dText(string str, Vector3 pos) { TextMesh t = (TextMesh)Instantiate(textOf3D, pos, Quaternion.identity); t.transform.SetParent(canvas.transform); t.text = str; } // 设置3dText(跟随) public void set3dText(string str, GameObject obj, Vector3 diff) { TextMesh t = (TextMesh)Instantiate(textOf3D, obj.transform.position, Quaternion.identity); t.transform.SetParent(canvas.transform); t.text = str; Destroy(t.gameObject.GetComponent<AutoDestroy>()); t.gameObject.AddComponent<AutoFlow>().setFlowAndDiff(obj, diff); } }

测试用例

textControl.addNotice("啦啦啦啦啦绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿绿"); textControl.setError("冲车冲车错"); textControl.set3dText("Body-(Lv.1)", GameObject.Find("Body"), Vector3.up * 2);

转载请注明原文地址: https://www.6miu.com/read-42343.html

最新回复(0)