Troubleshooting of communication test on the Unity client (2 ).
Handling of communication test problems on the Unity client (2)
In the communication test process of the client, the problem of scenario loading brings a lot of trouble to you. Because the message parsing method is called in a separate listening thread, it means that the Unity API cannot be called in the message parsing method. It was originally intended to call the collaborative program StartCoroutine in the parsing method after receiving the scenario switching message to implement asynchronous loading of the scenario. However, once called, the following error will be prompted:
StartCoroutine_Auto can only be called from the main thread...
It cannot be called directly in the listening thread, so you have to find another method. How can I trigger a collaborative program asynchronously loaded in a scenario? So I thought of the Start method, because it is a good place to call the coprocessor. So how can we call the Start method? Yes, it is implemented through Object Instantiation.
When an object is instantiated, Awake and Start methods in its script components are successively executed. Therefore, I trigger the Start method by instantiating the game object, and then call the coprocessor in the Start method to load the scenario asynchronously. The Object Instantiation process is controlled by a Boolean variable. When the parsing method receives the scenario switching message, the Boolean variable corresponding to the target scenario is set to true, in this way, the scenario loading object is instantiated in the Update method. The Code is as follows:
Void Update ()
{
If (_ isLoginLevel ){
_ IsLoginLevel = false;
InstantiateLoadLevelObject (LOGIN_LEVEL_NAME );
} Else if (_ isFirstLevel ){
_ IsFirstLevel = false;
InstantiateLoadLevelObject (FIRST_LEVEL_NAME );
} Else if (_ isSecondLevel ){
_ IsSecondLevel = false;
InstantiateLoadLevelObject (SECOND_LEVEL_NAME );
}
}
To implement this process, we cannot do without the preparation of Prefab. We need to create a Prefab for different scenarios in the project and use it as an object for asynchronous loading. The Prefab is as follows:
Then, you need to write scripts bound to each Prefab. The function implemented by the script is to load different scenarios based on different objects. The Code is as follows:
Void Start ()
{
If (LOGIN_OBJECT_NAME = gameObject. name ){
SceneMng. GetInstance (). _ isInLoad = true;
StartCoroutine (SceneMng. GetInstance (). LoadLevelAsync (SceneMng. LOGIN_LEVEL_NAME ));
} Else if (FIRST_OBJECT_NAME = gameObject. name ){
SceneMng. GetInstance (). _ isInLoad = true;
StartCoroutine (SceneMng. GetInstance (). LoadLevelAsync (SceneMng. FIRST_LEVEL_NAME ));
} Else if (SECOND_OBJECT_NAME = gameObject. name ){
SceneMng. GetInstance (). _ isInLoad = true;
StartCoroutine (SceneMng. GetInstance (). LoadLevelAsync (SceneMng. SECOND_LEVEL_NAME ));
}
}
You can directly call the asynchronous loading method of the scenario written by the scenario management class in the collaborative program to complete the asynchronous loading of the scenario. The code for the asynchronous loading scenario is as follows:
Public IEnumerator LoadLevelAsync (string levelName)
{
If (null = levelName ){
Yield break;
}
_ Async = Application. LoadLevelAsync (levelName );
Yield return _ async;
}
Through the above processing, when we receive the scenario switching message in the message parsing method in the listening thread, the game object corresponding to the target scenario will be immediately instantiated, then, it completes asynchronous loading of the scenario by calling the Coordinator program in the Start method.
I am still pondering that the Update method may not be the best way to deal with the instantiation process of scenario loaded objects. If you have any mistakes or good ideas, please contact me.
This article from du Xiaomeng's blog, without special indication are my original, please do not for any commercial purposes, reproduced please maintain integrity and indicate the source: http://blog.csdn.net/haohan_meng