我创建了一个编辑器工具,可以在一个圆圈中创建游戏对象,彼此相同(下面的代码).它有一个创建按钮,因此在创建被粉碎之前调整参数.我想在场景视图中看到动态变化.如何使用滑块调整
>使用对象计数滑块创建或销毁游戏对象,同时调整以保持对象保持相同的角度
>通过更改半径滑块动态调整游戏对象
>使用角度滑块旋转圆上的所有对象(如旋转滚轮)
CircleSpawn
public class CircleSpawn : MonoBehaviour { public float radius; public int numOfItems; public GameObject clonedObject; public List<GameObject> spawnedObjects; }
CircleSpawnEditor
[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, 0, angle); var localPos = rot * Vector3.right * tar.radius; tar.spawnedObjects.Add(Instantiate(tar.clonedObject, tar.transform.position + localPos, rot)); angle += angleBetween; } } } }
Create or destroy game objects using an object count slider, whiles adjusting to keep the objects the same angle apart
使用IntSlider
for numOfItems并在以下情况下重新创建对象:
numOfItems != spawnedObjects.Count
Dynamically adjust the game objects by changing a radius slider
使用Slider
作为半径,当它发生变化时,迭代生成的对象并移动它们:
pos = rot * Vector3.right * tar.radius
Rotate all objects on the circle (like spinning a wheel) using an angle slider
使用Slider
进行旋转,当它发生变化时,迭代生成的对象并通过以下方式旋转它们:
rot = Quaternion.Euler(0, 0, tar.spin + angle)
CircleSpawn:
public class CircleSpawn : MonoBehaviour { public float radius, radiusLast, spin, spinLast; public int numOfItems; public GameObject clonedObject; public List<GameObject> spawnedObjects; }
CircleSpawnEditor:
[CustomEditor(typeof(CircleSpawn))] public class CircleSpawnEditor : Editor { public override void OnInspectorGUI () { var tar = (CircleSpawn)target; EditorGUILayout.LabelField("Radius"); // Set as required tar.radius = EditorGUILayout.Slider(tar.radius, 0f, 100f); EditorGUILayout.LabelField("Angle"); // Set as required tar.spin = EditorGUILayout.Slider(tar.spin, 0f, 360f); EditorGUILayout.LabelField("Number of Items"); // Set as required tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems, 0, 36); EditorGUILayout.LabelField("Object"); tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject, typeof(GameObject), true); float angle, angleBetween = 360.0f / tar.numOfItems; if (tar.spawnedObjects == null) tar.spawnedObjects = new List<GameObject>(); // Solution #1 if (tar.spawnedObjects.Count != tar.numOfItems) { foreach (var ob in tar.spawnedObjects) DestroyImmediate(ob); tar.spawnedObjects.Clear(); angle = 0f; for (int i = 0; i < tar.numOfItems; i++) { var rot = Quaternion.Euler(0f, 0f, tar.spin + angle); var localPos = rot * Vector3.right * tar.radius; tar.spawnedObjects.Add(Instantiate(tar.clonedObject, tar.transform.position + localPos, rot)); angle += angleBetween; } } // Solutions #2 & 3 if (!Mathf.Approximately(tar.spin, tar.spinLast) || !Mathf.Approximately(tar.radius, tar.radiusLast)) { tar.spinLast = tar.spin; tar.radiusLast = tar.radius; angle = 0f; for (int i = 0; i < tar.numOfItems; i++) { var rot = Quaternion.Euler(0f, 0f, tar.spin + angle); var localPos = rot * Vector3.right * tar.radius; tar.spawnedObjects[i].transform.position = tar.transform.position + localPos; tar.spawnedObjects[i].transform.rotation = rot; angle += angleBetween; } } } }