- Home /
Problem using EditorGUILayout.ObjectField with custom type
So I have a class Skill, which is Serializable, and I want to add an inspector slot I can drag subclasses of Skill onto in order to customize those items in edit mode. My class currently reads:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Skill {
public string name;
public int id;
public float castTime;
public float cooldown;
public Skill(string skillName, int skillID, float skillCastTime, float skillCooldown){
name = skillName;
id = skillID;
castTime = skillCastTime;
cooldown = skillCooldown;
}
public void Use(GameObject npc){
}
}
I created a new variable of type Skill named itemSkill in my base item class, and in my database I'm trying to expose it with:
Skill itemSkill = EditorGUILayout.ObjectField(item.skillFunction, typeof(Skill), false);
However this generates an error, "The best overloaded match for `UnityEditor.EditorGUILayout.ObjectField(UnityEngine.Object, System.Type, params UnityEngine.GUILayoutOption[])' has some invalid arguments". Is there an obvious syntax error, or am I not using EditorGUILayout properly?
Try casting?
Skill itemSkill = (Skill)EditorGUILayout.ObjectField(item.skillFunction, typeof(Skill), false);
Hmm, that yields a different error, "best overloaded match has some invalid arguments", but the documentation says that it wants an object, a typecast, and the value of AllowSceneObjects, and I'm giving it all three.
I never had much experience coding for the Editor, so I'm not super sure what to suggest. I seem to recall that your first argument [can? must?] also be on the left-hand of the assignment operator, like in this snippet I dug up below. Also, guess I used an as-cast, not an implicit.
this$$anonymous$$ember.prefab = EditorGUILayout.ObjectField(this$$anonymous$$ember.prefab, typeof(GameObject), false) as GameObject;
Wait a sec... It's possible ObjectFields are only for components...? How else would you assign to them in the Inspector? I hate forgetting stuff I learned a year ago!
If that's the case, is there a function that can be used to expose custom classes? There must be...
Answer by isador34 · Aug 28, 2014 at 08:36 AM
for my editor I use this code: public ItemManager itemManager;
itemManager = EditorGUILayout.ObjectField(itemManager, typeof(ItemManager)) as ItemManager;
I'm afraid that still gives me the same overloaded method match has some invalid arguments error. :(
yes Because it should be "Object".
UnityEngine.Object
$$anonymous$$y good sample is here.
using UnityEngine;
using UnityEditor;
public class $$anonymous$$yEditor : EditorWindow
{
[System.Serializable] // needed.
class Hoge
{
public bool boolHoge;
public int intHoge;
public string strHoge;
}
[SerializeField] // needed.
Hoge[] hoges = new Hoge[]
{
new Hoge
{
boolHoge = false,
intHoge = 1,
strHoge = "2",
},
}
void OnGUI ()
{
ScriptableObject target = this;
SerializedObject so = new SerializedObject(target);
// set the propery name.
SerializedProperty stringsProperty = so.FindProperty("hoges");
EditorGUILayout.PropertyField(stringsProperty, true);
}
}