Make an object and position at the center of your circle. If you already have an object with a custom script there, make an Inspector for it. If you don't have anything, make a script. It doesn't have to do anything. It might just hold data. A custom inspector just needs an script. Let's call them CircleSpawn
and CircleSpawnEditor
. CircleSpawn gets attached to the object at the center of the circle. It might look like this:
public class CircleSpawn : MonoBehaviour {
public float radius;
public int numOfItems;
public GameObject clonedObject;
public List<GameObject> spawnedObjects;
}
CircleSpawnEditor needs to be saved to a folder called Editor
and looks something like this:
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
[CustomEditor(typeof(CircleSpawn))]
public class CircleSpawnEditor : Editor
{
public override void OnInspectorGUI()
{
var tar = (CircleSpawn)target;
//set its values
tar.radius = EditorGUILayout.FloatField("Radius:", tar.radius);
tar.numOfItems = EditorGUILayout.IntField("Number of Items:", tar.numOfItems);
tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject,
typeof(GameObject), true);
//Inspector button for creating the objects in the Editor
if (GUILayout.Button("Create"))
{
//clean up old objects
if (tar.spawnedObjects != null)
{
foreach (var ob in tar.spawnedObjects)
{
DestroyImmediate(ob);
}
}
tar.spawnedObjects = new List<GameObject>();
float angleBetween = 360.0f / tar.numOfItems;
float angle = 0;
for (int i = 0; i <= tar.numOfItems; i++)
{
//for each object, find a rotation and position
var rot = Quaternion.Euler(0, angle, 0);
var localPos = rot * Vector3.forward * tar.radius;
tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
tar.transform.position + localPos, rot));
angle += angleBetween;
}
}
}
}
I tested it and the result looks like this:
