Skip to content
This repository was archived by the owner on Nov 12, 2023. It is now read-only.

Commit 8ee4cea

Browse files
authored
Add files via upload
1 parent a5aecf0 commit 8ee4cea

8 files changed

+1032
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.SceneManagement;
4+
using Mirror;
5+
6+
/*
7+
Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
8+
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
9+
*/
10+
11+
public class #SCRIPTNAME# : NetworkManager
12+
{
13+
// Overrides the base singleton so we don't
14+
// have to cast to this type everywhere.
15+
public static new #SCRIPTNAME# singleton { get; private set; }
16+
17+
#region Unity Callbacks
18+
19+
public override void OnValidate()
20+
{
21+
base.OnValidate();
22+
}
23+
24+
/// <summary>
25+
/// Runs on both Server and Client
26+
/// Networking is NOT initialized when this fires
27+
/// </summary>
28+
public override void Awake()
29+
{
30+
base.Awake();
31+
}
32+
33+
/// <summary>
34+
/// Runs on both Server and Client
35+
/// Networking is NOT initialized when this fires
36+
/// </summary>
37+
public override void Start()
38+
{
39+
singleton = this;
40+
base.Start();
41+
}
42+
43+
/// <summary>
44+
/// Runs on both Server and Client
45+
/// </summary>
46+
public override void LateUpdate()
47+
{
48+
base.LateUpdate();
49+
}
50+
51+
/// <summary>
52+
/// Runs on both Server and Client
53+
/// </summary>
54+
public override void OnDestroy()
55+
{
56+
base.OnDestroy();
57+
}
58+
59+
#endregion
60+
61+
#region Start & Stop
62+
63+
/// <summary>
64+
/// Set the frame rate for a headless server.
65+
/// <para>Override if you wish to disable the behavior or set your own tick rate.</para>
66+
/// </summary>
67+
public override void ConfigureHeadlessFrameRate()
68+
{
69+
base.ConfigureHeadlessFrameRate();
70+
}
71+
72+
/// <summary>
73+
/// called when quitting the application by closing the window / pressing stop in the editor
74+
/// </summary>
75+
public override void OnApplicationQuit()
76+
{
77+
base.OnApplicationQuit();
78+
}
79+
80+
#endregion
81+
82+
#region Scene Management
83+
84+
/// <summary>
85+
/// This causes the server to switch scenes and sets the networkSceneName.
86+
/// <para>Clients that connect to this server will automatically switch to this scene. This is called automatically 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>
87+
/// </summary>
88+
/// <param name="newSceneName"></param>
89+
public override void ServerChangeScene(string newSceneName)
90+
{
91+
base.ServerChangeScene(newSceneName);
92+
}
93+
94+
/// <summary>
95+
/// Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed
96+
/// <para>This allows server to do work / cleanup / prep before the scene changes.</para>
97+
/// </summary>
98+
/// <param name="newSceneName">Name of the scene that's about to be loaded</param>
99+
public override void OnServerChangeScene(string newSceneName) { }
100+
101+
/// <summary>
102+
/// Called on the server when a scene is completed loaded, when the scene load was initiated by the server with ServerChangeScene().
103+
/// </summary>
104+
/// <param name="sceneName">The name of the new scene.</param>
105+
public override void OnServerSceneChanged(string sceneName) { }
106+
107+
/// <summary>
108+
/// Called from ClientChangeScene immediately before SceneManager.LoadSceneAsync is executed
109+
/// <para>This allows client to do work / cleanup / prep before the scene changes.</para>
110+
/// </summary>
111+
/// <param name="newSceneName">Name of the scene that's about to be loaded</param>
112+
/// <param name="sceneOperation">Scene operation that's about to happen</param>
113+
/// <param name="customHandling">true to indicate that scene loading will be handled through overrides</param>
114+
public override void OnClientChangeScene(string newSceneName, SceneOperation sceneOperation, bool customHandling) { }
115+
116+
/// <summary>
117+
/// Called on clients when a scene has completed loaded, when the scene load was initiated by the server.
118+
/// <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>
119+
/// </summary>
120+
public override void OnClientSceneChanged()
121+
{
122+
base.OnClientSceneChanged();
123+
}
124+
125+
#endregion
126+
127+
#region Server System Callbacks
128+
129+
/// <summary>
130+
/// Called on the server when a new client connects.
131+
/// <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>
132+
/// </summary>
133+
/// <param name="conn">Connection from client.</param>
134+
public override void OnServerConnect(NetworkConnectionToClient conn) { }
135+
136+
/// <summary>
137+
/// Called on the server when a client is ready.
138+
/// <para>The default implementation of this function calls NetworkServer.SetClientReady() to continue the network setup process.</para>
139+
/// </summary>
140+
/// <param name="conn">Connection from client.</param>
141+
public override void OnServerReady(NetworkConnectionToClient conn)
142+
{
143+
base.OnServerReady(conn);
144+
}
145+
146+
/// <summary>
147+
/// Called on the server when a client adds a new player with ClientScene.AddPlayer.
148+
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
149+
/// </summary>
150+
/// <param name="conn">Connection from client.</param>
151+
public override void OnServerAddPlayer(NetworkConnectionToClient conn)
152+
{
153+
base.OnServerAddPlayer(conn);
154+
}
155+
156+
/// <summary>
157+
/// Called on the server when a client disconnects.
158+
/// <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>
159+
/// </summary>
160+
/// <param name="conn">Connection from client.</param>
161+
public override void OnServerDisconnect(NetworkConnectionToClient conn)
162+
{
163+
base.OnServerDisconnect(conn);
164+
}
165+
166+
/// <summary>
167+
/// Called on server when transport raises an exception.
168+
/// <para>NetworkConnection may be null.</para>
169+
/// </summary>
170+
/// <param name="conn">Connection of the client...may be null</param>
171+
/// <param name="exception">Exception thrown from the Transport.</param>
172+
public override void OnServerError(NetworkConnectionToClient conn, TransportError transportError, string message) { }
173+
174+
#endregion
175+
176+
#region Client System Callbacks
177+
178+
/// <summary>
179+
/// Called on the client when connected to a server.
180+
/// <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>
181+
/// </summary>
182+
public override void OnClientConnect()
183+
{
184+
base.OnClientConnect();
185+
}
186+
187+
/// <summary>
188+
/// Called on clients when disconnected from a server.
189+
/// <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>
190+
/// </summary>
191+
public override void OnClientDisconnect()
192+
{
193+
base.OnClientDisconnect();
194+
}
195+
196+
/// <summary>
197+
/// Called on clients when a servers tells the client it is no longer ready.
198+
/// <para>This is commonly used when switching scenes.</para>
199+
/// </summary>
200+
public override void OnClientNotReady() { }
201+
202+
/// <summary>
203+
/// Called on client when transport raises an exception.</summary>
204+
/// </summary>
205+
/// <param name="exception">Exception thrown from the Transport.</param>
206+
public override void OnClientError(TransportError transportError, string message) { }
207+
208+
#endregion
209+
210+
#region Start & Stop Callbacks
211+
212+
// Since there are multiple versions of StartServer, StartClient and StartHost, to reliably customize
213+
// their functionality, users would need override all the versions. Instead these callbacks are invoked
214+
// from all versions, so users only need to implement this one case.
215+
216+
/// <summary>
217+
/// This is invoked when a host is started.
218+
/// <para>StartHost has multiple signatures, but they all cause this hook to be called.</para>
219+
/// </summary>
220+
public override void OnStartHost() { }
221+
222+
/// <summary>
223+
/// This is invoked when a server is started - including when a host is started.
224+
/// <para>StartServer has multiple signatures, but they all cause this hook to be called.</para>
225+
/// </summary>
226+
public override void OnStartServer() { }
227+
228+
/// <summary>
229+
/// This is invoked when the client is started.
230+
/// </summary>
231+
public override void OnStartClient() { }
232+
233+
/// <summary>
234+
/// This is called when a host is stopped.
235+
/// </summary>
236+
public override void OnStopHost() { }
237+
238+
/// <summary>
239+
/// This is called when a server is stopped - including when a host is stopped.
240+
/// </summary>
241+
public override void OnStopServer() { }
242+
243+
/// <summary>
244+
/// This is called when a client is stopped.
245+
/// </summary>
246+
public override void OnStopClient() { }
247+
248+
#endregion
249+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using Mirror;
5+
using UnityEngine;
6+
7+
/*
8+
Documentation: https://mirror-networking.gitbook.io/docs/components/network-authenticators
9+
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkAuthenticator.html
10+
*/
11+
12+
public class #SCRIPTNAME# : NetworkAuthenticator
13+
{
14+
#region Messages
15+
16+
public struct AuthRequestMessage : NetworkMessage { }
17+
18+
public struct AuthResponseMessage : NetworkMessage { }
19+
20+
#endregion
21+
22+
#region Server
23+
24+
/// <summary>
25+
/// Called on server from StartServer to initialize the Authenticator
26+
/// <para>Server message handlers should be registered in this method.</para>
27+
/// </summary>
28+
public override void OnStartServer()
29+
{
30+
// register a handler for the authentication request we expect from client
31+
NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
32+
}
33+
34+
/// <summary>
35+
/// Called on server from OnServerConnectInternal when a client needs to authenticate
36+
/// </summary>
37+
/// <param name="conn">Connection to client.</param>
38+
public override void OnServerAuthenticate(NetworkConnectionToClient conn) { }
39+
40+
/// <summary>
41+
/// Called on server when the client's AuthRequestMessage arrives
42+
/// </summary>
43+
/// <param name="conn">Connection to client.</param>
44+
/// <param name="msg">The message payload</param>
45+
public void OnAuthRequestMessage(NetworkConnectionToClient conn, AuthRequestMessage msg)
46+
{
47+
AuthResponseMessage authResponseMessage = new AuthResponseMessage();
48+
49+
conn.Send(authResponseMessage);
50+
51+
// Accept the successful authentication
52+
ServerAccept(conn);
53+
}
54+
55+
#endregion
56+
57+
#region Client
58+
59+
/// <summary>
60+
/// Called on client from StartClient to initialize the Authenticator
61+
/// <para>Client message handlers should be registered in this method.</para>
62+
/// </summary>
63+
public override void OnStartClient()
64+
{
65+
// register a handler for the authentication response we expect from server
66+
NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
67+
}
68+
69+
/// <summary>
70+
/// Called on client from OnClientConnectInternal when a client needs to authenticate
71+
/// </summary>
72+
public override void OnClientAuthenticate()
73+
{
74+
AuthRequestMessage authRequestMessage = new AuthRequestMessage();
75+
76+
NetworkClient.Send(authRequestMessage);
77+
}
78+
79+
/// <summary>
80+
/// Called on client when the server's AuthResponseMessage arrives
81+
/// </summary>
82+
/// <param name="msg">The message payload</param>
83+
public void OnAuthResponseMessage(AuthResponseMessage msg)
84+
{
85+
// Authentication has been accepted
86+
ClientAccept();
87+
}
88+
89+
#endregion
90+
}

0 commit comments

Comments
 (0)