Unity's C# script compiles dll

foreword

In the past, the access rights of the project were that client developers could view all the contents of the front end, including C# scripts, lua codes, etc.
Now that the project team has adjusted, the C# code permission is only visible to the main program, and it is replaced by a file compiled into a dll type.
Now I'm finally free, let's study how C# scripts are compiled into dll files and call classes, methods, etc. in C#.

Prepare

First create a DllTest scene.
insert image description here
Create a C# script that will be compiled into a dll, name it TestLog, and add a method to print out to determine whether the method is called successfully. If it succeeds, it will output "Calling TestLog successfully" on the console.
insert image description here
Create a Text script to test whether the call to the dll is successful.
insert image description hereMount the Text script for the camera
insert image description here

compile dll

The preparatory work is completed, and the next step is to compile the dll file.
insert image description here
insert image description here
Click to generate an .asmdef file, change it to the name you want, here it is changed to TestLog. At this time, the dll file will be generated in the following directory.
insert image description here
Cut this file under Plugins.
insert image description here
Delete the C# script compiled into dll and prepare for testing.

test

Run the project and find that: the Main Camera object automatically mounts the TestLog script; the ShirlLog of the TestLog script is successfully called, and the output is "successful in calling TestLog".
insert image description here
That's because in the Test script, I provide two tests:
the first one is to test the C# script in the mounted dll to the game object:

private TestLog t;
t = GameObject.Find("Main Camera").GetComponent<TestLog>();
if(t == null)
{
    
    
    t = GameObject.Find("Main Camera").AddComponent<TestLog>();
}

The second test is to test whether the call to the method in the class compiled into dll is successful

private TestLog t;
TestLog l = new TestLog();
l.ShirlLog();

Guess you like

Origin blog.csdn.net/shirln/article/details/120036921