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 Almikhlafi · Dec 05, 2011 at 01:05 AM · dll

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.

Comment
Add comment · Show 3
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 dannyskim · Dec 05, 2011 at 07:11 AM 0
Share

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

avatar image CHPedersen · Dec 05, 2011 at 09:21 AM 0
Share

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.

avatar image Almikhlafi · Dec 06, 2011 at 10:35 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · 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
avatar image Almikhlafi · Dec 06, 2011 at 09:53 AM 0
Share

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?

avatar image Statement · Dec 06, 2011 at 06:21 PM 0
Share

You need to copy I$$anonymous$$athBox.dll into your Assets folder in Unity.

avatar image Gatsu · Feb 15, 2012 at 02:16 AM 0
Share

$$anonymous$$any thanks for this great example! Does this work platform independently (especially Android/iOS)? Is UnityPro required?

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

6 People are following this question.

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

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

C# and Mysql problem in Windows standalone 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