Unity uses NuGet to obtain the required dll - Example: Android + NPOI plug-in

 

Table of contents

Preface

1. Introduction to development environment

1. Unity version

2. Publishing platform

3. This time’s goal

2. The failure process surrounding NPOI (pure complaint, can be skipped)

3. Record the correct steps

1. Prepare the environment

        ①.Create/open c# class library

        ②. VS opens the NuGet package manager

2. Organize packages and dependencies

        ①. Nuget search plug-in

        ②. View dependencies

        ③. Organize dependencies

3. Download and import Unity

        ①. Download all packages

        ②. Extract package

        ③. Import into Unity

 4. Summary

5. Summary of issues

6. Download link


Preface

        I always encountered a problem before: I found an open source library but it was very troublesome to use it. Not only does it take a lot of time to find dlls compiled by others online, but it may not be usable. Even if it can be used, it will not work if you change the environment (Unity often needs to be released on multiple platforms). Compiling it yourself is difficult. Creating a blank project and writing the code from scratch can compile successfully, but generating a DLL from an existing project always fails. . .

        This time the accidental collision finally worked out, and Nuget, this treasure, could finally be carried home! ! It feels like the village finally has a highway...

1. Introduction to development environment

1. Unity version

        The currently used unity version is 2022.1.8f1

        Guess: You can use .Net4.x or .NetStandard2.0 and higher versions above.

        As for the lower version that can only use .net2.0, it may be a problem. It seems that Nuget started with .net4.

2. Publishing platform

        The current platform is Android and the selected one is .NetStandard2.1

        For other platforms, see Unity release settings Configuration -> Api Compatibility Level

        The determination of dependencies in Nuget is consistent with the Unity settings (there are screenshots later, and they are all listed in Nuget)

3. This time’s goal

        Function: The mobile phone uses the NPOI plug-in to dynamically generate word (the function is not important, the main thing is to extract the dll)

        Core: In fact, it is how to obtain the required dll. Ahem, I want something that works! !

2. The failure process surrounding NPOI ( pure complaint, can be skipped )

a. GitHub found the NPOI plug-in and found that the source code did not have dll and .NET Framework 7.2 was missing. It could not be opened and gave up.

b. The first time I find a DLL compiled by someone else, I import it and report an error directly.

c. Extract the compiled NPOI from Nuget, but an import error is reported and ICSharpCode.SharpZipLib is missing. I also searched for this library online, but the version after import was wrong. I found the original project on Github, but it still needed to be compiled. I thought that NPOI had not been completed yet, so I decided to give up.

d. For the Nth time, I found someone else’s DLL number and it works! ! !

    ① The compiler works and no errors are reported during release. I am really happy. It seems that this function will be developed soon;

    ② A problem occurs: the word generated by the compiler can be opened normally and has the correct format, but after the apk is released, it is all garbled. . . The first reaction in my heart was that the cheating WPS mistakenly downloaded Office on my mobile phone (the computer is Office 2019 and the mobile phone is WPS. This reaction is quite normal, not contemptuous). . . . However, I was too naive and did not realize the seriousness of the problem. . . When the word generated by the compiler could be opened on the mobile phone, but the word generated by the mobile phone could not be opened on the computer, I realized that it was not the problem of office and wps. Although it felt a little wrong, I didn't take it seriously, thinking that the mobile phone might need to set something separately. . . When the font, encoding, style, etc. are changed but the problem remains the same, and when the code only generates one line of English and is still garbled when opened, I find that it is really cheating. . . Next, check the Unity engine settings and project settings, but the problem remains. . . There is no other way, so try another method: create a new template, open it with code and replace keywords, so you don’t need to worry about formatting issues. . . I was really confused when I found that the keyword replacement expectation did not match the actual situation: the replacement was successful, but it was not successful. The success is because the keyword is indeed replaced, but the failure is because the replaced word is wrong: the code replaces "A" in "ACCC" with "B", and the output result should be "BCCC", but it actually outputs "DCCC". . . . After adding breakpoints, I found that the function of the plug-in function is wrong: tempText = tempText.Replace("A", "B");. . . . Shocking: The function function can be concatenated with the function name! Shocked: Unity didn’t report an error! ! Shocked: Wearing the skin of a function and doing our own thing! ! !

    ③ Guess: After being silent for a long time, I feel that the dll version is inconsistent with Unity. The dll is compiled with .NETFramework, and Unity Android uses .NETStandard2.1.

