可编写脚本的对象中的条件变量


10

使用时ScriptableObjects,如何使某些变量成为条件变量?

示例代码:

[System.Serializable]
public class Test : ScriptableObject
{
      public bool testbool;
      public string teststring;
      public int testint;
}

目标:testbool == true然后teststring是提供给编辑,如果testbool == falsetestint是提供给编辑,而另一种是“ 变灰 ”。

Answers:


7

编辑器友好路径是“自定义检查器”。用Unity API术语,这意味着扩展Editor类。

这是一个有效的示例,但是上面的doc链接将带您了解许多详细信息和其他选项:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
    private Test targetObject;

    void OnEnable()
    {
        targetObject = (Test) this.target;
    }

    // Implement this function to make a custom inspector.
    public override void OnInspectorGUI()
    {
        // Using Begin/End ChangeCheck is a good practice to avoid changing assets on disk that weren't edited.
        EditorGUI.BeginChangeCheck();

        // Use the editor auto-layout system to make your life easy
        EditorGUILayout.BeginVertical();
        targetObject.testBool = EditorGUILayout.Toggle("Bool", targetObject.testBool);

        // GUI.enabled enables or disables all controls until it is called again
        GUI.enabled = targetObject.testBool;
        targetObject.testString = EditorGUILayout.TextField("String", targetObject.testString);

        // Re-enable further controls
        GUI.enabled = true;

        targetObject.testInt = EditorGUILayout.IntField("Int", targetObject.testInt);

        EditorGUILayout.EndVertical();

        // If anything has changed, mark the object dirty so it's saved to disk
        if(EditorGUI.EndChangeCheck())
            EditorUtility.SetDirty(target);
    }
}

请记住,此脚本使用仅编辑器的API,因此必须将其放置在名为Editor的文件夹中。上面的代码会将您的检查器变为以下内容:

在此处输入图片说明

在您对编辑器脚本更加满意之前,这应该可以使您滚动。


4
[System.Serializable]
public class Test : ScriptableObject
{
    private bool testbool;
    public string teststring;
    public int testint;

    public string TestString 
    {
        get 
        {    
            return teststring; 
        }
        set 
        {
            if (testbool)
                teststring = value; 
        }
    }
}

看起来很精确!我将进行测试并报告!
Valamorde

看来这只会防止输入错误的值,并且不会使条件为时无法进行编辑true
Valamorde
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.