The distribution of our game scenarios is as follows:
Scenario A on the registration/login page, game scenario B, world scenario C, and combat scenario D.
To facilitate interface management (for example, you can open interface 1-> interface 2-> Interface 3, and then jump to the page to jump, at this time, if you inherit a base class uibase from all interfaces and save them in a dictionary, you can use a for loop to display a full screen interface );
The main method is to place all interfaces in Scenario A. The game loads Scenario A first, and then all interfaces (uiroot) all are in the same game object (such as alluigameobject), and then in the Code dontdestroyonload (allgameobject), so that when switching to other scenarios (B, c, d, the interface can continue to exist. If Scenario A is returned again (for example, when the account is switched back to the login interface), the login process must be repeated, A lot of data in the previous account is invalid, So destroy drops the previous alluigameobject object (if some role data is saved as static or monolithic, we still need to dynamically reset it), and then reload Scenario A according to the above steps to go through the same process (here the process is simple, it refers to the launchgame below. the START () function in the CS script ).
Because destroyonload (OBJ) is used to switch back and forth to the scenario where obj is located, multiple parts of OBJ will be cloned. Therefore, some methods are searched online, for example, you can use a flag to determine whether the object has been cloned, or use a singleton to determine whether the object has been initialized. However, the object will be repeated! Therefore, it is changed to the above-mentioned ideas.
Launchgame. CS:
1 using UnityEngine; 2 3 public class LaunchGame : MonoBehaviour 4 { 5 public GameObject mAllUIGameObject = null; 6 7 private static LaunchGame mInstance = null; 8 public static LaunchGame Instance 9 {10 get11 {12 return mInstance;13 }14 private set15 {16 17 }18 }19 void Start ()20 {21 if (mInstance == null)22 {23 Output.Log("LaunchGame.Start(), mInstance == null");24 mInstance = this;25 }26 27 DontDestroyOnLoad(mAllUIGameObject);28 29 UIManager.Instance.Init();30 31 Output.Log("LaunchGame.Start()");32 }33 34 void Update ()35 {36 37 }38 39 public void DestroyAllUIGameObject()40 {41 Destroy(mAllUIGameObject);42 }43 }
Uimanager. CS:
1 using system. collections. generic; 2 3 // Panel Type 4 Public Enum uitype 5 {6 ui1, 7 ui2, 8 ui3, 9 ui4, 10} 11 12 // interface logic manager 13 public class uimanager: singleton <uimanager> 14 {15 public ui1 ui1 = NULL; 16 public ui2 ui2 = NULL; 17 Public ui3 ui3 = NULL; 18 public ui4 ui4 = NULL; 19 20 dictionary <uitype, uibase> muidic = new dictionary <uitype, uibase> (); 21 22 // initialization interface logic 23 public void Init () 24 {25 checkmembe RS (); 26 initallpanel (); 27} 28 29 // check whether the member variable is valid 30 void checkmembers () 31 {32 assert. isnotnull (ui1, "uimanager. checkmembers (), ui1 is null object! "); 33 assert. isnotnull (ui2," uimanager. checkmembers (), ui2 is null object! "); 34 assert. isnotnull (ui3," uimanager. checkmembers (), ui3 is null object! "); 35 assert. isnotnull (ui4," uimanager. checkmembers (), ui4 is null object! "); 36} 37 38 // Initialize all panels 39 void initallpanel () 40 {41 muidic. add (uitype. ui1, ui1); 42 muidic. add (uitype. ui2, ui2); 43 muidic. add (uitype. ui3, ui3); 44 muidic. add (uitype. ui4, ui4); 45 46 foreach (uibase UI in muidic. values) 47 {48 if (UI. m_panel! = NULL) 49 {50 UI. checkwidget (); 51 UI. init (); 52 UI. bindingevent (); 53 UI. reset (); 54} 55} 56} 57 58 // display the specified panel 59 Public void showpanel (uitype type) 60 {61 foreach (keyvaluepair <uitype, uibase> item in muidic) 62 {63 If (type = item. key) 64 {65 item. value. show (); 66} 67 else68 {69 item. value. hide (); 70} 71} 72} 73}
Other scripts (such as ui1 .. ui4, uibase), no more, nothing. In scenea-> sceneb-> scenec, application is used. loadlevel (). Before scene X-> scenea, launchgame is called. the destroyalluigameobject () function in CS before loadlevel (Scenario ).
The above is the general idea and implementation. If there are any bad aspects, please let us know. Thank you!
Manage multiple interfaces in multiple ngui scenarios