Loading and Switching Scenes
In addition, as of v2.4, the Asset Bundle has added a new loading method:
bundle.loadScene('MyScene', function (err, scene) {
director.runScene(scene);
The loadScene
provided by the Asset Bundle will only load scenes from the specified bundle and will not automatically run the scenes, you also need to use to run the scenes.loadScene
also provides more parameters to control the loading process, so developers can control the loading parameters themselves or do some processing after the loading scene.
For more information about loading scenes in the Asset Bundle, you can refer to the Asset Bundle documentation.
The engine will only run one scene at the same time. When switching scenes, all nodes and other instances in the scene will be destroyed by default. Developer’s may need to use a component to control the loading of all scenes, or to transfer parameter data between scenes, mark the node where the component is located as a Persistent Node so that it will not be automatically destroyed when the scene is switched, and will remain in memory. Example:
Cancelling the persistece of a node is easy. Example:
director.removePersistRootNode(myNode);
When loading a scene, you can attach a parameter to specify the callback function after the scene is loaded. Example:
Since the callback function can only be written in this script, the scene loading callback is usually used in conjunction with the persistent node and used in the script mounted on the persistent node.
will automatically switch to run the new scene after loading the scene. It is also possible to silently load the new scene in the background and switch manually after the loading is complete. Use the preloadScene
interface to preload the scene in advance. Example:
director.preloadScene("table", function () {
});
Then call at an appropriate time to switch scenes. Example: