pitar parkar pitar parkar - 3 months ago 30
C# Question

Unity 5 loading scripts at runtime from build directory

I want to attach a script to a empty gameObject at runtime using addComponent.
But, i don't want to drag the script directly into the project, i want to first build the project and the go into the build folder and drag it there.
After the game starts, i want to load it from there and attach it to the empty gameObject.

Answer

If I don't misunderstand you, that's possible.

  1. Read C# codes from a text file using System.IO.File.ReadAllText(...) or Resources.Load(...) as TextAsset.
  2. Use CSharpCodeProvider to dynamically generate a dll.
  3. Load the Component type from the dll into the AppDomain.
  4. Create a object of the type.
  5. Attach it to the gameObject.

More on this here: How to load a class from a .cs file

Comments