加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何制作一个动态创建和销毁圆圈中的2D游戏对象的滑块

发布时间:2020-12-16 01:26:32 所属栏目:百科 来源:网络整理
导读:我创建了一个编辑器工具,可以在一个圆圈中创建游戏对象,彼此相同(下面的代码).它有一个创建按钮,因此在创建被粉碎之前调整参数.我想在场景视图中看到动态变化.如何使用滑块调整以下参数: 使用对象计数滑块创建或销毁游戏对象,同时调整以保持对象保持相同的
我创建了一个编辑器工具,可以在一个圆圈中创建游戏对象,彼此相同(下面的代码).它有一个创建按钮,因此在创建被粉碎之前调整参数.我想在场景视图中看到动态变化.如何使用滑块调整以下参数:

>使用对象计数滑块创建或销毁游戏对象,同时调整以保持对象保持相同的角度
>通过更改半径滑块动态调整游戏对象
>使用角度滑块旋转圆上的所有对象(如旋转滚轮)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读