C# EditorGUILayout.ObjectField未接收引用
我正在学习自定义编辑器。此时会出现C# EditorGUILayout.ObjectField未接收引用,c#,unity3d,C#,Unity3d,我正在学习自定义编辑器。此时会出现对象字段,可以将包含它们的目录脚本或游戏对象拖到插槽中。但是当你放弃的时候,什么都没有发生。该字段亮起,但掉落的对象不会粘住 当我按下按钮时,target为null public class AddInventory : EditorWindow { public Inventory target; [MenuItem("Inventory/Add items")] public static void ShowWindow()
对象字段
,可以将包含它们的目录
脚本或游戏对象
拖到插槽中。但是当你放弃的时候,什么都没有发生。该字段亮起,但掉落的对象不会粘住
当我按下按钮时,target
为null
public class AddInventory : EditorWindow {
public Inventory target;
[MenuItem("Inventory/Add items")]
public static void ShowWindow()
{
GetWindow<AddInventory>("Add items");
}
void OnGUI()
{
GUILayout.Label("Add items to inventory", EditorStyles.boldLabel);
Inventory target = null;
target = (Inventory) EditorGUILayout.ObjectField("Inventory thingy", target, typeof(Inventory), true);
if (GUILayout.Button("I am button!"))
{
Debug.Log(target.thing);
}
}
}
您正在重新定义目标。删除
库存目标=null使用ObjectField
时,请确保Inventory
类当然是一个统一的对象
public class AddInventory : EditorWindow {
public Inventory target;
[MenuItem("Inventory/Add items")]
public static void ShowWindow()
{
GetWindow<AddInventory>("Add items");
}
void OnGUI()
{
GUILayout.Label("Add items to inventory", EditorStyles.boldLabel);
target = (Inventory)EditorGUILayout.ObjectField("Inventory thingy", target, typeof(Inventory));
if (GUILayout.Button("I am button!"))
{
Debug.Log(target.thing);
}
}
}
公共类附加项:编辑器窗口{
公共库存目标;
[菜单项(“库存/添加项目”)]
公共静态void ShowWindow()
{
获取窗口(“添加项目”);
}
void OnGUI()
{
Label(“将项目添加到库存”,editorstyle.boldLabel);
target=(Inventory)EditorGUILayout.ObjectField(“Inventory thingy”,target,typeof(Inventory));
if(GUILayout.Button(“我是Button!”)
{
Debug.Log(target.thing);
}
}
}
@Lautaro确实如此,但是在调用ObjectField
之前,您将target
设置为null
,因此target
将始终为null
。你有没有按照建议去掉那条线?这是我第一次尝试!但它在你发布新答案前几分钟起了作用。这当然是最有意义的。谢谢
public class AddInventory : EditorWindow {
public Inventory target;
[MenuItem("Inventory/Add items")]
public static void ShowWindow()
{
GetWindow<AddInventory>("Add items");
}
void OnGUI()
{
GUILayout.Label("Add items to inventory", EditorStyles.boldLabel);
target = (Inventory)EditorGUILayout.ObjectField("Inventory thingy", target, typeof(Inventory));
if (GUILayout.Button("I am button!"))
{
Debug.Log(target.thing);
}
}
}