我如何为数组中的每个游戏对象创建检查器选项?

How can i make in the inspector options for each gameobject in the array?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpinObject : MonoBehaviour
{
    private bool slowDown = false;
    public float rotationSpeed;
    public float slowdownMax;
    public float slowdownMin;
    public GameObject[] objectsToRotate;

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
        for (int i = 0; i < objectsToRotate.Length; i++)
        {
            if (rotationSpeed > slowdownMax)
                slowDown = true;
            else if (rotationSpeed < slowdownMin)
                slowDown = false;

            rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
            objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
        }
    }
}

我有游戏对象数组:GameObject[] 我希望检查器中的每个游戏对象都有选项,所以当我更改选项时,它会影响游戏对象而不是所有游戏对象。

每次我添加一个新的 gmaeobject 时,我都希望为他提供选项。

使用 class 或我猜的简单结构 :

[System.Serializable]
public struct SpinableObject
{
    public Transform t ;
    public float rotationSpeed ;
    public float minSpeed ;
    public float maxSpeed ;
    public float speedRate;
    [HideInInspector]
    public bool slowDown ;
}

public class SpinObject : MonoBehaviour
{
    private bool slowDown = false;
    public float rotationSpeed;
    public float slowdownMax;
    public float slowdownMin;
    public GameObject[] objectsToRotate;

    // Update is called once per frame
    void Update ()
    {
        for (int i = 0; i < objectsToRotate.Length; i++)
        {
            SpinableObject spinableObject = objectsToRotate[i]

            if (spinableObject.rotationSpeed > spinableObject.maxSpeed )
                spinableObject.slowDown = true;
            else if (spinableObject.rotationSpeed < spinableObject.minSpeed )
                spinableObject.slowDown = false;

            spinableObject.rotationSpeed = (spinableObject.slowDown) ? spinableObject.rotationSpeed - 0.1f : spinableObject.rotationSpeed + 0.1f;
            spinableObject.t.Rotate(Vector3.forward, Time.deltaTime * spinableObject.rotationSpeed);
        }
    }
}

我不知道你想使用哪个选项,我可能做了一些。但是如果你想使用那么多选项,我会使用一个 class 和它自己的函数来管理旋转,另一个来指示对象是应该加速还是减速。

[System.Serializable]
public class SpinableObject
{
    [SerializeField]
    private Transform t ;
    [SerializeField]
    private float rotationSpeed ;
    [SerializeField]
    private float minSpeed ;
    [SerializeField]
    private float maxSpeed ;
    [SerializeField]
    private float speedRate;
    private bool slowDown ;

    public void Rotate()
    {
        if (rotationSpeed > maxSpeed )
            slowDown = true;
        else if (rotationSpeed < minSpeed )
           slowDown = false;

        rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
        t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
    }
}

public class SpinObject : MonoBehaviour
{
    private bool slowDown = false;
    public float rotationSpeed;
    public float slowdownMax;
    public float slowdownMin;
    public SpinableObject[] objectsToRotate;

    // Update is called once per frame
    void Update ()
    {
        for (int i = 0; i < objectsToRotate.Length; i++)
        {
            objectsToRotate[i].Rotate();
        }
    }
}