Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Unity中修改自定义编辑器脚本中的序列化变量_C#_User Interface_Unity3d_Editor - Fatal编程技术网

C# 如何在Unity中修改自定义编辑器脚本中的序列化变量

C# 如何在Unity中修改自定义编辑器脚本中的序列化变量,c#,user-interface,unity3d,editor,C#,User Interface,Unity3d,Editor,我有一个带有1个序列化字符串的测试脚本,我试图通过在TextField中键入一些内容来访问和修改它,但我不知道将TextField分配给什么 测试脚本: using UnityEngine; public class Test : MonoBehaviour { [SerializeField] private string value; } 测试工具脚本: using UnityEngine; using UnityEditor; [CustomEditor(typeof(Te

我有一个带有1个序列化字符串的测试脚本,我试图通过在TextField中键入一些内容来访问和修改它,但我不知道将TextField分配给什么

测试脚本:

using UnityEngine;

public class Test : MonoBehaviour
{
    [SerializeField] private string value;

}
测试工具脚本:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test))]
public class TestTool : Editor
{
[ExecuteInEditMode]
public override void OnInspectorGUI()
{

    base.OnInspectorGUI();

    Rect textFieldRect = new Rect(EditorGUILayout.GetControlRect(false, EditorGUIUtility.currentViewWidth));

    EditorGUI.DrawRect(textFieldRect, Color.gray);

    EditorGUI.TextField(textFieldRect, "Type here...");
}
}

这是:

Test myTest = (Test)target;
myTest.value = EditorGUI.TextField(textFieldRect, myTest.value);

target
是通过编辑器超类提供的属性,包含对正在检查的任何对象实例的引用

我不建议直接使用

Test myTest = (Test)target;
myTest.value = EditorGUI.TextField(textFieldRect, myTest.value);
改用

这样做的巨大优势在于,撤销/重做以及将场景和类标记为“脏”都是自动处理的,您不必手动执行

但是,要使此工作,变量必须始终为
公共
[SerializedField]
,这在您的类中已经存在

实际上,我建议您使用和设置尺寸,而不是
rect

选项

Guillayout.宽度, Guillayout.身高, Guillayout.MinWidth, GUILayout.MaxWidth, Guillayout.MinHeight, GUILayout.MaxHeight, Guillayout, Guillayout.身高

为了不显示标签,请使用
GUIContent.none

所以看起来像

EditorGUILayout.PropertyField(_value, GUIContent.none, GUILayout.ExpandHeight, GUILayout.ExpandWith);

当Rect很小时,它不起作用,如何使它不显示属性的名称?它还因为名称而弄乱了Rect。我更新了我的答案。但我现在意识到你实际上是在另一帧中用一个矩形画的。在问题的第一张图片上,它看起来有点不同。但是您也可以简单地使用
GUIContent.none
作为
EditorGUI
版本
EditorGUILayout.PropertyField(_value, GUIContent.none, GUILayout.ExpandHeight, GUILayout.ExpandWith);