Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Visores · Feb 10, 2014 at 03:41 PM · c#runtimeguitext

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 10, 2014 at 04:21 PM 0
Share
 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.

avatar image Visores · Feb 10, 2014 at 04:27 PM 0
Share

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)

avatar image robertbu · Feb 10, 2014 at 04:33 PM 0
Share

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";
     }
 }
avatar image Visores · Feb 10, 2014 at 04:41 PM 0
Share

Thank you very much!! The key was

 go.addComponent<GUIText>();

:) have a great day!

avatar image
0

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!");
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Visores · Feb 10, 2014 at 04:17 PM 0
Share

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.

avatar image DajBuzi · Feb 10, 2014 at 04:22 PM 0
Share

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 :)

avatar image Visores · Feb 10, 2014 at 04:24 PM 0
Share

ah yeah cool but ty anyway :)

avatar image
0

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 );

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges