Hello there.
I did a scriptable object to memorize some datas in it, like a database supported by Unity.
The object itself is quite simple, but the datas it contains need solid constraints and support, meaning I can’t really use the inspector as it is too small to contains all the controls.
The answer is to use an EditorWindow. But, an editor isn’t directly related to any object, it is just a window.
So I’m trying to link the window to the currently selected object, so I can edit it in the window instead of the inspector.
So I written all of these :
My scriptable object
using UnityEngine;
using System.Collections;
public class SpriteAnimation : ScriptableObject {
// Many datas here
}
I store the scriptable as .asset files in a random folder of my unity project.
My editor window
public class SpriteAnimationWindow : EditorWindow {
[MenuItem ("Excellence/Editors/SpriteAnimation")]
public static void ShowSpriteAnimationWindow()
{
Object[] selection = Selection.GetFiltered(typeof(SpriteAnimation), SelectionMode.Assets);
if(selection.Length > 0)
{
SpriteAnimation anim = selection[0] as SpriteAnimation;
if(anim != null)
{
SpriteAnimationWindow window = GetWindow<SpriteAnimationWindow>();
window.m_anim = anim;
}
}
}
public static void ShowSpriteAnimationWindow(Object target)
{
SpriteAnimationWindow window = GetWindow<SpriteAnimationWindow>();
window.m_anim = target as SpriteAnimation;
}
private SpriteAnimation m_anim;
private SerializedObject m_object;
public void OnEnable () {
if(m_anim == null)
{
Debug.LogWarning("Oops");
return;
}
m_object = new SerializedObject(m_anim);
}
public void OnGUI () {
if(m_object == null)
return;
m_object.Update ();
// My controls
m_object.ApplyModifiedProperties();
}
}
ShowSpriteAnimationWindow allow to create the window, directly from a script, or using the selection.
By problem is : if I select and object, the m_anim reference is null whatever happen.
I tried to open it using the target attribute from the inspector, but the same apply :
[CustomEditor(typeof(SpriteAnimation))]
public class SpriteAnimationEditor : Editor {
public override void OnInspectorGUI()
{
if(GUILayout.Button("Open Editor"))
{
SpriteAnimationWindow.ShowSpriteAnimationWindow(target);
}
}
}
So, what should I do to assign a valid object to my editor window instead of null?
Should I save the asset manually and not rely on SerializedObject?