- Home /
generate GUIText at runtime C#
Hello,
I want automaticly generate with my script a GUIText. At the moment i have a script that work, but you need to add a GuiText with drag and drop at the Inspector.
THE SCRIPT:
using UnityEngine;
using System.Collections;
public class hover : MonoBehaviour
{
public Vector3 offset;
Camera cam;
public string mytext;
public GUIText myGuiText;
void Start ()
{
myGuiText.text = mytext;
myGuiText.enabled = false;
cam = Camera.main;
}
void Update ()
{
myGuiText.transform.position = cam.WorldToViewportPoint (transform.position + offset);
}
void OnMouseEnter ()
{
myGuiText.enabled = true;
}
void OnMouseExit ()
{
myGuiText.enabled = false;
}
}
I need this script to run without using "public GUIText myGuiText".
Sry if there is somewhere a solution... I found a lot of similar ones. But I dont get it to fix, so it work with my script.
thy for help... I hope.
Answer by robertbu · Feb 10, 2014 at 04:03 PM
You can use AddComponent(). This code creates a new game object, add a GUIText component, centers the text on the screen (because GUIText uses Viewport coordiantes), and sets the text:
#pragma strict
function Start() {
var go = new GameObject();
go.AddComponent(GUIText);
go.transform.position = Vector3(0.5,0.5,0.0);
go.guiText.text = "Hello World";
}
You can add the component to an existing empty game object, but remember that GUITexts use Viewport coordinates, not world coordinates.
But my script shows the text in the middle of the object in the 3D space
If you are using GUIText and generating it automatically, you will need to do a Camera.WorldToViewportPoint() to correctly place the GUIText.
can you show me the same with C# source code? :D I understand what you do, but I dont get it in C# (I failed at this many times befor i created this question)
Here is the C# version. Attach it to any game object.
using UnityEngine;
using System.Collections;
public class CreateGUIText : $$anonymous$$onoBehaviour {
void Start() {
GameObject go = new GameObject();
go.AddComponent<GUIText>();
go.transform.position = new Vector3(0.5f,0.5f,0.0f);
go.guiText.text = "Hello World";
}
}
Thank you very much!! The key was
go.addComponent<GUIText>();
:) have a great day!
Answer by DajBuzi · Feb 10, 2014 at 04:01 PM
http://docs.unity3d.com/Documentation/ScriptReference/GUI.Label.html
private bool displayLabel;
void OnMouseEnter(){
displayLabel = true;
}
void OnMouseExit(){
displayLabel = false;
}
void OnGUI(){
if(displayLabel){
GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
}
}
Thy for answer, it works but not the way i want. Now the Label has fix positon in top,left cornor. But my script shows the text in the middle of the object in the 3D space. And at Rect you only can put 2 koordinates.
I didnt know that you've wanted this to be displayed in 3D space, my bad. But Robertbu gaved you an "3D" example, hope it helps :)
Answer by Xelnath · Apr 08, 2015 at 12:50 AM
Alternatively:
GameObject g = new GameObject();
Canvas canvas = g.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
CanvasScaler cs = g.AddComponent<CanvasScaler>();
cs.scaleFactor = 10.0f;
cs.dynamicPixelsPerUnit = 10f;
GraphicRaycaster gr = g.AddComponent<GraphicRaycaster>();
g.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 3.0f);
g.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 3.0f);
GameObject g2 = new GameObject();
g2.name = "Text";
g2.transform.parent = g.transform;
Text t = g2.AddComponent<Text>();
g2.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 3.0f);
g2.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 3.0f);
t.alignment = TextAnchor.MiddleCenter;
t.horizontalOverflow = HorizontalWrapMode.Overflow;
t.verticalOverflow = VerticalWrapMode.Overflow;
Font ArialFont = (Font)Resources.GetBuiltinResource (typeof(Font), "Arial.ttf");
t.font = ArialFont;
t.fontSize = 7;
t.text = "Test";
t.enabled = true;
t.color = Color.black;
g.name = "Text Label";
bool bWorldPosition = false;
g.GetComponent<RectTransform>().SetParent(this.transform, bWorldPosition);
g.transform.localPosition = new Vector3(0f, fTextLabelHeight, 0f);
g.transform.localScale = new Vector3(
1.0f / this.transform.localScale.x * 0.1f,
1.0f / this.transform.localScale.y * 0.1f,
1.0f / this.transform.localScale.z * 0.1f );
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Why is my OnCollisonEnter not working? 1 Answer
White screen on windows phoen8 app after unity splash 0 Answers