Enjoy the feeling of a great god. in unity, use C # To encapsulate dll by yourself,

Source: Internet
Author: User

Enjoy the feeling of a great god. in unity, use C # To encapsulate dll by yourself,

What's wrong with my first blog?

(1) Open vs (I use vs2013) and create a project, as shown in, and select. ENT3.5 framework, or lower than 3.5. This is because the framework used in Unity may be less than 3.5. Otherwise, an error will be reported, and you can change the name of your dll to a path (this path should be used later)

(2) after entering the project, if you do not need to use classes or encapsulation methods in unity, you can directly write the method you want to encapsulate as follows:

 

1 namespace MyDll // namespace (there is no namespace in unity) when used in unity, add using MyDll 2 {3 public static class Compute // class name 4 {5 public static int GetMax (params int [] arr) // static class to the script, of course, you can write a non-static one here, but in Unity, You need to instantiate 6 {7 for (int I = 0; I <arr. length; I ++) // here is a bubble sort 8 {9 for (int j = 0; j <arr. length-I-1; j ++) 10 {11 if (arr [j]> arr [j + 1]) 12 {13 int temp = arr [j]; 14 arr [j] = arr [j + 1]; 15 arr [j + 1] = temp; 16} 17} 18} 19 return arr [arr. length-1]; 20} 21} 22}

Of course, we mainly talk about unity, but the classes in vs cannot inherit MonoBehaviour, so we need to import the unity dll to use the encapsulated method in unity.

Import Procedure

 

Select Browse on the left-click browse next, select the path of the unity package dll you want to import, and click OK to import it successfully. Here I import UnityEngine. dll

(For unity-encapsulated dll, you can create a project in unity, create a script, and open a reference on the Right of vs. After opening it, select with the mouse, and you can see the path below)

Then, add a class (right-click MyDll-add-class) Like above, for example

 

1 using UnityEngine; // import the namespace (if you need to use the UI, you only need to import it again) UnityEngine. the UI is ready, the methods are the same. 2 // <summary> 3 // encapsulate a Camera control script 4 /// </summary> 5 6 [AddComponentMenu ("Camera/Controller")] // This is to add the most component of this class to the AddComponent menu bar under the unity attribute panel. The string indicates the path 7 public class CameraController: monoBehaviour (this class can also be written under the name you want, you need to import the namespace when using IT) 8 {9 public Transform target; // The offset of the Target 10 private Vector3 following the camera; // the offset of the target and the camera 11 12 13 Void Awake () 14 {15 offset = transform. position-target. position; // calculates the offset of 16 transform. lookAt (target. position); // initial viewing target point 17} 18 19 20 private void CameraFollow () 21 {22 transform. position = target. position + offset; // keep the offset of the camera and the target point unchanged, and then follow 23} 24 25 26 void Update () 27 {28 CameraFollow (); 29 CameraScroll (); 30} 31 32 33 private void CameraScroll () 34 {35 if (Input. getAxis ("Mouse ScrollWheel ")! = 0) 36 {37 Vector3 temp = offset. normalized * Input. getAxis ("Mouse ScrollWheel"); // find the direction and distance of the slide 38 offset = offset + temp; // The offset 39 40 41} 42} 43}

(3) We have already written two classes. Now we need to generate the dll. On the top menu bar of VS, there is a "generate"-"generate solution", or directly F6.

(4) go to our project folder and find the dll file (I will not elaborate on it here)

(5) import it to our unity Project and put it in the Project (try to create a folder Plugins and put it in it to prevent errors)

(6) Now I can use it. The following is an example.

6.1 let's first talk about the usage of unity encapsulation.

6.1.1 if you add [AddComponentMenu ("Camera/Controller")] to the class just now, you can find the Camera-Controller on the attribute panel AddComponent, which is your encapsulated class. You can directly select this class.

Because MonoBehaviour has been inherited during encapsulation, it can be inherited (if this code is not added, it can also be loaded)

6.2 let's talk about the encapsulated algorithm.

6.2.1 If you have just added a namespace, you need to use the using space name for the method in use, and then use the same method as in time, as shown below:

1 using UnityEngine; 2 using System. collections; 3 using MyDll; // import namespace 4 5 public class TTTT: MonoBehaviour 6 {7 void Awake () 8 {9 Debug. log (Compute. getMax (3,10, 7,5, 4); // The encapsulated Method 10} 11}

(7) At the last point, if the framework is not tuned, the following error will occur (I am confused for a long time, because I have not mentioned how to solve this error in other posts, later. after the NET Framework is lowered)

 

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.