- Home /
Changing GUIText.font on runtime
So i realy try hard to find a solution for this and dont understand why this failure happens.
I creating a GUIText and add it to a GameObjekt.
On each Update i calculate the distance between the GO and the camera, and based to this distance i change my font of the GUIText.
Im using 10 different and added all to a List
GameObject label = new GameObject("label"); // Gui Text GUIText guiT = label.AddComponent<GUIText>(); // I stored all my Fonts in a List in another class so do not // wonder where it got instantiatet guiT.font = FontAssets.univerRoman[0]; guiT.material.color = new Color(1.0f, 1.0f, 1.0f); guiT.text = "name";
void Update() { int distanceToCamera = Vector3.Distance(label.transform.position,cam.transform.position);
int scale = Mathf.FloorToInt((99 - _distanceToCamera)/10);
guiT.font = FontAssets.univerRoman[scale];
}
All my text got realy messed up, and it looks like the size of the texture the text is rendered on is the wrong size.
EDIT: So after spending several hours with no real result i tryed 3dtext, and instantiate it in this way.
GameObject _label3d = new GameObject("label3d"); TextMesh _guiT3D = _label3d.AddComponent<TextMesh>(); MeshRenderer _ren = _label3d.AddComponent<MeshRenderer>();
_label3d.renderer.material = FontAssets.refFontAssets.univerRoman_22_mat;
_guiT3D.text = _name; _guiT3D.font = FontAssets.refFontAssets.univerRoman_22; _guiT3D.font.material = FontAssets.refFontAssets.univerRoman_22_mat; _guiT3D.renderer.material = FontAssets.refFontAssets.univerRoman_22_mat;
_ren.material = FontAssets.refFontAssets.univerRoman_22_mat; _ren.material.color = Color.white;
And still i have the same result. The scale of the Text material does not fit tothe scale of the textSize.
Answer by Tetrad · Jun 28, 2010 at 11:25 PM
Your GUIText object also has a renderer in it which is going to need to be updated to the right material.
Although are you just making it smaller for things that are further away? Why not just use 3D text?
I think i will use pretty allot of them. Like i think at least 10000. Im not sure about the performance, but i believe rendering a 2d text is cheaper than a 3d text. Or do i think wrong?
Okay, if i try " _label.renderer.material = FontAssets.univerRoman[scale].material; " it says label does not have a renderer attached. If i try to attach one, Unity says a Renderer can not be attached. :/
@Case23: 2D text and 3D text are the same thing: they are both textures on quads. If anything, I'd expect 3D text to be slightly cheaper. In either case, 10000 of them will kill any computer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Are Real Vector Fonts Possible in Unity? 3 Answers
Changing the font size of GUIText (on Unity iPhone)? 1 Answer
Unity3d font with strikethrough 2 Answers
How to display Greek alphabets 0 Answers
Font Scaling Issues 2 Answers