将字符串变量保存为自定义EditorWindow格式类似TextArea的格式

问题描述 投票:0回答:1

基本上,我想弄清楚如何做:

  1. 将字符串(或任何)变量保存在自定义编辑器窗口中(继承在[EditorWindow]中更改)。
  2. 以类似TextArea的格式显示字符串,同时仍然允许如上所述保存更改。
  3. 按索引显示字符串数组中的字符串,而不是单独定义的字符串(我之前遇到过麻烦)
  4. 如果您也知道如何在自定义检查器中执行上述操作,(继承自Editor,而不是EditorWindow),那也很棒。

我已经遇到了这个问题,它使用从Editor继承的不同类,并且以前通过使用PropertyField而不是TextArea / TextField来解决,但这摆脱了我想要的TextArea样式格式。另外,从EditorWindow继承的类似乎不允许以相同的方式使用(t = (script type)target;不起作用,PropertyField需要它)..?

我对定制检查器和这些东西还很陌生,因此,如果可能,代码示例将非常有用。

谢谢!

unity3d unity-editor unity3d-editor
1个回答
0
投票

在开始一般注释之前,因为您在问题中提到它:

[只要有可能,我强烈建议完全避免使用target!特别是不要直接设置任何字段。这使诸如标记场景direty这样的事情变得非常复杂,因为保存更改以及撤消/重做功能非常复杂,因为您必须自己实现它!

而是始终将SerializedPropertySerializedPropertySerializedObject.Update结合使用(示例如下)。这样就可以处理所有这些内容,例如标记dirty,从而自动为您保存场景更改和“撤消/重做”!


然后到文本区域。

假设您有一个类似的课程

SerializedObject.Update

基本上有三个主要选项。我将首先执行SerializedObject.ApplyModifiedProperties(自定义检查器)脚本,因为您会更加灵活。

SerializedObject.ApplyModifiedProperties将在下面。


编辑器属性public class Example : MonoBehaviour { [SerializeField] private string _exampleString; public string AnotherExampleString; }

实际上,您甚至根本不需要Editor脚本!只需将相应的字段标记为EditorWindow,如下所示:

[TextArea]

这已经看起来像这样

[TextArea]


Editor

然后if您仍然需要[TextArea]脚本,关于[TextArea]的好处是,它会自动根据相应的类型使用正确的抽屉...并且也适用于所有编辑器属性!这不是很好吗?

所以简单地有public class Example : MonoBehaviour { [SerializeField] [TextArea] private string _exampleString; // You can also directly configure the min and max line count here as well // By default it is 3 lines [TextAre(3,7)] public string AnotherExampleString; } 之类的>>

enter image description here

结果看起来基本相同:

EditorGUILayout.PropertyField

EditorGUILayout.PropertyField

使用Editor,您可以将any EditorGUILayout.PropertyField显示为多行文本区域。这也适用于EditorGUILayout.PropertyField

让我们再说一次,我们没有标记我们的Editor字段

[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{
    private SerializedProperty _exampleString;
    private SerializedProperty AnotherExampleString;

    private void OnEnable()
    {
        // Link in the serialized properties to their according fields
        _exampleString = serializedObject.FindProperty("_exampleString");
        AnotherExampleString = serializedObject.FindProperty("AnotherExampleString");
    }

    public override void OnInspectorGUI()
    {
        DrawScriptField();

        // load the real target values into the serialized properties
        serializedObject.Update();

        EditorGUILayout.PropertyField(_exampleString);
        EditorGUILayout.PropertyField(AnotherExampleString);

        // write back the changed properties into the real target
        serializedObject.ApplyModifiedProperties();
    }

    // Little bonus from my side so you have the script field on top
    private void DrawScriptField()
    {
        EditorGUI.BeginDisabledGroup(true);
        EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((Example)target), typeof(Example), false);
        EditorGUILayout.Space();
        EditorGUI.EndDisabledGroup();
    }
}

但是我们可以使它们像使用此enter image description here脚本之前一样出现:

EditorGUILayout.TextField

尽管您可以看到我们已经不得不使用附加的EditorGUILayout.TextField对其进行伪造>


您也可以在EditorGUILayout.TextArea中执行相同的操作。在大多数情况下,对于EditorGUILayout.TextArea],通过string并没有多大意义。

EditorWindow

其结果

string

© www.soinside.com 2019 - 2024. All rights reserved.