如何在阵列中的每个游戏对象的检查器选项中进行?

时间:2017-07-22 20:34:16

标签: c# unity3d unity5

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 [] 我希望在检查器中为每个GameObject提供选项,这样当我更改选项时,它将对游戏对象产生影响,而不是所有游戏对象。

每次我都会添加一个新的gmaeobject,我想为他提供选择。

1 个答案:

答案 0 :(得分:1)

我猜是使用类或简单结构:

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

我不知道你想要使用哪些选项,我做了一点点也许。但是如果你想使用那么多选项,我会使用一个具有自己的函数的类来管理旋转,另一个用于指示对象是应该加速还是减速。

[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();
        }
    }
}