Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Created April 21, 2020 11:58
Show Gist options
  • Save openroomxyz/76ff4d1dbfe41620e1f34625a0b0caf7 to your computer and use it in GitHub Desktop.
Save openroomxyz/76ff4d1dbfe41620e1f34625a0b0caf7 to your computer and use it in GitHub Desktop.
Unity : How to make simple Editor script?
using UnityEditor;
using UnityEngine;
public class BasicObjectSpawner : EditorWindow
{
string objectBaseName = "";
int objectId = 1;
GameObject objectToSpawn;
float objectScale;
float spawnRadius = 5f;
[MenuItem("Tools/Basic Object Spawner")]
public static void ShowWindow()
{
GetWindow(typeof(BasicObjectSpawner)); //GetWindow is a method inherited from the EditorWindow class
}
public void OnGUI()
{
GUILayout.Label("Spawn new Object", EditorStyles.boldLabel);
objectBaseName = EditorGUILayout.TextField("Base Name", objectBaseName);
objectId = EditorGUILayout.IntField("Object ID", objectId);
objectScale = EditorGUILayout.Slider("Object Scale", objectScale, 0.5f, 3f);
spawnRadius = EditorGUILayout.FloatField("Spawn Radius", spawnRadius);
objectToSpawn = EditorGUILayout.ObjectField("Prefab to Spawn", objectToSpawn, typeof(GameObject), false) as GameObject;
if(GUILayout.Button("Spawn Object"))
{
SpawnObject();
}
}
private void SpawnObject()
{
if(objectToSpawn == null)
{
Debug.LogError("Error : Please assign an object to be spawned.");
return;
}
if(objectBaseName == string.Empty)
{
Debug.LogError("Error : Plese enter a base name for the object.");
return;
}
Vector2 spawnCircle = Random.insideUnitCircle * spawnRadius;
Vector3 spawnPos = new Vector3(spawnCircle.x, 0f, spawnCircle.y);
GameObject newObject = Instantiate(objectToSpawn, spawnPos, Quaternion.identity);
newObject.name = objectBaseName + objectId;
newObject.transform.localScale = Vector3.one * objectScale;
objectId++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment