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;
}
}
直接挂载上去即可。
本文仅做个人记录使用,如有问题,请及时联系!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!