Search Unity

Unity UI (4.6) Creating elements at runtime with LayoutGroups

Discussion in 'UGUI & TextMesh Pro' started by keiranlovett, Nov 24, 2014.

  1. keiranlovett

    keiranlovett

    Joined:
    May 9, 2013
    Posts:
    16
    I have an issue regarding generating Unity UI elements at runtime. I can generate them fine, but when trying to create LayoutGroups with LayoutElements at runtime via C# the elements don't seem to register that they're part of a LayoutGroup until an Update() or if I manually nudge them in the editor.

    Is there an issue in setting up I've missed, or is this a bug. It seems like it could be a bug since everything adjusts itself when I play with any variable or setting in the editor window or adjust position.

    Here's some of the relative code:

    //Generate Horizontal Group. Name - the gameobjects name.
    public static RectTransform BeginHorizontal(string name = "HorizontalLayoutGroup") {
    positionCache.Push(currentPosition.x);
    modeCache.Push(Mode.Horizontal);
    // RectTransform t = go.AddComponent<RectTransform>();

    RectTransform t = BeginSubControl(name);
    HorizontalLayoutGroup lg = t.gameObject.AddComponent<HorizontalLayoutGroup>();
    lg.padding = new RectOffset(defaultMargin, defaultMargin, defaultMargin, defaultMargin);
    lg.spacing = defaultMargin;
    lg.childForceExpandWidth = true;
    lg.childForceExpandHeight = false;

    return t;
    }
    //Terminate BeginHorizontal component
    public static void EndHorizontal() {
    currentPosition.x = positionCache.Pop();
    modeCache.Pop();
    EndSubControl();
    }

    public static RectTransform BeginSubControl(string name = "SubGroup") {
    GameObject go = new GameObject(name);
    RectTransform t = go.AddComponent<RectTransform>();
    Rect tr = t.rect;
    if (subControls.Count > 0) {
    RectTransform temp = subControls.Peek();
    t.SetParent(temp, false);
    }
    subControls.Push(t);
    t.anchoredPosition = Vector2.zero;
    t.anchorMin = new Vector2(0, 0);
    t.anchorMax = new Vector2(1, 1);

    t.offsetMax = new Vector2(0, 0);
    t.offsetMin = new Vector2(0, 0);
    t.pivot = currentPosition;

    return t;
    }

    public static void EndSubControl() {
    subControls.Pop();
    }

    public static void AddChild(RectTransform trans) {
    trans.parent = subControls.Peek();
    //trans.anchoredPosition = currentPosition;
    }

    //GUI.Button(new Rect(10, 10, 50, 50), btnTexture)
    public static Text Label(string text, string name = "Text") {
    GameObject go = new GameObject(name);
    Text t = go.AddComponent<Text>();
    t.font = Resources.FindObjectsOfTypeAll<Font>()[0];
    t.text = text;
    AddChild(t.rectTransform);
    return t;
    }

    public static void Button(string text, string customFont = "Arial", string name = "Button") {

    GameObject go = new GameObject(name);
    Image i = go.AddComponent<Image>();

    Button b = go.AddComponent<Button>();
    b.targetGraphic = i;

    //Creates a secondary gameObject which is parented to go, this will hole the text label - since its a little different to set up we can't reuse Label()
    GameObject go2 = new GameObject(name + "- label");
    go2.transform.parent = go.transform;

    Text t = go2.AddComponent<Text>();
    t.text = text;
    t.rectTransform.anchoredPosition = new Vector2(.5f, .5f);
    t.font = Resources.FindObjectsOfTypeAll<Font>()[0];
    t.fontSize = 20;
    t.color = Color.red;
    t.alignment = TextAnchor.MiddleCenter;

    LayoutElement le = go.AddComponent<LayoutElement>();
    le.minHeight = defaultBtnSize;

    //Button created, lets add it to the canvas objects
    AddChild((RectTransform)go.transform);

    // http://gamedev.stackexchange.com/qu...y-change-the-functions-called-by-gui-elements
    b.onClick.AddListener(delegate {
    Debug.Log("Button " + b.name + " has been clicked!");
    });
    }​




    Then executed here

    void Start () {

    root = GUIHelper.BeginSubControl();
    root.SetParent(this.GetComponent<RectTransform>().transform, false);


    GUIHelper.BeginHorizontal();

    GUIHelper.Button("My Button");
    GUIHelper.Button("My Button");

    GUIHelper.EndHorizontal();

    GUIHelper.EndSubControl();

    }​
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892