Unity笔记之调节text的字间距

需求:unity内置的调节间距的只有行距,无法调节字的间距,只能通过代码来实现调节字间距的效果。
内容我是百度别人的拿来直接用的,附一下大佬的地址

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Playables;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace ZXL
{
    /// <summary>
    /// 对话系统
    /// </summary>
    public class DialogueSystem : MonoBehaviour
    {
        public Sprite[] sprites;

        public Image img_HeadPortrait;
        public Text txt_Context;

        public string path;//文件路径

        List<string> imgList;
        List<string> txtList;

        private int index;
        public float logSpeed;//打印速度

        private bool isLoging;//打印中
        private bool isLog;//直接打印


        private void Awake()
        {
            imgList = new List<string>();
            txtList = new List<string>();

            index = 0;
            isLoging = false;
            isLog = true;

            path = Application.streamingAssetsPath + "/Load/" + path;

            //Analysis(path);
            //StartCoroutine(Send(path));
            isFirst = true;

            //sprites[0] = Resources.Load<Sprite>("/sprite/老板头像_03");
            //sprites[1] = Resources.Load<Sprite>("/sprite/小白头像_03");
            //sprites[2] = Resources.Load<Sprite>("/sprite/小精灵头像_03");
        }
        private void OnEnable()
        {
            //index = 0;
            //isLoging = false;
            //PlayDialogue(path);
            StartCoroutine(Send(path));
        }
        bool isFirst;
        //private void Update()
        //{
        //    //if (Input.GetKeyDown(KeyCode.Q))
        //    //{
        //    //    //if (!isLoging && !isLog)
        //    //    //    StartCoroutine(SS());
        //    //    //else if (!isLog && isLoging)
        //    //    //    isLog = !isLog;

        //    //    PlayDialogue();
        //    //}
        //    Debug.Log(isTrue);
        //}
        public void PlayDialogue()
        {
            //index = 0;
            //isLoging = false;
            //isLog = true;

            //this.path = path;
            StartCoroutine(SS());
        }

        /// <summary>
        /// 解析
        /// </summary>
        public void Analysis(string path)
        {
            string[] s = File.ReadAllLines(path);
            for (int i = 0; i < s.Length; i++)
            {
                if (i % 2 == 0) imgList.Add(s[i]);
                else txtList.Add(s[i]);
            }
        }
        private bool isTrue;
        IEnumerator Send(string url)
        {
            UnityWebRequest request = UnityWebRequest.Get(url);
            yield return request.SendWebRequest();

            if (request.isHttpError || request.isNetworkError)
            {
                UnityEngine.Debug.Log(request.error);
            }
            else
            {
                UnityEngine.Debug.Log(request.downloadHandler.text);

                string[] s = request.downloadHandler.text.Split('\n');
                string[] ss = new string[s.Length];
                for (int i = 0; i < ss.Length; i++)
                {
                    //Debug.Log(ss[i]);
                    ss[i] = s[i].Replace("\r", "");
                }

                for (int i = 0; i < ss.Length; i++)
                {
                    if (i % 2 == 0) imgList.Add(ss[i]);
                    else txtList.Add(ss[i]);
                }
                isTrue = true;
            }
        }

        public void BedRoomOnly()
        {
            StopAllCoroutines();
            OverallSituation.instance.audioManager.StopAudio();
            img_HeadPortrait.gameObject.SetActive(false);
            //OverallSituation.instance.audioManager.PlayAudio(num);//音频还未给,后面更改索引
            txt_Context.text = "";
        }

        IEnumerator SS()
        {
            yield return isTrue;
            //Debug.Log("imgList.Count" + imgList.Count + "+" + "index" + index);

            if (index >= imgList.Count)
            {
                Debug.Log("退出");
                //OverallSituation.instance.mainCanvas.shiYanModelChooseButton.SetActive(true);
                yield break;
            }
            if (isFirst)
            {
                OverallSituation.instance.audioManager.PlayAudio(num);
                isFirst = false;
            }
            //Debug.Log(index);

            isLoging = true;

            txt_Context.text = "";
            //char[] c = txtList[index].ToCharArray();
            switch (imgList[index])
            {
                case "老板":
                    img_HeadPortrait.sprite = sprites[0];
                    //img_HeadPortrait.color = Color.green;
                    break;
                case "小白":
                    img_HeadPortrait.sprite = sprites[1];
                    //img_HeadPortrait.color = Color.red;
                    break;
                case "精灵":
                    img_HeadPortrait.sprite = sprites[2];
                    //img_HeadPortrait.color = Color.blue;
                    break;
                default:
                    break;
            }

            //int i = 0;

            //while (!isLog && i < c.Length - 1)
            //{
            //    txt_Context.text += c[i];
            //    i++;
            //    yield return new WaitForSeconds(logSpeed);
            //}
            //if (isLog) 
            txt_Context.text = txtList[index];

            Scene scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
            if (scene.name == "Company0")
            {
                if (index == 0)
                    yield return new WaitForSeconds(1f);//1
                else if (index == 1)
                    yield return new WaitForSeconds(3f);//4
                else if (index == 2)
                    yield return new WaitForSeconds(2.5f);//6.5
                else if (index == 3)
                    yield return new WaitForSeconds(2.5f);//9
                else if (index == 4)
                    yield return new WaitForSeconds(4f);//13
                else if (index == 5)
                    yield return new WaitForSeconds(1f);//12.20
            }
            else if (scene.name == "StudyAndBedRoom0")
            {
                if (index == 0)
                    yield return new WaitForSeconds(2f);
                else if (index == 1)
                    yield return new WaitForSeconds(2.5f);
                else if (index == 2)
                    yield return new WaitForSeconds(3.5f);
            }
            else if (scene.name == "MengJingDaTing0")
            {
                if (index == 0)
                    yield return new WaitForSeconds(2f);//2
                else if (index == 1)
                    yield return new WaitForSeconds(2.5f);//4.5
                else if (index == 2)
                    yield return new WaitForSeconds(1.5f);//6
                else if (index == 3)
                    yield return new WaitForSeconds(3f);//9
                else if (index == 4)
                    yield return new WaitForSeconds(5f);//14
                else if (index == 5)
                    yield return new WaitForSeconds(1.5f);//15.5
                else if (index == 6)
                    yield return new WaitForSeconds(5.5f);//21
            }
            else if (scene.name == "Company1")
            {
                if (index == 0)
                    yield return new WaitForSeconds(1f);
                else if (index == 1)
                    yield return new WaitForSeconds(2f);
                else if (index == 2)
                    yield return new WaitForSeconds(1.5f);
            }

            isLog = false;
            isLoging = false;
            index++;

            OverallSituation.instance.mainCanvas.num_Dialog++;
            PlayDialogue();
        }
        public int num;
    }
}

直接挂载上去即可。
本文仅做个人记录使用,如有问题,请及时联系!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值