• 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
1
Question by der_wolf · May 29, 2012 at 11:15 PM · assetbundle

How to compile script to include it to AssetBundle?

According to this manual I can include scripts as binary data and load them via Reflection. I understand all steps, except one: how can I compile script to get binary data for stroring it in .binary file? Is there any tutorial/help?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by der_wolf · Jun 03, 2012 at 02:22 PM

Solution:

  1. Open Monodevelop and create C#Library project

  2. Add UnityEngine.dll to References

  3. Compile your scripts

  4. Copy bin/.dll from your library project folder to Assets of your unity project and change extension from .dll to .bytes

That's all.

Comment
Add comment · Show 8 · 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
avatar image Terikon · Feb 16, 2013 at 01:10 PM 0
Share

The only problem with this approach is with $$anonymous$$onoBehaviours. Suppose you have some script (i.e. component) attached to GameObject. And you want to stream both in AssetBundle. You store the assembly (as TextAsset) and the GameObject in the AssetBundle, and load them in run time. But loading the assembly will not re-connect the script to the GameObject. You'll need to do it manually, with AddComponent. And that means you lose all the parameters you configured for the script. Which is bad. If there's some solution, it will be appreciated.

avatar image Ekta-Mehta-D · Sep 01, 2015 at 10:47 AM 0
Share

@Terikon did you get any solution for loading script in assets bundle ?

avatar image Bunny83 · Sep 01, 2015 at 12:09 PM 0
Share

@Ekta $$anonymous$$ehta D.

"Scripts" are not assets from the runtime's point of view. Unity compiles all scripts into an assembly which is included directly in the build. As mentioned above and in the docs you can use reflection to load a custom assembly at runtime. However the classes inside that assembly aren't recognised as "assets".

There's this old blog post of AngryAnt on that topic. I haven't tried loading an assembly recently. In the past i got it working but without using assetbundles at all. It might be possible to load an assetbundle with attached components from an external assembly as long as you load the assembly before loading the assetbundle as the classes should already be available when Unity loads the bundle. So either use two assetbundles (one for the assembly and one for the assets) or simply load the dll file seperately.

As i said i'm not sure if that will work but i think it should. Be careful to not "overlap" classes in your assemblies. So you should not include the same class in two assemblies. So if you have a class / script already in your unity project you can't include it in the custom assembly. If you need access to certain classes from your actual project, those classes should be compiled into a seperate "shared" assembly. That assembly would be included in the actual project. The advantage is that when you create your custom assembly you can simply reference the common assembly and have access to those classes.

Also note that once an assembly is loaded into memory it can't be "unloaded" at runtime only by restarting the application. That's a limitation of .NET / $$anonymous$$ono. The only way to do something like that is using a seperate AppDomain which can be unloaded entirely as a whole. Using AppDomains can be a real pain and they won't work for $$anonymous$$onoBehaviours. Also when i was testing this stuff i noticed that creating an AppDomain on Android seems to be not supported as the app always crashes at that point. $$anonymous$$aybe this has been fixed already

avatar image Ekta-Mehta-D · Sep 01, 2015 at 12:43 PM 0
Share

@Bunny83 i am trying to add script as a component of gameobject in assets bundle. I followed above points. but i stuck at 3 rd point that is compile your script .

I followed 2 steps and now i have confusion like how it will have reference of my script attached to game object after i put .bytes of that .dll in library project. ?

avatar image Bunny83 · Sep 01, 2015 at 03:03 PM 0
Share

@Ekta $$anonymous$$ehta D.

Well, all you have to do in $$anonymous$$onoDevelop is to click build / build PROJECTNA$$anonymous$$E in the menu or simply press F7 that will actually compile your project and it should create a DLL file in that project folder. This DLL contains your compiled classes,

Personally i only use Visual Studio (Express) which is far more stable and reliable than $$anonymous$$onoDevelop. However the process of building your project is almost identical ^^.

Show more comments
avatar image
0

Answer by rohankad · Sep 01, 2015 at 01:41 PM

We used to load asset bundles, in which the scripts were attached the objects. It worked for us. Unity automatically compiles the script attached to the GameObjects.

~Thanks

Comment
Add comment · Show 1 · 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
avatar image Bunny83 · Sep 01, 2015 at 04:10 PM 0
Share

That's simply because the scripts aren't included in the Assetbundle but are already part of your actual application. So when you load an asset from an assetbundle it has no problems finding the scripts. The question is more about adding those scripts later along with the assetbundle.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to import the object from server to unity 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to move value in the inspector to script in the assetbundle 0 Answers

yield on WWW? or AssetBundleRequest? or both? 2 Answers

Is it sensible to build an assetbundle for each texture/mesh? 1 Answer


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