- Home /
how can i put components in external dlls and store dlls in asset bundles
i want to store my components in assemblies that i define and then put them in asset bundles. in this way, new functionality can be added to games. complete patches are easily possible. so can i create a car mesh and a car.dll assembly and make an asset bundle of them and then download it from my game and use it with it's new scripts?
there is not any info about components in external assemblies nowhere. where should i put the dll? where should i put that for web players. where unity looks for external components. for example if i say
CarScript s = new CarScript();
this.gameObject.AddComponent(s);
this is not real code.
where unity looks for the component?
Answer by Ashkan_gc · Apr 03, 2011 at 01:18 PM
now it's possible to put components in dlls and use them. unity will recognize them in the editor. also i think from 3.2 we can add components from assemblies loaded in runtime as macfanpro answewred here. reflection is not needed anymore and you just need to use AddComponent with the type that is in the dll.
But where in the file system do I put the dll so Unity can find it?
Answer by ckfinite · Apr 02, 2011 at 04:58 PM
This is a quite complicated question, for a couple of reasons. First, Unity was not really intended for run-time modification of behaviors from external sources, and as such, you cannot really use, for instance, MonoBehavior from a outside DLL. However, there is a way around this, and that is to use your own behaviour class that exposes the same virtual functions as MonoBehavior and load those using C# reflection from a external DLL.
What you could do is make a class like this:
public abstract class ExtBehavior {
public virtual void Initialize()
{
}
public virtual void Update()
{
}
}
Then extend it like this
public class TstBehaviour : ExtBehavior {
public override void Initialize()
{
}
public override Update()
{
}
}
You will need to put the external DLL containing ExtBehavior into Library/Frameworks, under the Unity folder. This is so the compiler can resolve the type of ExtBehavior.
Then load the behaviors like this
Assembly assem = Assembly.LoadFrom("nameOfDLL.dll");
IEnumerable<Type> behaviors =assem.GetTypes()
.Where(type=>typeof(ExtBehavior).IsAssignableFrom(type));
Now, when you need a component, you just iterate through this array of Types until you find the one you need, construct it, cast it, then use it. I recommend creating a normal MonoBehavior that takes the name of the component that is loaded, as well as a Dictionary that takes parameters, then loads this dictionary into the external behavior through a protected constructor in ExtBehavior.
Answer by Ray-Pendergraph · Oct 14, 2010 at 01:05 PM
I won't say you can't store it but I think the problem is that how do you get the unadulterated DLL bits out of the bundle? We tried this with other binary formats and the rather ugly workaround was to Zip compress then Base64 encode the bits as a text asset. Bad, bad, bad... but it was right before the release. It blew the game size up like a balloon. If you could figure out a way to say "Get this blob as raw bits" you could load the DLL internally and use it with some C# trickery.
If you figure out how to do this with a DLL I would love to know... I'll update the community page.
there is a good way to do that easily. you can use file reading system.IO of .net to read a encoded file (package of your pdfs) and then decode it and use it. for dlls, using reflection is possible but not as easy as i want.
Sure you can read anything with System.IO but that gets tricky with a varying deployment base. What about web player, when will the content get installed to the drive?
i just updated the answer with the up to date information.
Answer by LtKelleyUSMC · Jan 13, 2013 at 01:33 AM
Hey there people, I have actually created an external DLL, and have it working with MonoDevelop, and Unity3D. Check out my video at http://www.youtube.com/watch?v=M4PoAc3XQKI&feature=youtu.be I will be creating a tutorial video on how to do this soon.
Please answer the question here in place, if at all possible.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075633im_/https://answers.unity.com/themes/thub/images/avi.jpg)