• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by wingsoftheblackbird · Sep 10, 2015 at 12:34 PM · assetbundledllbuildpipelinebuildassetbundle

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?

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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).

Comment
vahid62

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Build Assetbundles outside project? 0 Answers

BuildAssetBundles() error: An abnormal situation has occurred: the PlayerLoop internal function has been called recursively. 0 Answers

Can not implicitly convert a unityEngine.GameObject from unityEngine.Coremodule.dll to unityEngine.GameObject from Unityengine.dll 1 Answer

Give error after exporting AssetBundle by using BuildPipeline 0 Answers

Building asset bundles with dependencies in the player build? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges