Search Unity

How to work with nested UI prefabs in new Unity prefab workflow?

Discussion in 'Prefabs' started by radiowaves, Oct 15, 2019.

  1. radiowaves

    radiowaves

    Joined:
    Jan 20, 2017
    Posts:
    12
    I have a project that uses UI prefabs inside canvas, which have additional UI prefabs instantiated inside them (I call it subpanel). Sizes of the prefabs depend on nested prefabs. This was all created in old prefab enviornment where I had a prefab in the scene, I dragged instantiable prefab inside prefab and edited it, and deleting the nested prefab before applying. This way I could see the implications directly. When I want to change the contents of the subpanel, I put it inside the father prefab and edit it, applying it did not affect the father prefab in which it was nested to.


    Nested prefab looks like this:

    [canvas]
    [father prefab, size modified by child]
    [child prefab, size modified during play]
    [/father prefab]
    [/canvas]

    How do I edit the child UI prefab in new enviornment? When I open prefab asset, it is just stretched onto canvas which I cannot edit.

    I use version 2018.4.10f1, is this fixed in newer versions? (Too risky to update to newer versions at this stage) I am sorry if this is solved, there is a lot of information regarding new prefab mode and I haven't quite grasped it yet. If someone could point me to right direction, this would help a lot.

    Thanks
     
    Last edited: Oct 15, 2019
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Your old workflow should still mostly work (unless you need to delete or reorder GameObjects) but of course it might be a bit cumbersome.

    You can make a custom scene with a Canvas in and specify in Project Settings > Editor to use that for UI Prefabs. Opened UI Prefabs will still stretch to that Canvas (if they're set to stretch) but if you make the Canvas smaller, maybe it's less of an issue?

    In 2020.1 there will be a new feature called Prefab Mode in Context which should make it easier to edit UI Prefabs in Prefab Mode, since they will be placed in a parent that matches the size of the one the instance is in in the scene.
     
    phuwadon likes this.
  3. radiowaves

    radiowaves

    Joined:
    Jan 20, 2017
    Posts:
    12
    I do need to delete, reorder and create new stuff in a prefab.

    Can I set the UI Prefabs canvas to use a parent prefab?

    If I understand correctly, the 2020.1 feature will still not work, since prefab itself is resized depending of its content AND parent prefab, its size could actually be 0 in its standalone state, in which case canvas will be set to 0.

    Or, ok it matches the size of the parent, but I want to test it, how can I do it if in prefab mode canvas is not flexible. Remember I have child modifiyng the parent, testing it will require me to create and delete its contents. So each time I want to test the layout of parent prefab, I have to go inside child prefab and edit it (which contents is saved), then go back and after that I have to go back inside child prefab again and remove the modifications again...

    Here is an example:

    [canvas]
    [father prefab: minimum size is set, but is modified by child, maximum size is also set by canvas size, vertical layout group with content size fitter etc]
    [child prefab, size modified by child game objects which are created on the fly]
    [gameobject with vertical layout group etc]
    [child game object 1: layout element]
    [child game object 2: layout element]
    [child game object 3: layout element]
    etc...
    [/gameobject]
    [/child prefab]
    [/father prefab]
    [/canvas]

    [Child prefab] is reused in multiple prefabs, all in different sizes and modifications.
    [child game object: layout element] will actually give size to this child prefab, but there is none of those inside it in its default state (only switched off prototype child element, which will be cloned).

    This may complicate even further, having child prefab inside child prefab.
     
    Last edited: Oct 15, 2019
    AlanMattano likes this.
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    It's a bit hard to understand your entire setup, but you can test with 2020.1.0a5 and see for yourself if it works for your case: https://unity3d.com/unity/alpha/2020.1.0a5
    If it doesn't we'd be happy to get a bug report with your project and steps to observe the issue so we can look into whether we can address it better.