- Home /
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.CollectDependencies
| 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
Caching.CleanCache();
WWW www = WWW.LoadFromCacheOrDownload (url, 0);
while(!www.isDone){
yield return null;
}
// Wait for download to complete
Debug.Log("asda");
// 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");
Activator.CreateInstance(type);
Debug.Log(type.Name);
}
I have replaced bundle.Load() with bundle.LoadAsset() as the prior is deprecated. It generates the following error:
BadImageFormatException
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?
Answer by kork · Feb 09, 2016 at 08:08 PM
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 (http://www.ancientlightstudios.com/utomate/documentation/actiondocs/5.3/build_dll.html) (which uses the CSharpCodeProvider
solution internally).