-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddComponent_TextMesh.cs
More file actions
37 lines (30 loc) · 1.14 KB
/
AddComponent_TextMesh.cs
File metadata and controls
37 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using UnityEngine;
// AddComponent_TextMesh script //
/* 26.04.2021 - Simplify code
* 25.04.2021 - Added script */
[RequireComponent(typeof(MeshRenderer))] // TesxtMesh needs MeshRenderer. Here I force its creation, but you can add it manually or make sure your gameobject has one.
public class AddComponent_TextMesh : MonoBehaviour
{
public string text = "My realtime text";
/* Tips :
* Decrease Character Size and
* increase FontSize to get a nice text render */
public float characterSize = 0.01f;
public int fontSize = 80;
private TextMesh textmesh;
private void Start()
{
// Add a TextMesh Component in Realtime on a gameobject
DisplayText(text, characterSize, fontSize);
}
// Add a TextMesh Component in Realtime on a gameobject
private void DisplayText(/*optional*/ string Text, float CharacterSize, int FontSize)
{
textmesh = transform.gameObject.AddComponent<TextMesh>();
// Optional
textmesh.anchor = TextAnchor.MiddleCenter;
textmesh.text = Text;
textmesh.characterSize = CharacterSize;
textmesh.fontSize = FontSize;
}
}