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

Load a script in build mode ?

Discussion in 'Scripting' started by WoofWoofDude, Apr 6, 2014.

  1. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    Hi !

    Just make some search and didn't find the answer...

    Is it possible to load a script in an external folder from the build mode (.exe) ?
    It is for add more functionality to my game, like modding..

    Thanks !

    (In C# if it's possible)
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    A "script" doesn't exist for your deployed game. Like any software, your "script" is first compiled.

    What you can do, is use a .DLL to add functionality.
     
  3. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    But in a DLL file it will not allow me to add a class to an object ?

    If yes, can you explain to me how I can make it !!
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Class; written definition - blueprint - of an object.

    Object; Allocated memory space which size and content is define by a class.

    Do you mean adding an object - which class is deriving from MonoBehaviour - to a GameObject? If so, I don't see why it wouldn't work.
     
  5. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    I didn't work with DLL before..

    I create a C# script/class (inherited from MonoBehaviour) and I want to add it to a GameObject yeah (I know I need to make an instantiation of the class as an Object). But that script will be in another folder when the game will be build. How I can add it to my GameObject ? This is my question.

    You said I can use DLL for that.. Right ?
    How I can ?
     
    Last edited: Apr 6, 2014
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    When Unity build your code for deployment, it does exactly the same thing as you would do for building a DLL, turning your written code into bytecode.

    You usually load a DLL - while using C# - by using the method Assembly.LoadFile, which returns the loaded Assembly.
    Once loaded, you can access its internal type definition and use them the same way you would with your "scripts", such as AddComponent(myType).

    http://msdn.microsoft.com/en-us/library/b61s44e8.aspx
     
  7. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
  8. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    I make some search on DLL but I don't know how get all types and use it in my script ?

    I want to use DLL's classes every where.. I don't know all class' names because it's a mod.. But all class in DLL inherited from a sub class of MonoBehaviour, so I can use them as a component and add them to a GameObject, normaly ?

    But After loading the DLL with Asembly.LoadFile(..), I don't know how I can use DLL's classes ?
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You just have to place the dll in your project to use it.
     
  10. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    Last edited: Apr 6, 2014
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I reported that post, because double posting is not allowed.

    Why should that not work?
    Code (csharp):
    1. Assembly a = Assembly.LoadFile("Addon/Test.dll");
    2. foreach (Type t in a.GetTypes())
    3.     if (...make sure t inherits MonoBehaviour...)
    4.         gameObject.AddComponent(t);
    5.  
     
  12. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    Realy Dantus ? it's not a Double post.. it's another question coming from this post..

    Because the type t doesn't existe in the AssetDatabase so Unity don't reconise t as component. (I think)

    A try with the fullname:
    Code (csharp):
    1.  
    2. Can't add component because class 'Test.Class1' doesn't exist!
    3. UnityEngine.GameObject:AddComponent(String)
    4. Data:Awake() (at Assets/Script/Utilities/Data.cs:21)
    5.  
    A try with the name of the class:
    Code (csharp):
    1.  
    2. Can't add component because class 'Class1' doesn't exist!
    3. UnityEngine.GameObject:AddComponent(String)
    4. Data:Awake() (at Assets/Script/Utilities/Data.cs:21)
    5.  
     
    Last edited: Apr 6, 2014
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You were using the string variant of AddComponent, did you also try the AddComponent(Type) variant? Like that you don't have to care about the name.

    As it is a continuation, there is no need to create a new thread.
     
  14. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    Yeah !!!

    AddComponent(Type)

    it's kinda working :D

    I will make some test about it and come back if I get some problems..

    THANKS
     
  15. WoofWoofDude

    WoofWoofDude

    Joined:
    Apr 6, 2014
    Posts:
    12
    Mmm I have a problem..

    Every class in the dll inherited from OutputStream wich is in my main game dll..
    Normaly every things looking good.. But I can't cast new modded class to OutputStream :

    Code (csharp):
    1.  
    2.  
    3. InvalidCastException: Cannot cast from source type to destination type.
    4. ...
    5.  
    I think I get conflict over there.. The OutputStream of the DLL "isn't" the same of the game (but it's the same :/).. Can it's be a possibility ?

    -------------

    It's fine.. The dll don't use the updated main dll :)
     
    Last edited: Apr 6, 2014
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You can't have the same class in the dll that you are loading and in the actual game. You need to find a solution that only requires one of them.
     
  17. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Usually, you will put that class in your "game" library, and have your "plugin" library reference it.
     
  18. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221