ObjectField
示例程序 | |
---|---|
说明 | 生成控制 Object 变量的区域 |
用法
public static Object ObjectField(Object obj, Type objType, bool allowSceneObjects, params GUILayoutOption[] options);
public static Object ObjectField(string label, Object obj, Type objType, bool allowSceneObjects, params GUILayoutOption[] options);
public static Object ObjectField(GUIContent label, Object obj, Type objType, bool allowSceneObjects, params GUILayoutOption[] options);
参数
label
对应的标签名称。
obj
该区域控制的 Object
类型变量,也可以是 Object
子类型的变量。
objType
允许设置的物体的类型,可以通过拖放或者选择器来设置该区域。
allowSceneObjects
是否允许设置场景物体。
如果引用的物体存储为 asset 的一部分 ,则设置为 false
,因为资源无法存储对场景中物体的引用。
如果 ObjectField
是一个脚本组件 Script Component 的自定义编辑器中的一部分,那么可以通过 EditorUtility.IsPersistent()
方法检查该组件是在资源上还是在场景物体上。
options
(可选)用于指定额外的布局属性,该参数将覆盖默认样式。
示例
public override void OnInspectorGUI () {
// object field for Object
_target.Obj = EditorGUILayout.ObjectField ("Object", _target.Obj, typeof (Object), true);
// object field for Transform
_target.Transf = (Transform)EditorGUILayout.ObjectField ("Transform", (Object)_target.Transf, typeof (Transform), false);
}
编译结果如下: