Author Topic: Interface and widgets created dynamically (on the fly)  (Read 2807 times)

vangorra

  • Guest
Interface and widgets created dynamically (on the fly)
« on: May 28, 2013, 02:27:22 PM »
The nature of my project that a UI is created dynamically at runtime. Can somebody provide me with a working example on how to create an interface programmatically? I've dug around the UICreateWidgetWizard. It is helpful but really only applies to the unity interface.

In the example below, I'm trying to place a simple label on the screen. When running the code in Unity, I can see the Label getting created and added the the main GameObject however, nothing is actually displayed on the screen. Any ideas?

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NewBehaviourScript : MonoBehaviour {
  5.  
  6.         // Use this for initialization
  7.         void Start () {
  8.                 UILabel lbl = NGUITools.AddWidget<UILabel>(this.gameObject);
  9.                 lbl.text = "Hello world!";
  10.         }
  11.        
  12.         // Update is called once per frame
  13.         void Update () {
  14.        
  15.         }
  16. }
  17.  

vangorra

  • Guest
Re: Interface and widgets created dynamically (on the fly)
« Reply #1 on: May 29, 2013, 11:42:06 PM »
Bump. Absence in replies leads me to believe that this is not easily doable. I would really like to use NGUI instead of writing my own toolkit. Could somebody help?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Interface and widgets created dynamically (on the fly)
« Reply #2 on: May 30, 2013, 12:28:08 AM »
In order for a UILabel to be displayed, you must be trying to perform NGUITools.AddWidget on a game object that's on a layer that's visible by a UI camera.

This means if you simply create a game object in the middle of nowhere and add that script to it, your widget will be massive in scale (as it won't belong to the UIRoot), and it may or may not be visible, depending on what sees that game object.

vangorra

  • Guest
Re: Interface and widgets created dynamically (on the fly)
« Reply #3 on: May 30, 2013, 08:24:15 AM »
Thank you for the reply.

I see what you mean now.