Use DLL in Unity3D and DLL in Unity3D

Source: Internet
Author: User

Use DLL in Unity3D and DLL in Unity3D
First, we need to create a new class library project, which can be done using Visual Studio or Monodevelop. Here I use vs2012 to create a project: select the project type and write the project name. The new project contains a Class1 class by default.
You can rename the class name you want in Solution Explorer. For example, I changed it to Math3D.
Then write a simple static method in it. Here I wrote the simplest addition (Add ). You can also write a common public method without writing a static method. You can call this method when the new object is used. This is no different from normal C # programming.
After writing the script, select "generate" in the Resource Manager. The dll file will be displayed in the bin/debug file of the project. For example, my project is AzhaoDll. dll.
 
Next, create the project you need in Unity3D and place the dll file in the Assets folder of the project. Some people on the Internet say that this dll file must be stored in the plug-in folder. In fact, it is not needed and can be identified at any location. Of course, it is reasonable to create a folder dedicated to plug-ins to store dll files according to good project path habits.
Then create a C # script in Unity3D and write a simple line of code to call the Math3D. Add method we just wrote.
In this case, we will find that the Math3D class is not recognizable. We use the using AzhaoDll namespace.

Then, mount the script to the camera in Unity3D and run it. The correct result is displayed, which proves that the dll has been successfully called.
Review some of the small details we have just done in the Math3D class of the class library project. The AzhaoDll namespace is used by default. So if we do not use a namespace, or use other namespace rows? The answer is yes. The namespace can be changed at will. If you do not need a namespace, you do not need to use using when calling it. If you use another namespace, the corresponding using namespace is enough. Delete the namespace as follows: You can use it directly in Unity3D:
The running result is also normal:
Of course, it is not recommended that you do not use namespaces. This is because the scripts created by unity3D do not use namespaces by default, you can compile Unity3D scripts without namespaces directly.
Review the details again. We used native C # Just now. What should we do if we need to write a method to call the Unity3D function in the class library project? In the Unity3D installation directory Editor \ Data \ Managed, find the UnityEditor. dll and UnityEngine. dll files. Then add references to the class library project and add the two dll. At this time, we can use the Unity3D method by using the using UnityEngine in the class library project, for example, we have simply written a CreateGameObject method to generate a gameobject named "CreateByDll.
Generate the dll and put it back in the Unity3D project. We can call this method:
Run the command and you will see that this object called "CreateByDll" has been generated. Through the above instructions, we will find that it is very easy to generate your own dll in Unity3D. We can also add other classes written by ourselves to the class library project, and then generate the dll for use.
Finally, let's talk about the precautions. The generated dll is basically usable in the Unity3D editor, but may not be compiled. It is possible that an error will be reported during compilation into exe or apk. Note that the. Net Framework Version Used by the class library project. My own attempt is to use the dll released by. Net 4.0 or later, which will not be compiled in Unity3D. Therefore, we can use version 2.0 or 3.0 to release it.
Some other situations are that some ready-made dll dynamic libraries are released using the complete. Net2.0, so we may need to select the complete. Net 2.0 for release during compilation.
In this step, we can cut all the scripts in the Unity3D code folder into the class library project to generate the dll, and then put it back in the Unity3D project. In this way, no one else can directly edit the code in our project. However, the dll itself is not safe, and others can easily see the content in it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.