61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
public class Tools_object_UI : MonoBehaviour
|
|
{[HideInInspector]
|
|
public string UI_name;
|
|
public string sprite_name;
|
|
public TMPro.TMP_Text text;
|
|
public Image sprite;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
public void change()
|
|
{
|
|
text.text = UI_name;
|
|
string name = KeepChinese(UI_name);
|
|
Debug.Log(name);
|
|
if (Resources.Load("UI/" + name) != null)
|
|
{
|
|
Debug.Log(sprite_name);
|
|
Texture2D tubiao = Resources.Load("UI/"+ name) as Texture2D;
|
|
Debug.Log(tubiao);
|
|
Texture2D tubiao2 = Instantiate(tubiao) as Texture2D;
|
|
|
|
Sprite sprite3 = Sprite.Create(tubiao2, new Rect(0, 0, tubiao2.width, tubiao2.height), new Vector2(0.5f, 0.5f));
|
|
Debug.Log(sprite3);
|
|
sprite.GetComponent<Image>().sprite = sprite3;
|
|
}
|
|
}
|
|
public static string KeepChinese(string str)
|
|
{
|
|
//声明存储结果的字符串
|
|
string chineseString = "";
|
|
|
|
|
|
//将传入参数中的中文字符添加到结果字符串中
|
|
for (int i = 0; i < str.Length; i++)
|
|
{
|
|
if (str[i] >= 0x4E00 && str[i] <= 0x9FA5) //汉字
|
|
{
|
|
chineseString += str[i];
|
|
}
|
|
}
|
|
|
|
|
|
//返回保留中文的处理结果
|
|
return chineseString;
|
|
}
|
|
|
|
}
|