我创建了一个编辑器工具,可以在一个圆圈中创建游戏对象,彼此相同(下面的代码).它有一个创建按钮,因此在创建被粉碎之前调整参数.我想在场景视图中看到动态变化.如何使用滑块调整以下参数:
>使用对象计数滑块创建或销毁游戏对象,同时调整以保持对象保持相同的角度
>通过更改半径滑块动态调整游戏对象
>使用角度滑块旋转圆上的所有对象(如旋转滚轮)
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,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,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,360f); EditorGUILayout.LabelField("Number of Items"); // Set as required tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems,36); EditorGUILayout.LabelField("Object"); tar.clonedobject = (GameObject)EditorGUILayout.ObjectField(tar.clonedobject,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,tar.spin + angle); var localPos = rot * Vector3.right * tar.radius; tar.spawnedobjects.Add(Instantiate(tar.clonedobject,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,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; } } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。