- Home /
Create custom asset and add editor window
Hi,
I'm trying to create custom asset with its own editor window. I have the following code for creating my asset:
[CreateAssetMenu(fileName = "My new asset.myasset", menuName = "Create My New Asset")]
[System.Serializable]
public class MyAsset : ScriptableObject {
[SerializeField, Multiline]
public string description;
}
And the following for opening editor window:
[UnityEditor.Callbacks.OnOpenAsset(1)]
public static bool OnOpenAsset(int instanceID, int line)
{
if (Selection.activeObject as MyAsset != null)
{
ShowEditor();
return true;
}
return false;
}
The problem is Selection.activeObject returns null when converted to MyAsset. Debugging shows that when I select my asset, it is of type DefaultAsset, but other assets like Animation Controller or a Game Scene have the correct types.
What's wrong with open Editor Window manually or add a custom shortcut keyboard
I haven't used this API before, but based on the example in the scripting reference instanceID
should be converted into an Object
using EditorUtility.InstanceIDToObject(instanceID)
. Try to "cast" that to $$anonymous$$yAsset
, maybe Selection.activeObject
is not valid for custom types.
@Harinezumi I tried your method, sadly, it doesn't work. The object I get from this method is the same as Selection.activeObject.
@NoDumbQuestion it's not very convienient, but if nothing else works, I'll just have to go with that.
Answer by Sirozha1337 · Jun 09, 2018 at 04:37 PM
I solved it. The problem was here:
[CreateAssetMenu(fileName = "My new asset**.myasset**", menuName = "Create My New Asset")]
I provided my own custom extension for file, which for some reason doesn't play well with Unity. It only works without custom '.myasset' extension.