Another way to phrase this question would be "Get ScriptableObject from selection in editor code".
I have a public class DialogueObject : ScriptableObject from which I create many SOs. I'd like to automate some actions around these SOs like populating data using editor script but I can't figure out or Google-Fu how to get a handle on them.
This does not work (NullReferenceException):
DialogueObject obj = Selection.activeGameObject.GetComponent<DialogueObject>();
So, how to select a ScriptableObject in the project window and edit it's data via editor scripting?
Update:
I discovered that taking it as a public variable and assigning it in the inspector works (obj = (DialogueObject)EditorGUILayout.ObjectField("DialogueObject", obj, typeof(DialogueObject));), but I'd prefer to lift it from the Selection object instead, so still looking for help.
In this case you need a custom
EditorWindow. And in this editor window we can use one of the Unity Editor Events which isOnSelectionChange(), by events I mean all the message methods which are called by the Unity itself ie. Awake, OnEnable, Start, Update, etc...OnSelectionChange()will be called when ever you select an object, regardless ofScene HierarchyorProject View. Now you just have to get a selected object usingSelection.activeObject;. After getting the current selected object you can compare and cast it to your SO type. and can draw a custom editor based on your needs.Below is an example of a such simple:
Hope this gets you to your solution...✌️
Referances: EditorWindow EditorWindow.OnSelectionChange()