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

打算这周都要搞(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);

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值