Unity3d 在unity inspector中水平对齐变量

Unity3d 在unity inspector中水平对齐变量,unity3d,inspector,Unity3d,Inspector,我正在编写一个非常简单的类,用于存储数据(至少目前是这样),以便在Unity项目中使用,以与学习AI一起使用。我希望能够轻松地在inspector中自定义多个代理,垂直堆叠的复选框数量使我的项目的这一部分在inspector中占主导地位。如果我能简单地让一个部分利用检查员右侧的充足空间,那就不会那么难看了 我已经读了很多关于自定义属性抽屉和检查器窗口的书,但是似乎有很多工作要做,包括重写整个类的显示方式,只做一个更改 作为参考,这里是类本身 [System.Serializable] publ

我正在编写一个非常简单的类,用于存储数据(至少目前是这样),以便在Unity项目中使用,以与学习AI一起使用。我希望能够轻松地在inspector中自定义多个代理,垂直堆叠的复选框数量使我的项目的这一部分在inspector中占主导地位。如果我能简单地让一个部分利用检查员右侧的充足空间,那就不会那么难看了

我已经读了很多关于自定义属性抽屉和检查器窗口的书,但是似乎有很多工作要做,包括重写整个类的显示方式,只做一个更改

作为参考,这里是类本身

[System.Serializable]
public class NNInfo
{
    public string networkName = "Agent";

    [Header("Movement Properties")]
    public float movementSpeed = 10f;
    public float rotationSpeed = 1f;

    [Header("Learning Properties")]
    public float learningRate = 0.1f;
    public float momentum = 0f;
    public Rewards rewardFunc;
    public int[] hiddenLayers;

    [Header("Other Properties")]
    public int maxHealth = 1000;

    [Header("Optional Inputs")]
    public bool m_PointToNearestBall = false;           // input which is 1 while the agent is facing a ball and -1 when facing away
    public bool m_DistanceToNearestBall = false;        // input which is 1 while the agent is at the ball and -1 when at max possible distance away
    public bool m_PointToEndzone = false;               // similar to m_PointToNearestBall but for the endzone
    public bool m_Position = false;                     // two inputs which inform the player of its normalized x and y coordinates on the field
    public bool m_WhetherHoldingBall = false;           // tells the player whether its holding a ball
    public bool m_CanSeeHealth = false;                 // Whether the player can know its own health

    [Header("Optional Outputs")]
    public bool m_ForwardLeft = false;                  // turn left and move forward simultaneously
    public bool m_ForwardRight = false;                 // turn right and move forward simultaneously
    public bool m_Reverse = false;                      // move backwards
    public bool m_Flip = false;                         // instantly switch to opposite direction
    public bool m_TurnToBall = false;                   // instantly turn to face nearest ball
    public bool m_TurnToLeft = false;                   // instantly turn to face left side of field
    public bool m_Attack = false;                       // attack a player (or idle if no contact)
}

自定义属性抽屉是您应该感兴趣的关键字

编写自己的代码来管理这些属性-让您描述如何在Unity editor的Inspector视图中显示属性

要开始,请转到包含您可以基于的代码的官方

代码片段(可在链接下找到Javacrpt,c#版本):

对象代码:

enum IngredientUnit { Spoon, Cup, Bowl, Piece }

// Custom serializable class
class Ingredient extends System.Object {
    var name : String;
    var amount : int = 1;
    var unit : IngredientUnit;
}

var potionResult : Ingredient;
var potionIngredients : Ingredient[];

function Update () {
    // Update logic here...
}
@CustomPropertyDrawer(Ingredient)
class IngredientDrawer extends PropertyDrawer {

    // Draw the property inside the given rect
    function OnGUI (position : Rect, property : SerializedProperty, label : GUIContent) {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty (position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        var amountRect = new Rect (position.x, position.y, 30, position.height);
        var unitRect = new Rect (position.x+35, position.y, 50, position.height);
        var nameRect = new Rect (position.x+90, position.y, position.width-90, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField (amountRect, property.FindPropertyRelative ("amount"), GUIContent.none);
        EditorGUI.PropertyField (unitRect, property.FindPropertyRelative ("unit"), GUIContent.none);
        EditorGUI.PropertyField (nameRect, property.FindPropertyRelative ("name"), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty ();
    }
}
编辑器代码:

enum IngredientUnit { Spoon, Cup, Bowl, Piece }

// Custom serializable class
class Ingredient extends System.Object {
    var name : String;
    var amount : int = 1;
    var unit : IngredientUnit;
}

var potionResult : Ingredient;
var potionIngredients : Ingredient[];

function Update () {
    // Update logic here...
}
@CustomPropertyDrawer(Ingredient)
class IngredientDrawer extends PropertyDrawer {

    // Draw the property inside the given rect
    function OnGUI (position : Rect, property : SerializedProperty, label : GUIContent) {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty (position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        var amountRect = new Rect (position.x, position.y, 30, position.height);
        var unitRect = new Rect (position.x+35, position.y, 50, position.height);
        var nameRect = new Rect (position.x+90, position.y, position.width-90, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField (amountRect, property.FindPropertyRelative ("amount"), GUIContent.none);
        EditorGUI.PropertyField (unitRect, property.FindPropertyRelative ("unit"), GUIContent.none);
        EditorGUI.PropertyField (nameRect, property.FindPropertyRelative ("name"), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty ();
    }
}

这是一个非常酷的想法,但我认为这是不可能的(以一种简单的方式)。我认为这是一个不属于SO的主题。因为,问题是关于Unity开发工具的,它与纯编程没有直接关系。它应该属于Game Dev()或者为什么不属于Unity Answers网站()。要实现这一点,您需要更新编辑器代码(或者编写自己的函数)。这不是很难,网上有很多教程。