e. Unity can replace .NETStandard2.1 with .NETFramework. It seems that something needs to be changed. I didn't do this because it would affect all other modules.

f. Cannot find ready-made .NETStandard2.1, try to compile it yourself.

    ① .NET Framework 7.2~8 has been installed. It will be used sooner or later~

    ② The project can be opened, but it will not compile. . . According to memory and online tutorials, they all failed.

    ③ Suddenly I thought if the library missing in step c was also extracted from Nuget, would it be successful? The result is an unexpected failure:

Severity Code Description Project File Line Suppressed Status Error Unable to install package 'SharpZipLib 1.4.2'. You are trying to install this package into a project targeting 'native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with the framework. Please contact the package author for details. The solution to this problem is found in the problem summary below.

    ④ I have searched the Unity Forum, Baidu, and Google, but still found that compilation failed and Nuget extraction failed.

    ⑤ Suddenly realized: I actually used c++ to compile c#. . . (Actually, I suspected it before, but I couldn’t find the c# template and thought it was universal)

    ⑥ After installing the c# development environment, find the c# template, and finally successfully use Nuget to extract the class library. . . But I still get an error when I use it.

    ⑦ After sinking in pain for a long time, I calmed down, opened Nuget and studied it carefully (before, I always followed the tutorials and listened to the instructions without thinking). Suddenly, an idea flashed: I will compare the dependency table, and I will extract whatever he wants from Nuget. What? Just extract all the dependencies. (I feel like there is drama)

    ⑧ All 13 class libraries were extracted (I built an Excel one, and the more I sorted it out, the less I could use it as a living horse doctor).

    ⑨ Import unity one by one and find that it works. Do you think this will make me happy? Am I that naive? ? Can success be considered a success even if it has not been tested in actual combat? ? ? Sure enough, publishing the Apk failed:

ArgumentException: The Assembly System.Security.Cryptography.Xml is referenced by NPOI.OOXML ('Assets/Office/Plugins/NPOI.OOXML.dll'). But the dll is not allowed to be included or could not be found. This problem See the problem summary below for solutions.

    ⑩ System.Security.Cryptography.Xml is not in the dependency list. Is it a problem with the NPOI XML file? Delete the XML of all DLLs, and sure enough the problem remains. After all, XML is just a comment.

    ⑪ It is suspected that .NETStandard2.1 is not compatible with .NETStandard2.0, because I use 2.1 for NPOI and SharpZipLib, and others use 2.0. I found that the problem was not only not solved, but also new errors appeared, so I changed back decisively.

    ⑫ After many attempts to no avail, I suddenly had a whim, but I just went to nuget to extract this thing. No matter whether it is in the dependency list or not, what if the author missed it? And then there it is. . . ! ! ! ~\(≧▽≦)/~

3. Record the correct steps

1. Prepare the environment

Purpose: This step is to use VS to extract the package in Nuget

Prefix:

        a. I am using VS2019, other versions should be fine as long as they match Unity.

        b. VS is equipped with a c# development environment, as shown in the figure below:

step:

        ①.Create/open c# class library

                Create a c# class library and prepare to extract the package (you can also use it if you already have a project, this is just a tool class)

Note: When creating a VS project, select the C# template. Don’t think like I did before when I couldn’t find a C# template and had to use a DLL or C++ to make do.

        ②. VS opens the NuGet package manager

