“This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

๐Ÿ‘‰ About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

๐Ÿ‘‰ premise

[Pick up the launch page and cute animation content from the previous article]

๐Ÿ‘‰ Practice

๐Ÿ˜œ Core script

Then we create two scripts (ScriptStart and ScriptMaxScore)

The ScriptStart script contains two methods, loading the next scene and ending the game, called by the above two buttons, and then reads the score information in the onLoad method. If there is one, it reads the corresponding score and sets it to 0 if there is none (in case ScriptMaxScore script reads the value empty).

protected onLoad() {
    console.log("ScriptStart็š„onLoad");
    // Data encryption can only be considered with the best server
    if (localStorage.getItem(ScriptStatic.MaxScore) == null) {
        console.log("There is no top score.")
        // Why do I set it here when I don't have it
        localStorage.setItem(ScriptStatic.MaxScore, "0");
    } else {
        console.log("Got the highest score.".localStorage.getItem(ScriptStatic.MaxScore))
    }
}
ย 
// Start the game - load the next scene
GameStart() {
    director.loadScene("scene_game");
}

// End the game
GameExit() {
    game.end;
}
Copy the code

Note: The script life cycle is similar to Unity, with onLoad for all scripts followed by onEnable for all scripts followed by start for all scripts and so on. The order in which their same lifecycle functions are executed is affected by the order in the hierarchy manager.

The score is displayed using artsy. There are two ways to set artsy:

One is to directly generate the corresponding bitmap font file

One is for the Sprite to create multiple images for each setting for a single prefab code

We use the first method.

Create bitmap text font (FNT and PNG files) – don’t know how to create it can private message me.

Then I’m going to assign the Font to the Label component’s Font property. Remember to change CacheMode, and check to see if the Layer is correct.

Then we add script ScriptMaxScore to the score node

start() {
    console.log("ScriptMaxScore็š„start");
    // Read the saved score data, which is set either in the onLoad of Start or at the end of the game
    let score = localStorage.getItem(ScriptStatic.MaxScore)
    // Set the score art word
    let LabelScore = this.node.getComponent(Label);
    if(LabelScore ! =null) {
        LabelScore.string = score + "".toString();
    } else {
        console.log("Get text node empty")}}Copy the code

๐Ÿ‘‰ other

๐Ÿ“ข author: Kom and Kom in Kom

๐Ÿ“ข reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

๐Ÿ“ข welcome to like ๐Ÿ‘ collect ๐ŸŒŸ message ๐Ÿ“