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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Create GameObject and save in Hierarchy

Discussion in 'Scripting' started by Tdonk, Mar 10, 2015.

  1. Tdonk

    Tdonk

    Joined:
    Jan 29, 2015
    Posts:
    7
    I would like to create gameObjects (text, 3d stuff, etc.) from scripts and save them in the scene.
    For example: If I use a script like this,
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Start() {
    6.         GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
    7.        }
    8. }
    the "plane" will be add in Hierarchy temporarily (just after you press start).

    I'm looking for some way to create a lot of different gameObjects automatically (through a script, api, etc.) and save this stuff permanently in the hierarchy.

    Some suggestions?
    Thank You
     
    Last edited: Mar 10, 2015
  2. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    You have to write an editor script for that. Editor scripts work just like run-time ones for the most part, with their own caveats.

    A very simple example for achieving what you're trying to do would be as follows:

    Place the following script in the "Assets/Editor" folder within your project.

    testEditor.cs
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class testEditor : MonoBehaviour {
    6.     [MenuItem ("GameObject/Create Other/Primitive")]
    7.     static void DoPrimitive()
    8.     {
    9.         GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
    10.         EditorUtility.DisplayDialog("Yay!", "We're all done!", "Awesome!");
    11.     }
    12.  
    13.     [MenuItem ("GameObject/Create Other/Other Stuff")]
    14.     static void OtherStuff(){
    15.         EditorUtility.DisplayDialog("Other Stuff", "Doing other stuff", "Mmmmkay");
    16.     }
    17. }
    18.  
    This will create two menu items in your editor:
    Gameobject -> Create Other -> Primitive
    Gameobject -> Create Other -> Other Stuff

    One last note: if you're going to be creating meshes from within the editor, make sure you always use shared stuff, i.e. ".sharedMesh" instead of ".mesh", ".sharedMaterial" instead of ".material", or you'll start leaking. The editor mostly fixes that upon saving, but you shouldn't depend on that.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For real cheap hacks you can tag things with [ExecuteInEditMode]. Be warned, this is hacky and can do a lot of damage.
     
  4. camsha

    camsha

    Joined:
    Nov 18, 2013
    Posts:
    11
    One simple way is to execute your script and while the game is running and the hierarchy contains the objects you want, copy them from the hierarchy window (right click menu) and then stop your game and paste them into the hierarchy. Fast and easy, though not as elegant as writing an editor script.
     
    Kurt-Dekker and Kiwasi like this.