2. Organize packages and dependencies

Purpose: Find all packages that need to be downloaded and prepare for downloading

Note: Just sort it out without downloading it to prevent downloading the wrong version or confusing the version.

step:

        ①. Nuget search plug-in

                Picture description: Browse in the upper left corner -> Search for package name -> Select package -> View version and dependencies in the middle -> On the right is the downloaded package (ignore it first)

                If the window cannot be found, reset the layout directly: Toolbar "Window" -> Reset window layout

        ②. View dependencies

                In the picture, I use .NetStandard2.1, choose according to your own situation. ( Mentioned in the 2. Release Platform ” section above)

                Different versions have different dependencies (higher versions have more dependencies, so you can downgrade the version appropriately)

        ③. Organize dependencies

                The picture below shows all the dependent packages of NPOI that I used EXCEL to organize. I forgot to label some versions, which are included in the VS screenshot.

                According to my observation, version 4.5.0 is more suitable. The actual situation shall prevail. O(∩_∩)O

Note: In this step, first make sure that all dependencies can be found from Nuget and that all dependent package versions are consistent.

                (Although the probability of missing is small, but what if...)

3. Download and import Unity

        ①. Download all packages

                Tip: Download from back to front in the Excel organized above. There is a version introduction of the installed package in the upper right corner of VS. For dependent packages, you can check against the installed package before downloading.

 

        ②. Extract package

                In the VS installed package, right-click -> open the folder in File Explorer

                The dll is under the lib folder, just select the version you need.

          

        ③. Import into Unity

                Just drag and drop it to Unity's Plugins folder. It is recommended to import it together after downloading.

                Multi-platform needs to create sub-platform folders, such as Plugins -> Android -> xxx.dll

 4. Summary

        1. Although this method is feasible, it will make the project larger because it imports a lot of dependencies. The APK released by my project is 3M larger than before... Okay, I can accept it~~

        2. It is relatively cumbersome, with a large number of dependencies comparing versions one by one. . .

        3. NuGet does not seem to have .Net2.0 packages, so this method is not suitable for older versions of unity.

        4. NuGet has .Net4.x and .NetStandard2.0 and higher versions of libraries. Higher versions of unity should be able to use it.

        5. Some people said before that NuGet can be used, while others said that it cannot be used. The reason is that some libraries have no dependencies, such as Newtonsoft.Json

        6. It’s actually very simple, but I’m a little hesitant because I haven’t walked this road before.

5. Summary of issues

Problem 1: NuGet installation package failed

        Error: Severity Code Description Project File Line Suppressed Status Error Unable to install package 'SharpZipLib 1.4.2'. You are trying to install this package into a project targeting 'native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with the framework. Please contact the package author for details.

        Reason: The project is c++ and the installation package is c#, which is not the same development language.

        Solution: Create a project in the language of the installation package, here we use c# to create the project.

Problem 2: Failed to publish APK

        错误:ArgumentException: The Assembly System.Security.Cryptography.Xml is referenced by NPOI.OOXML ('Assets/Office/Plugins/NPOI.OOXML.dll'). But the dll is not allowed to be included or could not be found.

        Reason: Lack of dependencies, maybe the person who organized the dependency table missed it

        Solution: Go to Nuget to find this dependent package (remember to recurse the dependencies of this package, O(∩_∩)O: the dependent dependencies are also my dependencies)

Problem 3: Wrong dependencies or inconsistent versions

        Error: An error occurred after switching the plug-in .NetStandard version. For example, a new error occurred after my previous test was downgraded from 2.1 to 2.0.

        Reason: Different versions have different dependencies

        Solution: Go to Nuget to check the dependencies of the replaced version and add them one by one.

6. Download link

NPOI plug-in in NetStandard2_1 version of the library.zip
extraction code: CLPS

Guess you like

Origin blog.csdn.net/qq_26686865/article/details/129302995