Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nanorock · Feb 08, 2013 at 03:07 PM · plugindynamicassembly

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 !

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanorock · Feb 09, 2013 at 09:37 PM 0
Share

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"

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 ;-)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges