Today I have addressed another issue that the user has reflected. At the beginning of the design phase, we only designed a background for the overall atmosphere of the game, without considering the needs of the user for a variety of backgrounds. In view of this problem, we mainly added two new styles of different backgrounds, so that the hue has changed, The biggest question in the process of joining the background is how to change the background of each scene. Since Unity's loading unit is a scene, changing the background of the current scene does not affect the background of the next scene, and each of our levels is stored for a scene, making it more difficult to change the background. The original situation is that the background in each scene is static and does not change, but the requirement now is that the changes made in the menu will be reflected in each level. Uimanager is a singleton, in the entire game is responsible for storing the current level and other information, in the loading of the various levels he will be kept into the new level, so it is very appropriate to store background information, but only storage is not enough, changing the background is equally important, but the timing of replacement is also a problem. The Unity Loop consists of start and update two functions, start is executed at initialization, and update executes at each frame. It is not necessary to have a waste of resources if you are testing the current background at every frame. We want to detect if the background is the desired background whenever a new scene is loaded, or if it is not replaced. But the question is how to seize the moment. As mentioned earlier, start executes at initialization time, but as mentioned above, the Uimanager is not created repeatedly between scenes, but is kept in a new scene, which means that its start will not be executed. So how do we seize the moment? The method is very simple, although the Uimanager will not be created repeatedly, but our protagonist will be initialized in each scene, which means that its start function is executed every time the new scene is loaded. So we call Uimanager's updated background function in the start function of the protagonist. The code is as follows: void Start () { Other code ... Uimanager = Gameobject.find ("Uimanager"); Uimanager.sendmessage ("syncbkg"); } private void syncbkg () { Gameobject bkg1 = Gameobject.find ("Bkg1"); if (BKG1) { Spriterenderer Spriterenderer = Bkg1. Getcomponent<spriterenderer> (); if (spriterenderer) { Spriterenderer.sprite = bkgs[bkg]; } } } As follows: Added a new menu option Added a button to replace a new background New background can be updated correctly when loading new scenes |