Search Unity

Instantiate a ugui prefab at runtime ?

Discussion in 'UGUI & TextMesh Pro' started by sacha, Mar 19, 2015.

  1. sacha

    sacha

    Joined:
    Oct 29, 2012
    Posts:
    22
    I tried to instantiate a prefab during runtime using the 'usual' setparent method but it doesn't react the way i expected. Instead of having my popup centered or even showing on screen, it ended located out of the canvas space.

    What is the proper way to instantiate a uGui prefab ?

    thanks
    Code (CSharp):
    1.  
    2. private GameObject _mainPanel;
    3.         private GameObject _gameManager;
    4.  
    5.  
    6.         public void CreateModalWindowFromPrefab(object o) {
    7.             EqOkpanel msg = (EqOkpanel) o;
    8.  
    9.             GameObject go = Resources.Load(msg.Prefab,typeof(GameObject)) as GameObject;
    10.             go =GameObject.Instantiate(go);
    11.             go.transform.SetParent(_mainPanel.transform,true);
    12.             go.GetComponent<ModalHelper>().SetData(msg.Title,msg.Message);
    13.         }
     
  2. Feaver1968

    Feaver1968

    Joined:
    Nov 16, 2014
    Posts:
    70
    Here is a snippet where I'm loading a prefab toggle item for quality settings.
    Code (CSharp):
    1. public RectTransform containerQualitys;
    2. public GameObject prefabQualityToggle;
    3.  
    4. void AddItem ()
    5. {
    6.     GameObject addedChild = (GameObject)Instantiate(prefabQualityToggle);
    7.     addedChild.transform.SetParent(containerQualitys);
    8. }
     
  3. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    What I'm not seeing is you giving the prefab any position.

    Code (CSharp):
    1.  
    2.     public Image _Worker;
    3.     /// <summary>
    4.     /// Creates a worker at the position and for the team given.
    5.     /// </summary>
    6.     /// <param name="position">Position.</param>
    7.     /// <param name="team">Team.</param>
    8.     public void CreateWorker(Vector3 position, Teams team)
    9.     {      
    10.         // Create the gameobject
    11.         Image aImage = (Image) Instantiate(_Worker, position, Quaternion.identity);
    12.         aImage.transform.SetParent(_WorkerCanvas.transform, true);
    13.         aImage.GetComponent<WorkerAI>().Initialize(team);
    14.         aImage.rectTransform.sizeDelta = new Vector2(_BuildingSideLength / 3F, _BuildingSideLength / 3F);
    15.     }
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    That would depend on whether it actually needs a specific position. If the object in question happens to be inside a layout group, you don't need to set a position at all.
     
  5. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Ah, so what I've found is that making uGUI components prefabs breaks some of the relationships you'd expect them to keep. Maybe you need to re-add the instantiated uGui component to the layout group via code. Sorry, haven't done prefabs and layout coimponents, so it's just a thought.
     
  6. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Prefabs can't have references to other objects in the scene, if that's what you mean. Not specific to uGUI, though, that's the same for all types of prefabs.

    After instantiating your uGUI prefab, you would need to parent it to your desired parent object. If you don't do that, it will become a root object, which is most likely not what you want.
     
  7. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    If you do figure it out OP please post what it was, I was attempting to instantiate some buttons not long ago but gave up and ended up using another method to get the function completed, but I was running into similar problems when trying to instantiate uGui components from prefabs.
     
  8. rhythom

    rhythom

    Joined:
    Mar 10, 2015
    Posts:
    2
    you need call obj.GetComponent<RectTransform>.SetParent(parent.transform,false);
     
  9. sacha

    sacha

    Joined:
    Oct 29, 2012
    Posts:
    22
    no luck so far. The prefab is parented correctly now using GetComponent<RectTransform>.SetParent(parent.transform,false);

    but it appears completely destroyed: the background image is missing, the contents are outside its panel and the main anchor which is supposed to be centered is not taken in account...

     

    Attached Files:

    • oops.jpg
      oops.jpg
      File size:
      18.5 KB
      Views:
      1,722
  10. rhythom

    rhythom

    Joined:
    Mar 10, 2015
    Posts:
    2
    give me your unity runtime Hierarchy view
     
  11. sacha

    sacha

    Joined:
    Oct 29, 2012
    Posts:
    22
    Here is the runtime hierarchy...
    When i parent the prefab manually all is correct...
     

    Attached Files:

  12. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    What's wrong about that hierarchy, not being familiar with your project, I don't know? Can you show a picture of a correct one, so we can see what's missing?
     
  13. sacha

    sacha

    Joined:
    Oct 29, 2012
    Posts:
    22
    The problem is everything seems right but the result is not ! The popup appears in the bottom corner and its panel is 'crossed' like shown in a previous post.
     
  14. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Just a thought... have you tried this with "world space" set to true? In my UI project I had to switch to that so scaling would work on android machines.