DelayedDoubleField, DelayedFloatField, DelayedIntField, DelayedTextField

示例程序
说明生成可以键入浮点数、整数、文本且延迟影响变量值的区域

用法

四种方法的用法一致,只需改变相应的函数名称和变量类型即可。是 DoubleField FloatField IntField TextField 对应的有延迟版本,延迟是指新键入的值直到用户按下 ENTER 或者该区域失去鼠标焦点时才会被返回给被控制的变量。

public static double DelayedDoubleField(double value, params GUILayoutOption[] options);

public static double DelayedDoubleField(double value, GUIStyle style, params GUILayoutOption[] options);

public static double DelayedDoubleField(string label, double value, params GUILayoutOption[] options);

public static double DelayedDoubleField(string label, double value, GUIStyle style, params GUILayoutOption[] options);

public static double DelayedDoubleField(GUIContent label, double value, params GUILayoutOption[] options);

public static double DelayedDoubleField(GUIContent label, double value, GUIStyle style, params GUILayoutOption[] options);

参数

label

对应的标签名称。

value

该区域控制的变量,类型可以是 double float int string,改变相应的函数名即可。

style

(可选)由 GUIStyle 类型定义的 GUI 样式。

options

(可选)用于指定额外的布局属性,该参数将覆盖默认样式。

示例

public override void OnInspectorGUI () {

		// GUI style
		GUIStyle style = new GUIStyle ();
		style.fontSize = 30;
		style.normal.background = Texture2D.whiteTexture;
		style.normal.textColor = Color.red;
		style.margin = new RectOffset (0, 0, 3, 3);
		style.padding = new RectOffset (10, 10, 10, 10);
		style.alignment = TextAnchor.MiddleLeft;
	
		// double
		_target.DoubleValue = EditorGUILayout.DelayedDoubleField ("Double Value", _target.DoubleValue, style, GUILayout.Height(50));
	
		// float
		_target.FloatValue = EditorGUILayout.DelayedFloatField ("Float Value", _target.FloatValue, style, GUILayout.Height (50));
	
		// int
		_target.IntValue = EditorGUILayout.DelayedIntField ("Int Value", _target.IntValue, style, GUILayout.Height (50));
	
		// text
		_target.TextValue = EditorGUILayout.DelayedTextField ("Text Value", _target.TextValue, style, GUILayout.Height (50));

}

编译结果如下:

为展示其延迟效果,在 OnInspectorGUI() 中追加代码:

// update the dispalying text
_display = string.Join ("\n", new string [] {
			"Delayed Double Value: " + _target.DoubleValue.ToString (),
			"Delayed Float Value: " + _target.FloatValue.ToString (),
			"Delayed Int Value: " + _target.IntValue.ToString (),
			"Delayed Text Value: " + _target.TextValue
});

// force redraw every time when _target changes
if (GUI.changed) {
			EditorUtility.SetDirty (_target);
}

整体追加在 Scene View 中绘制文字的代码:

private void OnSceneGUI () {
		Handles.BeginGUI ();
		
		GUILayout.BeginArea (new Rect (10f, 10f, 360f, 100f));
		GUILayout.Label (_display);
		GUILayout.EndArea ();
		
		Handles.EndGUI ();
}

当修改 Double Value 区域的值后,不按下 ENTER 且保持鼠标焦点在该区域,Scene View 中的对应文本未发生变化:

当按下 ENTER 或鼠标点击该区域外边(但要保证 Inspector 在面板内,否则不会绘制文本)时,对应文本才会更新:

另外可以发现,有延迟效果的控制区域不可以通过鼠标拖动修改变量的值,而普通的控制区域如 DoubleField 是可以的。