C# 如何在编辑器中为Unity中的旋转平台脚本添加列表

C# 如何在编辑器中为Unity中的旋转平台脚本添加列表,c#,unity3d,rotation,C#,Unity3d,Rotation,我目前正在制作一个2D游戏,我添加了一个平台,它围绕一个中心旋转。现在我想,你可以选择围绕中心旋转多少个平台。就像屏幕截图上的线路点。我不是很热衷于编码,所以你能把它添加到我当前的脚本中吗 谢谢 截图: 当前脚本: using UnityEngine; public class Rotation_Object_System : MonoBehaviour { [SerializeField] private Transform rotationCenter; pub

我目前正在制作一个2D游戏,我添加了一个平台,它围绕一个中心旋转。现在我想,你可以选择围绕中心旋转多少个平台。就像屏幕截图上的线路点。我不是很热衷于编码,所以你能把它添加到我当前的脚本中吗

谢谢

截图:

当前脚本:

using UnityEngine;

public class Rotation_Object_System : MonoBehaviour
{
    [SerializeField]
    private Transform rotationCenter;

    public GameObject rotationObject;

    [SerializeField]
    private float rotationRadius = 2f, angularSpeed = 2f;

    public bool ClockwiseRotation;
    public bool HideRotationCenter;

    private float posX, posY, angle = 0f;

    private void Start()
    {
        Loading_Initial_Parameters();
    }

    private void Update()
    {
        // Rotation
        posX = rotationCenter.position.x + Mathf.Cos(angle) * rotationRadius;
        posY = rotationCenter.position.y + Mathf.Sin(angle) * rotationRadius;
        rotationObject.transform.position = new Vector2(posX, posY);

        float angularMovement = Time.deltaTime * angularSpeed;

        if (ClockwiseRotation)
            angle -= angularMovement;
        else
            angle += angularMovement;

        if (angle >= 360f)
            angle = 0f;
    }

    private void Loading_Initial_Parameters()
    {
        if (rotationCenter.TryGetComponent<SpriteRenderer>(out var rotationCenterSpriteRenderer))
        {
            rotationCenterSpriteRenderer.enabled = !HideRotationCenter;
        }
        else
        {
            Debug.LogWarning(
                $"Rotation Center ({rotationCenter.name}) does NOT HAVE " +
                $"a SpriteRenderer Component to hide/show");
        }
    }
}
使用UnityEngine;
公共类旋转对象系统:单行为
{
[序列化字段]
私有变换旋转中心;
公共游戏对象旋转对象;
[序列化字段]
专用浮动旋转半径=2f,角速度=2f;
公共图书馆顺时针旋转;
公共布尔HideRotationCenter;
专用浮点数posX,posY,角度=0f;
私有void Start()
{
加载初始参数();
}
私有void更新()
{
//轮换
posX=旋转中心位置x+数学坐标(角度)*旋转半径;
posY=旋转中心位置y+数学正弦(角度)*旋转半径;
rotationObject.transform.position=新向量2(posX,posY);
浮动角度移动=Time.deltaTime*角度速度;
if(顺时针旋转)
角度-=角度运动;
其他的
角度+=角度运动;
如果(角度>=360f)
角度=0f;
}
私有无效加载\初始\参数()
{
if(旋转中心.TryGetComponent(out var ROTATIONCENTERSPRITERENDER))
{
rotationCenterSpriteRenderer.enabled=!HideRotationCenter;
}
其他的
{
Debug.LogWarning(
$“旋转中心({rotationCenter.name})没有”+
$“要隐藏/显示的Spirterender组件”);
}
}
}

我认为一些更改将使这项工作适合您

  • 将公共字段添加为列表或数组。这将允许您在Unity中从inspector添加/删除元素
  • 更新更新代码以循环遍历数组/列表中的每个项目,并将位置更改应用于每个项目
  • 您可能需要添加一些代码来单独跟踪每个旋转位置。。。您已经在跟踪单个游戏对象的角度。。。您需要将其更新为数组/列表。。。每个游戏对象对应1件物品

  • 谢谢你的回答,你有一些示例代码吗?