gpt4 book ai didi

c# - 如何制作动态创建和销毁圆圈中的 2d 游戏对象的 slider

转载 作者:行者123 更新时间:2023-11-30 14:22:23 26 4
gpt4 key购买 nike

我创建了一个编辑器工具,可以在一个圆圈中创建游戏对象,彼此之间的距离相等(代码如下)。它有一个创建按钮,因此在粉碎创建之前调整参数。我希望看到在场景 View 中动态进行的更改。如何使用 slider 调整以下参数:

  1. 使用对象计数 slider 创建或销毁游戏对象,同时进行调整以使对象之间的角度保持相同

  2. 通过改变半径 slider 动态调整游戏对象

  3. 使用角度 slider 旋转圆圈上的所有对象(如旋转轮子)

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对于 numOfItems重新创建对象,当:

numOfItems != spawnedObjects.Count

Dynamically adjust the game objects by changing a radius slider

使用 Slider对于 radius,当它发生变化时,遍历 spawnedObjects 并将它们移动:

pos = rot * Vector3.right * tar.radius

Rotate all objects on the circle (like spinning a wheel) using an angle slider

使用 Slider对于 spin,当它发生变化时,迭代 spawnedObjects 并将它们旋转:

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;
}
}
}
}

关于c# - 如何制作动态创建和销毁圆圈中的 2d 游戏对象的 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com