- Home /
how I can make some code outside unity ?
hi,
I have question I hope find answer here.
Did i Can make some code outside unity (say DLL extension or C# file )? i want to modify the external file's without build EXE file.
Not quite sure what you're getting at there. Are you asking if you can make plug-ins? .DLL extensions are supported, but you more than likely need a pro-license (which the majority of people on here don't have).
http://unity3d.com/support/documentation/$$anonymous$$anual/Plugins.html
This is the correct answer. :) P/Invoke (calling native code in a DLL file) is what Unity calls a "plugin", and requires a Pro license.
I have Pro version but if i moved my own dll's to (..\Assets\Plugins) folder,the compiler always give me this error:
Internal compiler error. See the console log for more information. output was: Unhandled Exception: System.TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly.
Answer by Statement · Dec 05, 2011 at 09:58 AM
You can load managed libraries dynamically. I suggest you define a shared interface class that Unity know about and that your library know about to make it easier to bind. I will give an example of creating and loading a naive math library.
First, we'll make a new class library in Visual Studio. I call this IMathBox, and will contain my interface. The interface allow us to easily separate the actual implementation, while still maintaining some sort of type safety and you don't have to write a lot of binding code.
namespace IMathBox
{
public interface IMath
{
int Calc(int a, int b);
}
}
Compile that into IMathBox.dll and place a copy of that into your unity assets folder so Unity will know about it.
Next, we'll write two classes that implement IMath. These classes will not part of Unity and you may replace it anytime you want without rebuilding the executable. Make a new class library and call it MathBox. Obviously you'll need to add IMathBox to your assembly references since we will be implementing IMath. I created two classes, one for multiplication and one for division. Compile this into MathBox.dll and place it in the root folder of your Unity project while developing (Into the folder that hold Assets folder - not inside Assets folder). Make sure to include it as a copy in the root folder when you build a release.
namespace MathBox
{
public class MultiplyMath : IMathBox.IMath
{
public int Calc(int a, int b)
{
return a * b;
}
}
public class DivisionMath : IMathBox.IMath
{
public int Calc(int a, int b)
{
return a / b;
}
}
}
Now we need to actually load the dll, and create objects of those classes.
using UnityEngine;
using System.Reflection;
public class AssemblyLoadDemo : MonoBehaviour
{
string resultMessage;
void Awake()
{
Assembly dll = Assembly.LoadFile("MathBox.dll");
System.Type multiplyType = dll.GetType("MathBox.MultiplyMath");
System.Type divisionType = dll.GetType("MathBox.DivisionMath");
IMathBox.IMath multiply = (IMathBox.IMath)Activator.CreateInstance(multiplyType);
IMathBox.IMath division = (IMathBox.IMath)Activator.CreateInstance(divisionType);
resultMessage = string.Format("Multiply: {0}, Division: {1}", multiply.Calc(10, 5), division.Calc(10, 5));
}
void OnGUI()
{
GUILayout.Label(resultMessage);
}
}
We start by loading MathBox.dll, then extracting the types "MathBox.MultiplyMath" and "MathBox.DivisionMath". Then we use the Activator to instantiate objects of those types. Finally for this little demonstration, I call "Calc" on both these objects and make sure I get the expected results. In this case 10*5 should be 50 and 10/5 should be 2, and when I run this on my computer it behaves as expected.
You can also compile code on the fly, if that is more what you are looking for, since you mentioned C# files. I won't get into that since this already became quite lengty, but you can start looking at the CSharpCodeProvider class.
So yeah, this became 2 dlls. One dll for the interface that is shared between Unity and MathBox, called IMathBox. Then of course there is MathBox that implements IMathBox and Unity uses IMathBox types compile time and MathBox types runtime.
Thank you for your answer , I wrote two dll fiels ,the first one for interface (I$$anonymous$$athBox.dll) and second for my own class ($$anonymous$$athBox.dll), after that I copied those files to root folder .
In Unity I wrote this code:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class AssemblyLoadDemo : $$anonymous$$onoBehaviour {
string result$$anonymous$$essage;
// Use this for initialization
void Awake () {
Assembly dll = Assembly.LoadFile("$$anonymous$$athBox.dll");
System.Type multiplyType = dll.GetType("$$anonymous$$athBox.$$anonymous$$ultiply$$anonymous$$ath");
System.Type divisionType = dll.GetType("$$anonymous$$athBox.Division$$anonymous$$ath");
I$$anonymous$$athBox.I$$anonymous$$ath multiply = (I$$anonymous$$athBox.I$$anonymous$$ath)Activator.CreateInstance(multiplyType);
I$$anonymous$$athBox.I$$anonymous$$ath division = (I$$anonymous$$athBox.I$$anonymous$$ath)Activator.CreateInstance(divisionType);
result$$anonymous$$essage = string.Format("$$anonymous$$ultiply: {0}, Division: {1}", multiply.Calc(10, 5), division.Calc(10, 5));
}
}
but the compiler give me some errors in the flowing lines :
I$$anonymous$$athBox.I$$anonymous$$ath multiply = (I$$anonymous$$athBox.I$$anonymous$$ath)Activator.CreateInstance(multiplyType);
I$$anonymous$$athBox.I$$anonymous$$ath division = (I$$anonymous$$athBox.I$$anonymous$$ath)Activator.CreateInstance(divisionType);
the error description is :
Assets/AssemblyLoadDemo .cs(13,9): error CS0246: The type or namespace name `I$$anonymous$$athBox' could not be found. Are you missing a using directive or an assembly reference?
what i should to do now?
You need to copy I$$anonymous$$athBox.dll into your Assets folder in Unity.
$$anonymous$$any thanks for this great example! Does this work platform independently (especially Android/iOS)? Is UnityPro required?
Your answer
Follow this Question
Related Questions
Is it possible to assign a script that derives from MonoBehaviour from an external library? 1 Answer
Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers
First pass.dll error that I get whenever I open unity or press play 1 Answer
Can't build error - dll is not allowed to be included or could not be found. 3 Answers