UnityScript-如何遍历类的公共属性

Cod*_*rer 2 unity-game-engine unityscript

我在中有以下脚本UnityScript,该脚本在JavaScriptUnity Editor中被调用,但是对于遍历对象而言并不太相同。

public class UpgradeProfile extends MonoBehaviour {

    public var brakeSpeed : float = 0;
    public var jumpForce  : float = 0;
    public var maxJumps : int = 1;

};
Run Code Online (Sandbox Code Playgroud)

如何遍历此类的所有属性,例如记录值或将它们与同一类的另一个成员的值求和?

注意: UnityScript不是JavaScript C#,因此与这些语言有关的答案不能回答此问题。

Joh*_*ist 6

这对我来说是获得特性和价值的方法。

#pragma strict

public var test1 = 10;
public var test2 = 11;

function Start () 
{

    for(var property in this.GetType().GetFields()) 
    {
        Debug.Log("Name: " + property.Name + " Value: " + property.GetValue(this));
    }

}
Run Code Online (Sandbox Code Playgroud)

这打印出来

Name: test1 Value: 10
Name: test2 Value: 11
Run Code Online (Sandbox Code Playgroud)

如果你想与另一个组件要做到这一点,更换thiscomponent,而不是