Unity3d Project program encryption to use your own DLL in Unity3d

Source: Internet
Author: User
If you have a little attention to this knowledge of friends, you should know that Unity3d made out of the project is very easy to decompile, after the anti-compilation of all your resources and code will be rendered without cover. Since. NET itself has no encryption, we generally use confusing code to encrypt the program in some way. For the next 2 blogs, Azhao will simply talk about my own cryptographic practices in Unity3d. First of all, I'll briefly explain how to use DLL dynamic-link libraries in Unity3d, and then the second article will explain how to confuse DLLs.
Start with the first article: first, we need to create a new class library project that can be done using Visual Studio or MonoDevelop.   I am using vs2012 to create: Choose a good project type, write the project name. The newly generated project has a default Class1 class
You can change the name of the class you want by renaming it in Solution Explorer, like I changed it to Math3d.
Then write a simple static method inside. Here I have written the simplest addition (add). Also can not write static method, write the ordinary public method, use when the new object comes out to call this method also can. This is no different from normal C # programming.
After writing, in the Explorer to select "Generate", then in the project Bin/debug will see the DLL file, such as my this amount is AzhaoDll.dll

Next, create the project you need in Unity3d and put the DLL file in the project's assets folder. About this DLL file storage location, some people on the internet said that it must be placed in the plug-in folder, in fact, is not needed, in any location it can be recognized. Of course, according to good project path habits, we set up a dedicated plug-in folder to store DLL files, it is also reasonable.
Then create a C # script in Unity3d and write a simple line of code to invoke the Math3d.add method we just wrote.
You will find that the Math3d class is not recognized, and we using the Azhaodll namespace

Then in the Unity3d to hang the script on the camera, run, you can see the correct results are printed, which proves that the DLL has been successfully called.
Look back at some of the little details we've just done, inside the class library project's Math3d class. We use the Azhaodll namespace by default. So if we don't use namespaces, or use other namespaces. The answer is yes, the namespace can be arbitrarily changed, if not the namespace, then you do not need the use of the call, if other namespaces, the corresponding using its namespace is OK. The following try to delete the namespace: Then the Unity3d inside the call can be used directly:
The result of the operation is also normal:
Of course, it is not recommended not to use namespaces, this is a description, because the script created by Unity3d itself does not use the namespace, just to illustrate that if you do not want trouble, directly to the Unity3d no namespace script directly to compile is also possible.
Looking again at the details, just now we are using native C #, if we need to write the method of invoking Unity3d's own function in the Class library project.   In Unity3d's installation directory editor\data\managed, find UnityEditor.dll and UnityEngine.dll two files. Then add a reference to the class Library project and add the two DLLs in this case, we can use the Unity3d method in the Class library project using Unityengine, for example, we write a creategameobject method here, generate a name " Createbydll "of the Gameobject.
Build the DLL, put it back in the Unity3d project, and we can call this method:
Run and see this object called "Createbydll" generated. With the above instructions, we will find that it is very simple to build your own DLL in Unity3d. We can also add other class-in-class library projects that we have written, and then build DLLs to use.
Finally, let's talk about the precautions. The DLL that you just generated is basically available in the Unity3d editor, but it doesn't have to be compiled. It is possible to make an error when compiling an EXE or APK. It is important to note that the. Net framework version is used by the class library project. My own attempt was to use a DLL published in the. Net 4.0 version, which would not be compiled in Unity3d. So we can use a version of 2.0 or 3.0 to publish.
There are also cases where some off-the-shelf DLL dynamic libraries are used intact. Net2.0 to publish, so we may need to choose the full. Net 2.0来 release when compiling.
At this point, we can cut all the scripts in Unity3d's entire code folder into the Class library project to generate the DLL and put it back inside the Unity3d project. So that others can not directly edit the code in our project.
But the DLL itself is unsafe, others can easily see inside the content. Next blog We explain how to confuse the DLL.

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.