Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dynamicly change code

Discussion in 'Scripting' started by Orion_78, May 12, 2015.

  1. Orion_78

    Orion_78

    Joined:
    Feb 13, 2014
    Posts:
    66
    Hi,

    I am looking to load a .dll when starting a standalone windows build.
    I succeed to do so with such code :

    Code (CSharp):
    1.  
    2. public class DllLoader : MonoBehaviour
    3. {
    4.     void Awake()
    5.     {
    6.         string path = "file://" + Application.dataPath + "/Assemblies/dynamic.dll";
    7.  
    8.         var assembly = System.Reflection.Assembly.LoadFrom(path);
    9.  
    10.         // Instantiate a GameObject and add a component with the loaded class
    11.         GameObject go = new GameObject("Plugin");
    12.         go.transform.parent = transform;
    13.  
    14.         go.AddComponent(assembly.GetType("InputHandler"));
    15.         go.AddComponent(assembly.GetType("WorkingStuff"));
    16.        
    17.     }
    18. }
    So on start, it load a .dll from path and add component InputHandler and WorkingStuff. And it works!

    Everything seems to be happy until I change some behaviours in the .dll and notice it didn't change when I restart the game!

    The thing is I need to communicate with InputHandler within the Unity project AND the .dll code. Both need to access and modify this same class.

    How can I share a class between a dynamicly loaded .dll and Unity code ?
    Another way: how can I replace a Unity compiled class by an imported one (from a .dll)

    Any help would be appreciated.
    Thanks.
     
    Last edited: May 12, 2015
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Does "I change some behaviours in the .dll" mean that you change the library source code and recompile it?
    The first idea that comes to mind, you forgot to update your .dll in standalone application folder.

    What is the exact problem?
    By "modify the same class" do you mean modify the same instance of the class or modify static members of the class?

    Does it mean that you have two identical classes, one of which is declared in Unity script files and the second one is declared in you .dll? If so, these are two absolutely different classes.

    --
    I'm sorry to ask more questions than give answers, but first I need to know exactly what you are doing and what the actual problem is.
     
  3. Orion_78

    Orion_78

    Joined:
    Feb 13, 2014
    Posts:
    66
    Ok, I figure it out.
    It happend that Unity was loading my pre build in .dll (I had drag and drop inside the asset folder) insdead of loading the new one.

    So I manage to remove the .dll from my asset folder and now it is working !