Load a script in build mode ?

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)

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.

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 !!

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.

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 ?

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

Mmm nice !

I will do some search on that.
Thanks !

Just found that and it’s looking interesting

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 ?

You just have to place the dll in your project to use it.

That is not the problem…

I make a new post here
http://forum.unity3d.com/threads/238792-How-to-use-DLL?p=1584298#post1584298

I reported that post, because double posting is not allowed.

Why should that not work?

Assembly a = Assembly.LoadFile("Addon/Test.dll");
foreach (Type t in a.GetTypes())
    if (...make sure t inherits MonoBehaviour...)
        gameObject.AddComponent(t);

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:

Can't add component because class 'Test.Class1' doesn't exist!
UnityEngine.GameObject:AddComponent(String)
Data:Awake() (at Assets/Script/Utilities/Data.cs:21)

A try with the name of the class:

Can't add component because class 'Class1' doesn't exist!
UnityEngine.GameObject:AddComponent(String)
Data:Awake() (at Assets/Script/Utilities/Data.cs:21)

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.

Yeah !!!

AddComponent(Type)

it’s kinda working :smile:

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

THANKS

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 :

InvalidCastException: Cannot cast from source type to destination type.
...

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 :slight_smile:

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.

Usually, you will put that class in your “game” library, and have your “plugin” library reference it.

Consider an additional language such as LUA.

http://www.gfootweb.webspace.virginmedia.com/LuaDemo/