I’m building a package which is based on scriptable objects. I’d like to add some icons so that its easier for users to recognize these assets. In my package, I would like so that if the users decide to implement their custom scriptable objects, they would also receive the icons.
As an example, I have an abstract BaseSO scriptable object which looks like the following:
public abstract class BaseSO : ScriptableObject
{
}
Then, I set an icon for the BaseSO script:
Next, lets say a user creates a custom implementation for BaseSO:
[CreateAssetMenu]
public class ChildSO : BaseSO
{
}
And an asset:
However, the icon is not applied. How could I got about making sure that the icons get applied on each scriptable object which inherit the BaseSO?
Well, you can call non-public API in C# any time and you can write an editor script to change the icons when you save a script and you detect that it inherits from another script (although it can get messy, probably you want to use a Regexp for that I guess).
Some help https://answers.unity.com/questions/213140/programmatically-assign-an-editor-icon-to-a-game-o.html but I haven’t tried it.
IIRC there is some undocumented secret way to do this by using the Gizmos folder and naming the file something like Icon.jpg and it will automagically connect them. It’s not ideal, and I don’t think its worth recommending.
Finally got around to finishing this, however in a super hacky way where I used the Unity Atoms repository as an example. I couldn’t manage to get this working using SetIconForObject and other suggestions.
I followed a similar approach where I hack the .meta file in order to set the icon. However in my case it was a bit tricky as I didn’t want to force my users to apply attributes on each class:
Collect candidates for icon processing in OnPostprocessAllAssets (script asset paths)
Persist script asset paths in EditorPrefs
Retrieve script asset paths in a method marked with [DidReloadScripts] from EditorPrefs
Load MonoScript from each script asset path
Check type info, find the applicable icon path by inspecting the base class
Write the icon path using the Unity Atoms approach to the .meta file