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 /
avatar image
0
Question by SuperMasterBlasterLaser · Aug 31, 2014 at 10:48 AM · uiguieditordynamicunity4.6

Create UI elements dynamically on Unity 4.6

Hello everyone.

I'm so happy that new version of Unity has arrived. I watched tutorials and now we can create menu elements without OnGUI function.

However there is new question: can you create theese UI elements dynamically? For example:

  1. Inventory UI where it will show all your picked objects and you can manage them.

  2. Ask question game where server will download question and answer texts to show it on theese UI elements.

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

2 Replies

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

Answer by Scribe · Aug 31, 2014 at 11:32 AM

of course you can, just like any other component just use AddComponent, and then to change something you can use GetComponent and then get which bit you want and change it!

For most of the components (at least in C#) you will need to make sure to add using UnityEngine.UI; at the top, or it will not recognise the component names.

Component names are as you would expect:

  • RectTransform

  • CanvasRenderer

  • Shadow

  • Outline

  • PositionAsUV1

  • Image

  • RawImage

  • Text

  • Mask

  • Button

  • InputField

  • Scrollbar

  • Slider

  • Toggle

  • ToggleGroup

  • Selectable

For example:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class addText : MonoBehaviour {
     Text str;
     public Font myFont;
     void Start () {
         str = gameObject.AddComponent<Text>();
         str.text = "Hello World";
         str.font = myFont;
     }
 }


Scribe

Comment
Add comment · Show 5 · 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 Scribe · Aug 31, 2014 at 11:33 AM 0
Share

this will only work if you make it a child of a CanvasRenderer object, but you could also make one of those with the CanvasRenderer Component!

avatar image SuperMasterBlasterLaser · Aug 31, 2014 at 01:38 PM 0
Share

Thank you for answer!

avatar image Umai · Nov 28, 2014 at 05:13 AM 0
Share

Is this a working answer? I can't use this to instantiate a Button with a text label.

avatar image Scribe · Nov 30, 2014 at 03:19 PM 0
Share

@Umai It does work, make sure that the gameObject you are adding components to is a child of your CanvasRenderer, you may need to add a RectTransform component to a button yourself manually, I have not tried!

avatar image Kiwasi · Nov 30, 2014 at 06:17 PM 0
Share

Its worth adding here that UI elements are just regular GameObjects. You can make them up into prefabs and Instantiate just like you would with a regular object. Video tutorial here.

avatar image
1

Answer by Starwalker · Feb 04, 2016 at 05:31 PM

This is what worked for me, for anyone new to Procedural UI and banging your head everywhere... This works for uGUI. [Unity 5.3.1 version is when I wrote this code]

Requirement: Have a Panel (as a child of Canvas) and dynamically populated Text boxes inside it.

Procedure:

  1. I created the Panel on Canvas, hid it and put atleast one Text prefab as a child of the prefab and hid that too after making a prefab out of it (drag it to the project folder to make it a prefab). I would be activating the Panel and its children via gameObject.SetActive(true) when I need them.

  2. You have to put the code in either Start or Awake for sanity and static Instantiation.

    void Start () {

          // 17 is the number of Text UI objects I need inside the panel, I know the exact amount hence using a for loop.
     
             for(var i=0; i <= 17; i++ )
             {
                 // Instantiate it without rect and rotation parameters.
                 var tempTextBox = Instantiate(PanelText) as Text;
    
                 // Use code below to debug in case it doesn't instantiate for whatever reason.
     
                 //if (tempTextBox == null)
                 //{
                 //    Debug.Log(" Can't instantiate:  " + i);
                 //    return;
                 //}
     
                 tempTextBox.fontSize = 14; // Set font size if needed, else it will take from prefab.
                 //Set the text box's text element to the current textToDisplay:
                 tempTextBox.text = "Blank" + i;
                 tempTextBox.name = "Panel Text"; // Use this to easily locate this element in Hierarchy
     
                 //Parent to the panel, which is the original panel where prefab was made.
                 tempTextBox.transform.SetParent(Panel.transform, false); 
                 //Set the text box's text element font size and style:           
             }
         }
    
    
    
    
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

25 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 4.6 InputFields need help 1 Answer

UI Button and Color Tint Transition issue 0 Answers

GUI Editior Script not working in ( Unity 2019.x ) 0 Answers

ShaderGraph-like EditorWindow 0 Answers

How can I calculate whether an UI element is inside my camera view? 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