- Home /
Dynamic Assembly Plugin
Hello,
Question: How to Load and GetType of a dynamic assembly which is using the current project in Unity?
I can't get my script to work inside Unity while the same concept works flawless in a regular .net environement. I'm trying to allow dynamically created plugins.
Here is the Unity side:
using UnityEngine;
using System;
using System.IO;
using System.Reflection;
namespace InUnity
{
public class MyLabel : MonoBehaviour
{
//Controlled by Assembly
public Vector2 MyLocation = new Vector2(0,0);
//Assembly object
object assemblyObject;
//The gui method in the assembly
MethodInfo ongui;
void Start()
{
//Get assembly
Assembly a = Assembly.Load(File.ReadAllBytes(@"E:\Desktop\LabelMover.dll"));
//Get class
Type assemblyClass = a.GetType("SCRIPT.LabelMover");
//Get Object
assemblyObject = a.CreateInstance("SCRIPT.LabelMover");
//Get methods
MethodInfo start = assemblyClass.GetMethod("Start");
ongui = assemblyClass.GetMethod("OnGUI");
//Launch Start(ref this)
start.Invoke(assemblyObject,new object[]{typeof(MyLabel),this});
}
void OnGUI()
{
//Call the plugin on GUI
ongui.Invoke(assemblyObject,null);
GUI.Label(new Rect(MyLocation.x, MyLocation.y, 150, 20), "Assembly Label !");
}
}
}
The next script is compiled using Mono Runtime 2.8 or CodeDom .net2.0 (both results in the same Unity error) adding reference to the Unity project dll created, and the UnityEngine.dll. Compilation is successfull and I get the dll. And here is the Assembly I'm trying to get working:
namespace SCRIPT
{
public class LabelMover
{
InUnity.MyLabel me;
public void Start(ref InUnity.MyLabel myself)
{
me = myself;
}
int xt = 0,maxXT = 100;
int switcher = 1;
public void OnGUI()
{
me.MyLocation.x += switcher;
me.MyLocation.y += switcher;
xt+=switcher;
if(xt == maxXT || xt == 0)
switcher = -switcher;
}
}
}
Now this is working in a regular .net environement. But not in Unity. I believe it is because of cross reference somehow no ?
The error Unity output is : TypeLoadException: Could not load type 'SCRIPT.LabelMover' from assembly 'LabelMover, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) (at /Applications/buildAgent/work/3df08680c6f85295/mcs/class/corlib/System.Reflection/Assembly.cs:398)
If I switch the plugin to a full reflection, this is working :
namespace SCRIPT
{
public class LabelMover
{
object labelObject;
System.Reflection.FieldInfo myLocation;
public void Start(System.Type t, ref object theLabel)
{
labelObject = theLabel;
myLocation = t.GetField("MyLocation");
}
int xt = 0, maxXT = 100;
int switcher = 1;
public void OnGUI()
{
UnityEngine.Vector2 oldLocation = (UnityEngine.Vector2)myLocation.GetValue(labelObject);
oldLocation.x += switcher;
oldLocation.y += switcher;
xt += switcher;
if (xt == maxXT || xt == 0)
switcher = -switcher;
myLocation.SetValue(labelObject, oldLocation);
}
}
}
But that is not user friendly at all : /
I have tried compiling the plugin with Mono Runtime, using the v2.8 and with CodeDom .Net2.0 compiler and both fail in Unity.
I'm really hoping some input from the community, that would be really sweet ~.~ Thanks for any input !
Nobody ? :^( I made a tiny step by adding the project dll into the Plugins folder. However offcourse there are type collision, but project still launch. The new error is when passing the argument. "ArgumentException: failed to convert parameters"
Answer by dCalle · Dec 05, 2017 at 12:48 PM
Feeling you bro, think about using dynamic Assemblies as well. Would be nice to find something that's working ;-)
Your answer
Follow this Question
Related Questions
how to undo redo system implement on material.maintexture at runtime(InGame). 0 Answers
Can I use .NET 4.5 assembly as external library? 1 Answer
Script Execution Order settings with Multiple Assemblies? 0 Answers
[SOLVED]Need help using a C++ bundle plugin in Unity3D using Xcode 2 Answers
NotSupportedException while loading an external assembly in WebGL 0 Answers