unity---脚本创建文本


脚本创建文本

新建文件夹 Resources

方便引用字体

image

在文件Resources中新建Fonts,并且下载一个ttf字体

没有字体,文本内容无法显示

image

脚本如下

image

 public GameObject objprent;
public string objName;

void Start()
{
    
    GameObject obj =new GameObject("文本");
    obj.transform.SetParent(objprent.transform,false);
    Text text =  obj.AddComponent<Text>();
    text.text="这是你创建出来的"+objName;
    obj.transform.Translate(new Vector2(0f,200f));
    text.font=Resources.Load("Fonts/123456",typeof(Font)) as Font;

}

拖拽一下,确认父物体

image

结果

image
image


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM