Search Unity

Generate scene in the Editor by code

Discussion in 'Scripting' started by Flash-Gordon, Mar 3, 2016.

  1. Flash-Gordon

    Flash-Gordon

    Joined:
    Sep 9, 2013
    Posts:
    6
    Hi, I have a simple script that creates a Grid of prefabs so I don't have to copy all the objects in the scene manually.
    By now the Objects only get created when the game starts.
    So i wondered if there is a Way to execute scripts in the editor to help me build a world fast and preview it directly.

    I imagine something like a component "create prefab grid" where I can set the rows and columns and it generates these prefabs directly in the scene instead of when the game starts.

    Is there a (easy) way to do this?
    Thanks
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    You could always make a prefab that holds the prefab grid as children. It should be easy enough to just drag the grid into the scene, then remove the children from the parent using the Hierarchy view and delete the parent.

    Of course, you could also leave them as children if motion isn't relevant, or disconnect them in Start() with scripting.
     
  3. dfsantos

    dfsantos

    Joined:
    Mar 3, 2016
    Posts:
    6
    If I'm not mistaken, you will find what you seek here or here.

    Maybe this can be useful.
     
    Last edited: Mar 3, 2016
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    These all have to do with modifying the interface. OP wants to add prefabs to a scene when not in runtime.
     
  5. Flash-Gordon

    Flash-Gordon

    Joined:
    Sep 9, 2013
    Posts:
    6
    Well I found out that it is possible to copy the generated prefabs while the game runs and past them afterwards into the hierarchy. This solution is not ideal but its way faster than adding everything by hand and changing the positions manualy.
     
  6. dfsantos

    dfsantos

    Joined:
    Mar 3, 2016
    Posts:
    6
    You can change the interface to add prefabs to a scene when not in runtime. See this video.
     
    Flash-Gordon likes this.
  7. Flash-Gordon

    Flash-Gordon

    Joined:
    Sep 9, 2013
    Posts:
    6
    Perfect Thanks
     
    dfsantos likes this.