This series of articles will introduce to you the common design patterns in the game
The article is not purely about a lot of theoretical abstract content, the focus is no longer here.
In fact, design patterns if you write a lot of code to read, naturally, it is interesting to use.
But there is no such a system of consciousness, so this series of articles through a Unity3d game example to let everyone feel what design patterns, design mode role.
This article describes the singleton pattern and the state pattern written in front of you.
The various combat states in the game character (Idle,attack,run ... ), scene state (start scene, battle scene, main city scene ...) ) can use design patterns to manage the organization
This article provides an example of using state mode to create multiple game scenes in a management game
The main purpose of this article is to introduce design patterns, so some unity based on the basic content I do not introduce to you, we can refer to the source of learning click Open Link case Introduction
In the game start scene there are two buttons, click a button to enter the battle scene, click another button to enter the main city scene
The case is simple, but we have written the code to meet our goal in this article
Create a scene state base class
Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;
public class Iscenestate
{
protected string Mscenename {get; set;}
Public iscenestate (String scennname)
{
this.mscenename = scennname;
}
public virtual void Startscene () {} public
virtual void Upadtescene () {} public
virtual void EndScene () {}
}
The next step is to use an object to manage each scene
Using singleton mode, it can be understood as an instance of a manager, by which the manager switches our target scene
Designed to function as a singleton mode to facilitate our unified management, enhance code flexibility call simple, avoid unnecessary memory consumption, etc.
Using System;
Using System.Collections.Generic;
Using System.Text;
Class Scenestatemanager
{
//singleton mode
private static Scenestatemanager _instance;
public static Scenestatemanager Getinstacne
{
get
{
if (_instance = = null)
{
_instance = New Scenestatemanager ();
}
return _instance;
}
}
Private Iscenestate mstate {get; set;}
public void Setscenestate (iscenestate state)
{
//to the previous scene status exit processing
if (mstate! = null)
{
Mstate.endscene ();
}
Mstate = State;
Start processing of the new scene state
if (mstate! = null)
{
mstate.startscene ();}
}
public void Updatescenestate ()
{
if (mstate! = null)
{
mstate.upadtescene ()
;
}}}
The next step is to create each scene class, and the scene class is based on the Iscenestate class
Start Scene class Startscenestate
Using System;
Using System.Collections.Generic;
Using System.Text;
Using Unityengine;
Using Unityengine.ui;
Class Startscenestate:iscenestate
{public
startscenestate (): Base ("Startscene") {} public
override void Startscene ()
{
Gameobject.findgameobjectwithtag ("Battle"). Getcomponent<button> (). Onclick.addlistener (onbtnbattle);
Gameobject.findgameobjectwithtag ("MainMenu"). Getcomponent<button> (). Onclick.addlistener (Onbtnmainmenu);
}
private void Onbtnbattle ()
{
SceneStateManager.GetInstacne.SetSceneState (new Battlescene ());
}
private void Onbtnmainmenu ()
{
SceneStateManager.GetInstacne.SetSceneState (new Mainmenuscenestate ());
}
}
Battle Scene Class Battlescene
Using System;
Using System.Collections.Generic;
Using System.Text;
Using Unityengine.scenemanagement;
Class Battlescene:iscenestate
{public
battlescene (): Base ("Battlescene")
{
} public
override void Startscene ()
{
scenemanager.loadscene (this.mscenename);
}
}
Main City Scene class Mainmenuscenestate
Using System;
Using System.Collections.Generic;
Using System.Text;
Using Unityengine.scenemanagement;
Class Mainmenuscenestate:iscenestate
{public
mainmenuscenestate (): Base ("Mainmenuscene")
{
} Public
override void Startscene ()
{
scenemanager.loadscene (this.mscenename);
}
}
All right, all the scenes have been fulfilled.
The next step is to create a class to invoke the Management class (Scenestatemanager), which inherits from Monobehavior and mounts it to a game object
The code is as follows
Using System;
Using System.Collections.Generic;
Using System.Text;
Using Unityengine;
Class Gamecontext:monobehaviour
{
private void Start ()
{
SceneStateManager.GetInstacne.SetSceneState (New Startscenestate ());
}
private void Update ()
{
SceneStateManager.GetInstacne.UpdateSceneState ();
}
}
Summary of Content
Create a singleton management class that manages the transitions between scenes, each with its own State class to handle the logical time in the state of the scene
If you like the way this article describes the design patterns in the game, then you can focus on blog.liujunliang.com.cn more content continuously updated