Use Unity to make the game run normally before packaging, and the solution to the logic error after packaging

1. Situation description

        As a Unity novice, after I learned some basic operations of C# and Unity, I wanted to see what the game looked like after it was packaged, so I went online to find the process package. The reference article is this: (10 messages) [Transfer] unity works packaging_unity packaging project_little girl's big blog-CSDN blog

        The whole process of encountering the problem described in the title is like this:

        At the beginning, I didn't know where the script files should be stored, so at the beginning, all my script files were at the same level as the Assets folder. As a result, an error occurred when packaging, as follows:

        The general meaning of the translation is "when the editor is importing resources or compiling scripts, it cannot be packaged" (this is my own translation, I don't know if it is right), so I searched for the solution to this problem and found The result is that I need to store all my script files in the Editor folder under the Assets folder, so as to solve this error . So I created a new Editor folder under the Assets folder and moved all the script files into it.

        After packaging again, there is no error, and an exe file of the game is also generated. but! ! ! When I found the game exe file and opened it (I tried both normal mode and administrator mode, and the result was the same), none of my game logic was executed. I found it very strange that the logic was very different when running the game in Unity. Normal, why the logic doesn't run after packaging, it's outrageous! ! ! So I went to the Internet to find a solution, and now I will summarize my solution.

Two, the solution

1. Do not use Chinese to name anything

        When I went online to find a solution, the keywords at the beginning were similar to the title of my current article. Most of the results I found were saying not to use Chinese names, such as namespaces in scripts, script file names, etc. Do not even use Chinese on the path of the entire game project.

        My suggestion is not to use Chinese, because sometimes Chinese cannot be recognized correctly when identifying names, so it is better not to use it. However, the problem of my project is not here, because I myself pay great attention to this point, and did not use any Chinese when naming it.

2. Put the script file in the correct folder

        The problem with my project is out of this.

        As I said before, the way I solved my packaging error was to move the script files into the Editor folder. Unfortunately, after packaging, all the contents in the Editor folder, including the Editor folder, are invalid. Those that will act on the game scene can be understood as they do not exist at all. Some people may be about to say, just move all the script files out and put them in other folders. No! not like this. Before proposing a solution, I want to talk about why Unity reported the error I mentioned earlier when it was first packaged.

        After we create a new C# script file in Unity, we will find that the script file has added a namespace called "UnityEditor.VersionControl". This namespace can only be used in the Unity editor, and it cannot be used without the Unity editor. , and the script files that add this namespace need to be placed in the Editor folder, because the Editor folder is the folder used to store the extension scripts of the Unity editor , if not placed in the Editor folder, in the packaged Then the error I mentioned earlier will appear. This is the ultimate reason for the previous error.

        At this time, we understand that if your script file needs to use the "UnityEditor.VersionControl" namespace, it must be placed in the Editor folder, otherwise an error will be reported when packaging ; otherwise, if your script file does not contain If you need to use the "UnityEditor.VersionControl" namespace, you should delete or comment out the reference statement of this namespace, and then move the script file out of the Editor folder and put it in another folder .

        Since my script files do not need to use the "UnityEditor.VersionControl" namespace, I got my ultimate solution, which is to delete or comment out "using UnityEditor.VersionControl;", and then in the Assets folder Create a new Scripts folder (other names are also available, I just use this name because the semantics of Scripts are easier to understand), and then move all the script files in the Editor folder to this Scripts folder.

3. Conclusion

        Up to now, although the problems I encountered when packing have been solved, these methods of mine may only solve the problems of myself and some people. If you still can't solve the problem you have encountered after reading this article, then I suggest you find other people's solutions, or refer to this article (link below, this article helped me a lot this time Big help), in short, don’t give up easily, because there are always more solutions than difficulties hahahaha. Regarding the solution I proposed above, if you have any questions or I have made a mistake, please post your comment in the comment area and let me know, thank you in advance~

(11 messages) Unity common folder_unity folder_Prudence's Blog-CSDN Blog

        The reference articles in this article all provide links in the text, and no more links will be added here.

Guess you like

Origin blog.csdn.net/hotdogbs/article/details/129333755