Skip to content

Commit

Permalink
feat: Script Templates (#1217)
Browse files Browse the repository at this point in the history
* Add Script Templates

* Moved serialization section down

* Added comments to Awake & Start

* Capitalization

* meta files

* Added doc links and XML comments
  • Loading branch information
MrGadget authored and miwarnec committed Nov 23, 2019
1 parent 4770289 commit 8cf6a07
Show file tree
Hide file tree
Showing 10 changed files with 708 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using Mirror;

/*
Documentation: https://mirror-networking.com/docs/Components/NetworkManager.html
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
*/

public class #SCRIPTNAME# : NetworkManager
{
#region Unity Callbacks

public override void OnValidate()
{
base.OnValidate();
}

/// <summary>
/// Runs on both Server and Client
/// Networking is NOT initialized when this fires
/// </summary>
public override void Awake()
{
base.Awake();
}

/// <summary>
/// Runs on both Server and Client
/// Networking is NOT initialized when this fires
/// </summary>
public override void Start()
{
base.Start();
}

public override void LateUpdate()
{
base.LateUpdate();
}

public override void OnDestroy()
{
base.OnDestroy();
}

#endregion

#region Start & Stop

/// <summary>
/// Set the frame rate for a headless server.
/// <para>Override if you wish to disable the behavior or set your own tick rate.</para>
/// </summary>
public override void ConfigureServerFrameRate()
{
base.ConfigureServerFrameRate();
}

/// <summary>
/// This starts a network "host" - a server and client in the same application.
/// <para>The client returned from StartHost() is a special "local" client that communicates to the in-process server using a message queue instead of the real network. But in almost all other cases, it can be treated as a normal client.</para>
/// </summary>
public override void StartHost()
{
base.StartHost();
}

/// <summary>
/// called when quitting the application by closing the window / pressing stop in the editor
/// <para>virtual so that inheriting classes' OnApplicationQuit() can call base.OnApplicationQuit() too</para>
/// </summary>
public override void OnApplicationQuit()
{
base.OnApplicationQuit();
}

#endregion

#region Scene Management

/// <summary>
/// This causes the server to switch scenes and sets the networkSceneName.
/// <para>Clients that connect to this server will automatically switch to this scene. This is called autmatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready() again to participate in the new scene.</para>
/// </summary>
/// <param name="newSceneName"></param>
public override void ServerChangeScene(string newSceneName)
{
base.ServerChangeScene(newSceneName);
}

/// <summary>
/// This causes the server to switch scenes and sets the networkSceneName.
/// <para>Clients that connect to this server will automatically switch to this scene. This is called autmatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready() again to participate in the new scene.</para>
/// </summary>
/// <param name="newSceneName"></param>
/// <param name="sceneMode"></param>
/// <param name="physicsMode"></param>
public override void ServerChangeScene(string newSceneName, LoadSceneMode sceneMode, LocalPhysicsMode physicsMode)
{
base.ServerChangeScene(newSceneName, sceneMode, physicsMode);
}

#endregion

#region Server System Callbacks

/// <summary>
/// Called on the server when a new client connects.
/// <para>Unity calls this on the Server when a Client connects to the Server. Use an override to tell the NetworkManager what to do when a client connects to the server.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerConnect(NetworkConnection conn) { }

/// <summary>
/// Called on the server when a client is ready.
/// <para>The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerReady(NetworkConnection conn)
{
base.OnServerReady(conn);
}

/// <summary>
/// Called on the server when a client disconnects.
/// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerDisconnect(NetworkConnection conn)
{
base.OnServerDisconnect(conn);
}

/// <summary>
/// Called on the server when a client adds a new player with ClientScene.AddPlayer.
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerAddPlayer(NetworkConnection conn)
{
base.OnServerAddPlayer(conn);
}

/// <summary>
/// Called on the server when a client removes a player.
/// <para>The default implementation of this function destroys the corresponding player object.</para>
/// </summary>
/// <param name="conn">The connection to remove the player from.</param>
/// <param name="player">The player controller to remove.</param>
public override void OnServerRemovePlayer(NetworkConnection conn, NetworkIdentity player)
{
base.OnServerRemovePlayer(conn, player);
}

/// <summary>
/// Called on the server when a network error occurs for a client connection.
/// </summary>
/// <param name="conn">Connection from client.</param>
/// <param name="errorCode">Error code.</param>
public override void OnServerError(NetworkConnection conn, int errorCode) { }

/// <summary>
/// Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed
/// <para>This allows server to do work / cleanup / prep before the scene changes.</para>
/// </summary>
/// <param name="newSceneName">Name of the scene that's about to be loaded</param>
public override void OnServerChangeScene(string newSceneName) { }

/// <summary>
/// Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().
/// </summary>
/// <param name="sceneName">The name of the new scene.</param>
public override void OnServerSceneChanged(string sceneName) { }

#endregion

#region Client System Callbacks

/// <summary>
/// Called on the client when connected to a server.
/// <para>The default implementation of this function sets the client as ready and adds a player. Override the function to dictate what happens when the client connects.</para>
/// </summary>
/// <param name="conn">Connection to the server.</param>
public override void OnClientConnect(NetworkConnection conn)
{
base.OnClientConnect(conn);
}

/// <summary>
/// Called on clients when disconnected from a server.
/// <para>This is called on the client when it disconnects from the server. Override this function to decide what happens when the client disconnects.</para>
/// </summary>
/// <param name="conn">Connection to the server.</param>
public override void OnClientDisconnect(NetworkConnection conn)
{
base.OnClientDisconnect(conn);
}

/// <summary>
/// Called on clients when a network error occurs.
/// </summary>
/// <param name="conn">Connection to a server.</param>
/// <param name="errorCode">Error code.</param>
public override void OnClientError(NetworkConnection conn, int errorCode) { }

/// <summary>
/// Called on clients when a servers tells the client it is no longer ready.
/// <para>This is commonly used when switching scenes.</para>
/// </summary>
/// <param name="conn">Connection to the server.</param>
public override void OnClientNotReady(NetworkConnection conn) { }

/// <summary>
/// Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed
/// <para>This allows client to do work / cleanup / prep before the scene changes.</para>
/// </summary>
/// <param name="newSceneName">Name of the scene that's about to be loaded</param>
/// <param name="sceneOperation">Scene operation that's about to happen</param>
public override void OnClientChangeScene(string newSceneName, SceneOperation sceneOperation) { }

/// <summary>
/// Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
/// <para>Scene changes can cause player objects to be destroyed. The default implementation of OnClientSceneChanged in the NetworkManager is to add a player object for the connection if no player object exists.</para>
/// </summary>
/// <param name="conn">The network connection that the scene change message arrived on.</param>
public override void OnClientSceneChanged(NetworkConnection conn)
{
base.OnClientSceneChanged(conn);
}

#endregion

#region Start & Stop Callbacks

// Since there are multiple versions of StartServer, StartClient and StartHost, to reliably customize
// their functionality, users would need override all the versions. Instead these callbacks are invoked
// from all versions, so users only need to implement this one case.

/// <summary>
/// This is invoked when a host is started.
/// <para>StartHost has multiple signatures, but they all cause this hook to be called.</para>
/// </summary>
public override void OnStartHost() { }

/// <summary>
/// This is invoked when a server is started - including when a host is started.
/// <para>StartServer has multiple signatures, but they all cause this hook to be called.</para>
/// </summary>
public override void OnStartServer() { }

/// <summary>
/// This is invoked when the client is started.
/// </summary>
public override void OnStartClient()
{
base.OnStartClient();
}

/// <summary>
/// This is called when a server is stopped - including when a host is stopped.
/// </summary>
public override void OnStopServer() { }

/// <summary>
/// This is called when a client is stopped.
/// </summary>
public override void OnStopClient() { }

/// <summary>
/// This is called when a host is stopped.
/// </summary>
public override void OnStopHost() { }

#endregion
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using Mirror;

/*
Authenticators: https://mirror-networking.com/docs/Components/Authenticators/
Documentation: https://mirror-networking.com/docs/Guides/Authentication.html
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkAuthenticator.html
*/

public class #SCRIPTNAME# : NetworkAuthenticator
{
#region Messages

public class AuthRequestMessage : MessageBase { }

public class AuthResponseMessage : MessageBase { }

#endregion

#region Server

/// <summary>
/// Called on server from StartServer to initialize the Authenticator
/// <para>Server message handlers should be registered in this method.</para>
/// </summary>
public override void OnStartServer()
{
// register a handler for the authentication request we expect from client
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
}

/// <summary>
/// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
/// </summary>
/// <param name="conn">Connection to client.</param>
public override void OnServerAuthenticate(NetworkConnection conn) { }

public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
{
AuthResponseMessage authResponseMessage = new AuthResponseMessage();

conn.Send(authResponseMessage);

// Invoke the event to complete a successful authentication
base.OnServerAuthenticated.Invoke(conn);
}

#endregion

#region Client

/// <summary>
/// Called on client from StartClient to initialize the Authenticator
/// <para>Client message handlers should be registered in this method.</para>
/// </summary>
public override void OnStartClient()
{
// register a handler for the authentication response we expect from server
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
}

/// <summary>
/// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
/// </summary>
/// <param name="conn">Connection of the client.</param>
public override void OnClientAuthenticate(NetworkConnection conn)
{
AuthRequestMessage authRequestMessage = new AuthRequestMessage();

NetworkClient.Send(authRequestMessage);
}

public void OnAuthResponseMessage(NetworkConnection conn, AuthResponseMessage msg)
{
// Invoke the event to complete a successful authentication
base.OnClientAuthenticated.Invoke(conn);
}

#endregion
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8cf6a07

Please sign in to comment.