Search Unity

[Possible Bug] Dynamically create UI elements without having to rely on a prefab?

Discussion in 'UGUI & TextMesh Pro' started by vexe, Jan 22, 2015.

  1. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    I'm in a situation where I want the least amount of configuration to set things up, i.e. less drag-drops in inspector etc. I don't want to deal with prefabs in this instance.

    In my inventory I have this ActionMenu where depending on what item you have, you get different entries like Use, Consume, Examine, Combine, etc. - I'm trying to create those entries from code at runtime, but for some reason the Text is not showing up. If I run the same code in editor, it works just fine and the Text shows up.



    Here's my code

    Code (csharp):
    1.  
    2.      [Show] private void NewAction(string actionName)
    3.      {
    4.        var entryGo = new GameObject(actionName);
    5.        entryGo.transform.SetParent(transform, false);
    6.  
    7.        entryGo.AddComponent<Image>();
    8.        entryGo.AddComponent<Button>();
    9.  
    10.        var trigger = entryGo.AddComponent<PointerEventTrigger>();
    11.        trigger.Subscribe(PointerEventType.PointerClick, OnActionClickedHandler);
    12.  
    13.        var textGo = new GameObject("Text");
    14.        textGo.AddComponent<Text>().text = actionName;
    15.        textGo.transform.SetParent(entryGo.transform, false);
    16.  
    17.        var rect = entryGo.GetComponent<RectTransform>();
    18.        rect.SetHeight(entryHeight); // an extension method
    19.  
    20.        actionCount++;
    21.      }
    22.  
    Works fine if I use a prefab and instantiate it, but not when creating a new gameObject and adding the required components.

    Any ideas?

    Thanks.
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    vexe likes this.
  3. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    Awesome! Thanks! Will look into it. I poked around in ILSpy, couldn't find anything of interest. Missed that class
     
  4. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    Still no luck. Something's wrong here, even if I create the text (at runtime) from the menu item GameObject/UI/Text, it still doesn't show up! - While Image works fine, just Text.

    Can you give that a try? Just a new scene, create a canvas, hit play, and add a Text. Does it show up?

     
    Last edited: Jan 22, 2015
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Repeated your steps, worked perfectly fine for me :S

    Created a Canvas / Image / Text / Text / Image / Text

    I'm running 4.6.1 on Windows 8.1

    P.S>
    I Guess you know you can skip the Canvas step as adding any UI component will add a canvas :D
     
  6. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    Yeah I know about the canvas. Weird. Running 4.6.1 on Win 7 64-bit. You sure you added the Text in playmode?
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    No I didn't try that, but i did just notice something interesting. After trying to create Text the first time in play mode, it didn't render (nope not even the first one)
    After stopping the game and adding a Text component, it also DID NOT have text.
    Now here is the weird part. The FONT was null.

    After I set a font (Ariel) it worked fine, even in play mode.

    Bug me thinks, but something to keep in mind anyway
     
    vexe likes this.