基本上,我想弄清楚如何做:
我已经遇到了这个问题,它使用从Editor继承的不同类,并且以前通过使用PropertyField而不是TextArea / TextField来解决,但这摆脱了我想要的TextArea样式格式。另外,从EditorWindow继承的类似乎不允许以相同的方式使用(t = (script type)target;
不起作用,PropertyField需要它)..?
我对定制检查器和这些东西还很陌生,因此,如果可能,代码示例将非常有用。
谢谢!
在开始一般注释之前,因为您在问题中提到它:
[只要有可能,我强烈建议完全避免使用target
!特别是不要直接设置任何字段。这使诸如标记场景direty这样的事情变得非常复杂,因为保存更改以及撤消/重做功能非常复杂,因为您必须自己实现它!
而是始终将SerializedProperty
与SerializedProperty
和SerializedObject.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;
}
之类的>>
结果看起来基本相同:
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();
}
}
EditorGUILayout.TextField
尽管您可以看到我们已经不得不使用附加的
EditorGUILayout.TextField
对其进行伪造>您也可以在
EditorGUILayout.TextArea
中执行相同的操作。在大多数情况下,对于EditorGUILayout.TextArea
],通过string
并没有多大意义。EditorWindow
其结果
string