• 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 Nerosis · Jan 04, 2010 at 10:26 AM · editor-scriptingassetbundlemonocompiling

Dynamically loading scripts

My company is using Unity to create an MMO. If possible I'd like to split the game up into self contained blocks (with occasional dependancies on each other if necessary) containing assets and code, that can be downloaded by the users. The game can then load the blocks at run time and assemble the world. I've managed t$$anonymous$$s quite well for the assets using asset bundles and LoadLevelAdditive(), but I can't find a way to do the same with scripts.

How can one distribute and dynamically load scripts without using the one monolit$$anonymous$$c DLL that Unity builds?

Edit: I'm using the standalone Windows player.

Comment
AngryAnt
save
Verm

People who like this

3 Show 2
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 AngryAnt ♦♦ · Jan 04, 2010 at 11:36 AM 0
Share

I've actually had an example of how to do this lying around for some time - intended for blogging. I'll dust it off, upload it and link it here later.

avatar image Nerosis · Jan 05, 2010 at 07:43 AM 0
Share

That would be much appreciated, AngryAnt!

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by AngryAnt · Jan 05, 2010 at 09:15 AM

I've got a blog post on the subject here: Downloading the hydra. The trick is basically t$$anonymous$$s:

  1. Compile the external scripts to a .net assembly (the example I provide in the blog post also shows how to have t$$anonymous$$s use UnityEngine functionality).
  2. Download t$$anonymous$$s via the WWW class.
  3. Load the bytes of it into the runtime as an assembly.
Comment
jonas-echterhoff
Gunder
pragmascript
save
flamy
drawcode
qwee
AurimasBlazulionis
michaelstv
Good_Venson
sachin4dotnet

People who like this

11 Show 7 · 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 Nerosis · Jan 05, 2010 at 09:41 AM 0
Share

Thanks, AngryAnt. :-) I'll work through this, but this looks to be exactly what I had in mind. I'll post feedback tomorrow.

avatar image Nerosis · Jan 06, 2010 at 09:32 AM 0
Share

I got the compilation working by downloading and installing Mono as you suggest in the blog post, and managed to execute the external script. I haven't managed to get it working with the Mono framework that ships with Unity, but that's not a train smash. Thank you. :-)

avatar image pragmascript · Jun 30, 2011 at 11:45 PM 0
Share

you have to be careful so no one injects harmful code in your assembly. you should encrypt and sign the packages.

avatar image save · Mar 08, 2012 at 12:06 PM 0
Share

Legendary AngryAnt, thanks for sharing this!

avatar image tao.tao · Jun 02, 2015 at 02:02 AM 0
Share

hey, ant , your download link is invaild now, i can look the blog but can't get the source code.

Show more comments
avatar image

Answer by Ashkan_gc · Jan 05, 2010 at 08:32 AM

you can create an assetbundle containing all of your scripts. then you can get all of them from your assetbundles. you can seporate them by their names. it's a better way because can be used with web player too. i am waiting for angryant's solution but it can work for you. also i should mention scenes contain scripts too. even you can run codes generated at runtime by using a class in mon called "C# compiler as a service" t$$anonymous$$s class can execute C# code strings. the class is about 3MB and can be downloaded from mono's web site.

Comment
AngryAnt

People who like this

-1 Show 2 · 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 AngryAnt ♦♦ · Jan 05, 2010 at 09:16 AM 0
Share

The asset bundles do not contain the source, but rather the code is already compiled into your build.

avatar image Nerosis · Jan 05, 2010 at 09:22 AM 0
Share

It seems from the forums and my experimentation that asset bundles only contain references to the compiled scripts. The actual script is stored on the project itself. If I add a prefab that references a script to an asset bundle, and then delete the script from the project, the script no longer executes when I import the asset bundle (although the rest of the prefab is there). The same applies to scripts in scenes. I looked at Mono's "compiler as a service" - it could work, but it's going to require a lot of jumping through hoops. I would rather compile a DLL when the asset bundle is created.

avatar image

Answer by jonas-echterhoff · Jan 04, 2010 at 11:17 AM

If you have a standalone executable, you should be able to download dlls to disk, and dynamically load functions from them using the usual .NET/mono methods (don't actually know what those are, but google should find the answer to that).

If you are using a web player, then why do you need to do t$$anonymous$$s in the first place? You can always just change the code for the main web player when you need to. Yes, i can still see some valid reasons - code download size, or changing code dynamically w$$anonymous$$le playing or possibly the setup of your build pipeline - but if you describe your situation more detailed, maybe that would help coming up with solutions or workarounds.

Comment

People who like this

0 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 Nerosis · Jan 05, 2010 at 07:43 AM 0
Share

I should have specified that I'm using the standalone Windows player - I've edited the question. :-)

Loading DLLs at runtime should work well. I'm not sure how to compile only a subset of the Unity scripts in the project into a DLL - I'm not sure how to compile for Mono, and what parameters to pass to the compiler, or what other steps Unity goes through to create the DLL.

avatar image

Answer by metmat · Jun 30, 2011 at 11:14 PM

Any reason to suppose that AngryAnts solution for downloading compiled scripts also work on iphone and android apps?

Comment

People who like this

0 Show 4 · 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 Mene · Mar 08, 2012 at 12:02 PM 0
Share

For me it worked on Android without problems, but iOS made some problems yesterday. However, there were native exceptions, coming from semaphores. Not sure why (I didn't use any threading explicitly), but I hope I can investigate this further today.

avatar image AngryAnt ♦♦ · Aug 30, 2012 at 08:53 AM 1
Share

This will not work on iOS as the platform license prohibits execution of arbitrary code. Additionally, because of that restriction, we actually do not have a running JIT compiler, but in stead AOT compile everything. Therefore it would also be technically not feasible.

avatar image nosignal · Oct 12, 2012 at 04:47 PM 0
Share

Hi AngryAnt. I'm sure you understand this better than me, but could you get mono to compile AOT to a .so file (http://www.mono-project.com/AOT) and then use the so instead of a dll?

avatar image nosignal · Oct 12, 2012 at 05:05 PM 0
Share

Hi again AngryAnt. Has this iOS restriction changed now that Apple offers In-App Purchases? According to https://developer.apple.com/news/pdf/in_app_purchase.pdf "In-App Purchase will allow you to sell: ... Additional game levels". Does that mean the In-App Purchase can introduce new functionality by introducing new code? Or is it only able to unlock existing code?

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

Components Inside A DLL 3 Answers

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

Build from script 3 Answers

Export list of prefab to asset bundles recursively 0 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