Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Creating new gameobject and setting parent it to a UI

Discussion in 'Scripting' started by frankiwinnie, Feb 4, 2023.

  1. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Hello everyone,

    I create an empty gameobject in code like:
    Code (CSharp):
    1. GameObject placeHolder = new GameObject();
    2. placeHolder.AddComponent<LayoutElement>();
    3.  
    so basically, I want to set parent the placeHolder gameobject to a UI panel which contains a horizontal layout group:

    Code (CSharp):
    1. placeHolder.transform.SetParent(panel.transform);
    2. placeHolder.transform.SetAsLastSibling();
    When I set parent the game object I Debug.Log the local position of the placeholder however I am getting odd values. It seems that the placeHolder does not affected by horizontal layout group. If I Debug.Log the local position of other gameobjects in the panel their values appear to be true like (-200.00, 0.00, 0.00) or (-100.00, 0.00, 0.00) but the placeHolders position is not correct. Why can it be?

    Thanks,
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Manipulating UI stuff by code is really hard. It's always better to make example UI chunks in your scene using the Unity Editor, turn them off in your Start() method and then instantiate as many copies as you need at runtime, such as for dynamic numbers of UI elements.

    See enclosed example.

    Otherwise, if you absolutely insist on doing this in code, at a minimum you will need at least two things, probably several more that you will discover as things fail for various dimensional and anchoring reasons:

    - use the overload of .SetParent() that accepts a second argument regarding changing position

    - use the overload of new GameObject() that accepts a type and give it typeof(RectTransform)

    Doing UI by code is HARD. The UI source code might also give you some help. It is available on github.

    https://github.com/Unity-Technologies/uGUI
     

    Attached Files:

  3. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Thanks Kurt,

    The problem here is that it is a dynamic panel (like an inventory) so that the needed position in the panel may vary for numerous times. If I got you correctly preparing UI elements for different situations seemed a little bit too much work to me.

    After your information about difficulties concerning manipulation UI by code, I now stopped the empty gameobject approach and tried to spot the desired locations based on the existing UI elements in that panel like spotting the last sibling index and assigning a new vector2 based on the latest element in the panel considering the horizontal layout groups specifications. It seems a little "raw" approach but it works fine for now.

    Thanks for the reply.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    I assure you that it won't be less work than coding against the UnityEngine.UI API, in most cases.

    Glad you got something working. I always stick with making stuff in the editor FIRST, then duping it as needed.