Skip to content

Instantly share code, notes, and snippets.

@iwashihead
Last active August 18, 2017 09:06
Show Gist options
  • Save iwashihead/54ac2a1988494bb4cf72b2121ee84d73 to your computer and use it in GitHub Desktop.
Save iwashihead/54ac2a1988494bb4cf72b2121ee84d73 to your computer and use it in GitHub Desktop.
無理やりScriptableObjectのインスペクターにUnityEventを表示させる
[CustomEditor(typeof(Validator))]
public class ValidatorInspector : Editor
{
public override void OnInspectorGUI ()
{
var validator = target as Validator;
var width = EditorGUIUtility.currentViewWidth - 20;
var drawer = new UnityEventDrawer ();
if (GUILayout.Button ("Validate")) {
validator.DoValidate ();
}
var sp = serializedObject.FindProperty ("ue");
drawer.OnGUI (new Rect (new Vector2 (10, 80), new Vector2 (width, EditorGUI.GetPropertyHeight (sp))), sp, new GUIContent ("ue"));
}
}
[Serializable]
[CreateAssetMenu]
public class Validator : ScriptableObject
{
public UnityEvent ue;
}
@iwashihead
Copy link
Author

iwashihead commented Aug 18, 2017

結論から言うと表示させることはできるけどかなりインスペクタ拡張しないとダメで実用性低い。
UnityEventDrawer(カスタムプロパティドロワー)のOnGUIメソッドがoverloadされてて片方しか表示の対応をしていない。
ScriptableObjectのOnInspectorGUIがなぜMonoBehaviourと違うのかはわからないが、SrciptableObjectだと
表示対応されていないOnGUIを呼び出していた模様。
ScriptableObjectをインスペクター拡張して対応してる方の OnGUIを呼び出せば一応は解決。

default

@iwashihead
Copy link
Author

iwashihead commented Aug 18, 2017

あとインスペクターの横幅の取得方法
var width = EditorGUIUtility.currentViewWidth - 20;

と、シリアライズプロパティの高さの取得方法
EditorGUI.GetPropertyHeight (sp)

がわかったのは地味に嬉しい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment