Creating new GameObjects

I don’t know what you want when you create new GameObjects for 3D, but for my UI stuff, I need the new instance to be a child of the GameObject that I have selected in the hierarchy. When you use the command provided by the Unity editor, it creates the new GameObject at the root of your project. Plus when you move that GameObject (haha I wrote MovieClip at first) in the hierarchy, its position and scale will change according to the parent’s values. So basically, I was spending my time moving new GameObject and setting their position to (0,0,0) and scale to (1,1,1).

When I had enough of it, I took matters in my own hands and created a little panel that will create a new GameObject as a child of the GameObject selected in the editor. It also sets it position to 0 and scale to 1. Bam! Here is the code for it:

public class CreateGameObject : EditorWindow{
	[MenuItem("zedia/Utility/GameObject Creator")]
	public static void ShowWindow() {
		EditorWindow.GetWindow<CreateGameObject>("GameObject Creator");
	}
 
	void OnGUI (){
		GUILayout.Label("GameObject Creator4", EditorStyles.boldLabel);
		GUILayout.Label("Creates a new GameObject under the selected GameObject in the Hierarchy,", EditorStyles.label);
		GUILayout.Label("with local position (0,0,0) and local scale (1,1,1)", EditorStyles.label);
		EditorGUILayout.Separator();
		GUILayout.BeginHorizontal();
 
		var retainValue = GUILayout.Button("Add GameObject To:");
		GameObject sel = EditorGUILayout.ObjectField(Selection.activeGameObject, typeof(GameObject), true, GUILayout.Width(140f)) as GameObject;
		if (retainValue){
			CreateTheEmptyGameObject();
		}
	}
 
	public static void CreateTheEmptyGameObject(){
		GameObject newGameObject = new GameObject();
		newGameObject.transform.parent = Selection.activeGameObject.transform;
		newGameObject.transform.localScale = new Vector3(1,1,1);
		newGameObject.transform.localPosition = new Vector3(0,0,0);
		Selection.activeObject = newGameObject;
	}
 
	void OnSelectionChange () { Repaint(); }
}

Now, that was what I wanted, but it would have been even better if it could listen for a keyboard shortcut. So I set out to do just that in the previous little panel except it didn’t work. Something was preventing me from receiving the events from the keyboard (I can Debug.Log, but as soon as I execute code it doesn’t work). So I decided I would just create a MenuItem associated with a keyboard shortcut. Here is what the code look likes:

using UnityEngine;
using System.Collections;
using UnityEditor;
 
public class CreateGameObjectCommand : MonoBehaviour {
	[MenuItem("zedia/Utility/GameObject Creator Command #%m")]
	static void CreateGameObject () {
		GameObject newGameObject = new GameObject();
		newGameObject.transform.parent = Selection.activeGameObject.transform;
		newGameObject.transform.localScale = new Vector3(1,1,1);
		newGameObject.transform.localPosition = new Vector3(0,0,0);
		Selection.activeObject = newGameObject;
	}
}

So here you go; either a panel in the editor or a shortcut or both if you want to. Just grab the code and put the files in a folder named Editor, and Unity will add a menu for you. I hope Unity3D changes their default behavior for creating new GameObject but until then, we will make it work ourself!

, , , ,

  1. #1 by Nicki - September 18th, 2013 at 13:07

    If you’re using NGUI, then you have some new shortcuts like:

    shift+ctrl+n: Makes a GameObject under the currently selected GameObject
    shift+ctrl+S: New sprite the same way
    shft+ctrl+t: New UITexture
    shift+ctrl+L; new label

    Switch ctrl with Cmd on a mac. :)

  2. #2 by zedia.net - September 18th, 2013 at 17:02

    @ Nicki This is all nice I should have looked deeper into that. Seems to be switching ctrl for alt on a mac. Also when you create a new GameObject, Unity complains about breaking a prefab, which it doesn’t with my script, weird…

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.