Skip to content

Instantly share code, notes, and snippets.

@alikrc
Last active May 31, 2022 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alikrc/08126a025db36495a11531e39bafd6c5 to your computer and use it in GitHub Desktop.
Save alikrc/08126a025db36495a11531e39bafd6c5 to your computer and use it in GitHub Desktop.
Basic Object Spawner for unity editor
using UnityEngine;
using UnityEditor;
using System;
public class BasicObjectSpawner : EditorWindow
{
string objectBaseName;
int objectId;
GameObject objectToSpawn;
float objectScale;
float spawnRadius = 5f;
[MenuItem("Tools/Basic Object Spawner")]
private static void ShowWindow()
{
var window = GetWindow<BasicObjectSpawner>();
window.titleContent = new GUIContent("BasicObjectSpawner");
window.Show();
}
private 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("Prefab didn't assigned");
return;
}
if (String.IsNullOrEmpty(objectBaseName))
{
objectBaseName = objectToSpawn.name;
}
var spawnCircle = UnityEngine.Random.insideUnitCircle * spawnRadius;
var spawnPos = new Vector3(spawnCircle.x, 0f, spawnCircle.y);
var 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