Create asset from assembly

I’m trying to create a custom asset type, and can get it to work from scripts, but not when I compile the code into an assembly.

I have an asset class which extends UnityEngine.ScriptableObject (extending UnityEngine.Object doesn’t work, even using scripts, despite the signature of AssetDatabase.CreateAsset()), and I have a function which creates an instance of my asset (with ‘new’) and calls AssetDatabase.CreateAsset() on it.

public class MyAsset : UnityEngine.ScriptableObject {}

public class MyAssetMaker {
    [MenuItem("Test/Create Asset 1")]
    static void CreateMyAsset() {
        MyAsset asset = new MyAsset();
        AssetDatabase.CreateAsset(asset, "Assets/test.asset");
    }
}

When these classes are defined in Assets/MyAsset.cs and Assets/Editor/MyAssetMaker.cs respectively, all is well. If I move them out of the project, compile them into an assembly, and put the assembly into Assets/Editor, I get the following problems:

    1. Unity doesn’t create the menu item; I have to create an extra script and register a menu item there; I have that function call through to MyAssetMaker.CreateMyAsset()
    1. Unity reports an error on ‘new MyAsset()’: “Instance of MyAsset couldn’t be created because there is no script with that name.”
    1. Unity reports a NullReferenceException at the line where AssetDatabase.CreateAsset() is called (it’s reported as thrown in CreateMyAsset(), not CreateAsset(), but I think that must be a reporting error…)

What am I doing wrong?.. I anyone who has gotten this working can offer some insight, I’d appreciate it!

you can not have unity engine classes outside as they will file to comply to the requirements.

if you want to use such classes you must have “binding classes” within unity basically

To be clear, I’m not trying to run code with Unity dependencies outside of Unity; only compile it, and then run it from within Unity via an assembly.

If it’s really not possible to do this directly, perhaps I can look at an alternative approach; I assume by “binding classes” you mean some form of delegate/proxy class defined in a script that handles calling Unity APIs that don’t work from a DLL?

is that limitation true yet? I thought that was supposed to be supported from 3.0 on - quoting Unity 3.1 “What’s new”…

… but I’m having the same errors as laurie’s. Anyone managed to get EditorWindows alike working from a DLL?

The limitation no longer holds for U3

I’m still having the same error message “Instance of MyAsset couldn’t be created because there is no script with that name.” even in latest Unity 3.1.0f3.

Everything runs fine from C# source code. But when I compile an assembly using MonoDevelop that error occurs. If I suppress EditorWindows etc the assembly runs flawlessly.

I’m not sure if that may be a bad consequence of using a regular MonoDevelop install (not Unity’s) in order to compile the assembly. Actually, I did this because I failed to compile assemblies from Unity’s version. Apparently nothing gets generated when hitting “Build”. Is that anyhow different for anyone else? Can you guys compile assemblies from Unity’s MonoDevelop?

thanks!

I am able to compile assemblies from within Unity’s version of MonoDevelop just fine, but I still have the same basic problem: I can’t instantiate any editor classes from an assembly.

My code looks like:

public class GameDataEditor : EditorWindow {
	...
	
	[MenuItem("Window/GameData Editor")]
	static void OpenGameDataEditor() {
		EditorWindow.GetWindow(typeof(GameDataEditor), false, "GameData");
	}
	
	void OnGUI() {
		...
	}
}

The menu option actually DOES show up, which indicates that the Unity editor is reflecting into the assembly and finding the attributes like MenuItem. However, when I try to call it, I get basically the same error you’ve been getting:

Instance of GameDataEditor couldn't be created because there is no script with that name.
UnityEditor.EditorWindow:GetWindow(Type, Boolean, String)
GameDataEditor:OpenGameDataEditor() (at /Users/michaelpowell/Projects/unity/delve/dev/SOIEditorLib/GameDataEditor.cs:19)

I don’t really have a good solution. Sure, I could create a proxy object outside the assembly, but that defeats most of the purpose of putting it in an assembly to begin with. I want two things out of it:

A) To have an extension to the editor which is entirely encapsulated in a single file, that can be easily dropped in or swapped out.
B) Unity classes that are less dependent on the Library folder, so they can be more easily version controlled.

It appears I can have neither of these things with an editor assembly, so the assembly becomes useless to me.

Still using assemblies for most of my non-editor code, though.