Importing scripts through Asset bundles

I am trying to load new scripts through asset bundles. By reading the manual on this topic this is what i did:

I created a .dll file for test purposes(pre-compiled assembly means .dll file, right?) This is the content of that .dll file

using System;
namespace TestLibrary{
	public class MyClass{
		public string str = "If u see me, DLL thing works";
		public MyClass (){}

I copied this file in my unity assets folder and tried to export this as an asset bundle but this warning came up

Unrecognized assets cannot be included in AssetBundle: “Assets/TestLibrary.dll”.

I changed the file extension from .dll to .txt and built it again(Could this mess things up?) Here is my code for building the assetbundle (I kind of copied this from a source I am not exactly sure how each of this parts work):

static void ExportResource2(){
	string path = "AssetBundles/scriptbundle01.unity3d";
	Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
	BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,
		                               | BuildAssetBundleOptions.CompleteAssets);

I loaded the asset bundle and tried to load a class from it (even though the class does nothing right now) I followed the code from the manual:

IEnumerator Start () {
    		// Start a download of the given URL
    		WWW www = WWW.LoadFromCacheOrDownload (url, 0);
    			yield return null;
    		// Wait for download to complete
    		// Load and retrieve the AssetBundle
    		AssetBundle bundle = www.assetBundle;
    		// Load the TextAsset object
    		TextAsset txt = bundle.LoadAsset("TestLibrary.txt", typeof(TextAsset)) as TextAsset;
    		// Load the assembly and get a type (class) from it
    		var assembly = System.Reflection.Assembly.Load(txt.bytes);
    		Type type = assembly.GetType("MyClass");

I have replaced bundle.Load() with bundle.LoadAsset() as the prior is deprecated. It generates the following error:


I am not sure whet exactly are the mistakes i am making.
Any kind of help will be very much appreciated. Also, How am I supposed to get the reference to that string in the “MyClass” dll from my unity code if the class would ever instantiate…

One last thing if i want to include Monobehaviour classes in my dll should reference to UnityEngine be enough?

It seems to me that you’re just using a text file and rename that to .dll. This is not going to work. You actually need to compile that script of yours into a real DLL using the mono compiler. Then you can rename that dll to .txt so it becomes a text asset and then you can use the reflection code to load the classes inside the DLL (on platforms that support this).

You can compile a C# script to a DLL using MonoDevelop, Visual Studio, the Mono Compiler command line or you use the CSharpCodeProvider class to build the DLL through editor scripting. Another convenient way of building a DLL from within the Unity editor is uTomate’s build DLL action ( (which uses the CSharpCodeProvider solution internally).