mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
Merged master
This commit is contained in:
commit
cc5b9eaa6f
@ -14,27 +14,6 @@ public class NetworkDiscovery : NetworkDiscoveryBase<ServerRequest, ServerRespon
|
||||
{
|
||||
#region Server
|
||||
|
||||
public long ServerId { get; private set; }
|
||||
|
||||
[Tooltip("Transport to be advertised during discovery")]
|
||||
public Transport transport;
|
||||
|
||||
[Tooltip("Invoked when a server is found")]
|
||||
public ServerFoundUnityEvent OnServerFound;
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
ServerId = RandomLong();
|
||||
|
||||
// active transport gets initialized in awake
|
||||
// so make sure we set it here in Start() (after awakes)
|
||||
// Or just let the user assign it in the inspector
|
||||
if (transport == null)
|
||||
transport = Transport.active;
|
||||
|
||||
base.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process the request from a client
|
||||
/// </summary>
|
||||
@ -68,9 +47,11 @@ protected override ServerResponse ProcessRequest(ServerRequest request, IPEndPoi
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Client
|
||||
|
||||
/// <summary>
|
||||
/// Create a message that will be broadcasted on the network to discover servers
|
||||
/// </summary>
|
||||
@ -106,6 +87,7 @@ protected override void ProcessResponse(ServerResponse response, IPEndPoint endp
|
||||
|
||||
OnServerFound.Invoke(response);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -23,33 +23,46 @@ public abstract class NetworkDiscoveryBase<Request, Response> : MonoBehaviour
|
||||
{
|
||||
public static bool SupportedOnThisPlatform { get { return Application.platform != RuntimePlatform.WebGLPlayer; } }
|
||||
|
||||
// each game should have a random unique handshake, this way you can tell if this is the same game or not
|
||||
[HideInInspector]
|
||||
public long secretHandshake;
|
||||
[SerializeField]
|
||||
[Tooltip("If true, broadcasts a discovery request every ActiveDiscoveryInterval seconds")]
|
||||
public bool enableActiveDiscovery = true;
|
||||
|
||||
// broadcast address needs to be configurable on iOS:
|
||||
// https://github.com/vis2k/Mirror/pull/3255
|
||||
[Tooltip("iOS may require LAN IP address here (e.g. 192.168.x.x), otherwise leave blank.")]
|
||||
public string BroadcastAddress = "";
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The UDP port the server will listen for multi-cast messages")]
|
||||
protected int serverBroadcastListenPort = 47777;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("If true, broadcasts a discovery request every ActiveDiscoveryInterval seconds")]
|
||||
public bool enableActiveDiscovery = true;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Time in seconds between multi-cast messages")]
|
||||
[Range(1, 60)]
|
||||
float ActiveDiscoveryInterval = 3;
|
||||
|
||||
// broadcast address needs to be configurable on iOS:
|
||||
// https://github.com/vis2k/Mirror/pull/3255
|
||||
public string BroadcastAddress = "";
|
||||
[Tooltip("Transport to be advertised during discovery")]
|
||||
public Transport transport;
|
||||
|
||||
[Tooltip("Invoked when a server is found")]
|
||||
public ServerFoundUnityEvent OnServerFound;
|
||||
|
||||
// Each game should have a random unique handshake,
|
||||
// this way you can tell if this is the same game or not
|
||||
[HideInInspector]
|
||||
public long secretHandshake;
|
||||
|
||||
public long ServerId { get; private set; }
|
||||
|
||||
protected UdpClient serverUdpClient;
|
||||
protected UdpClient clientUdpClient;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
public virtual void OnValidate()
|
||||
{
|
||||
if (transport == null)
|
||||
transport = GetComponent<Transport>();
|
||||
|
||||
if (secretHandshake == 0)
|
||||
{
|
||||
secretHandshake = RandomLong();
|
||||
@ -58,24 +71,32 @@ void OnValidate()
|
||||
}
|
||||
#endif
|
||||
|
||||
public static long RandomLong()
|
||||
{
|
||||
int value1 = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
||||
int value2 = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
||||
return value1 + ((long)value2 << 32);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// virtual so that inheriting classes' Start() can call base.Start() too
|
||||
/// </summary>
|
||||
public virtual void Start()
|
||||
{
|
||||
ServerId = RandomLong();
|
||||
|
||||
// active transport gets initialized in Awake
|
||||
// so make sure we set it here in Start() after Awake
|
||||
// Or just let the user assign it in the inspector
|
||||
if (transport == null)
|
||||
transport = Transport.active;
|
||||
|
||||
// Server mode? then start advertising
|
||||
#if UNITY_SERVER
|
||||
AdvertiseServer();
|
||||
#endif
|
||||
}
|
||||
|
||||
public static long RandomLong()
|
||||
{
|
||||
int value1 = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
||||
int value2 = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
|
||||
return value1 + ((long)value2 << 32);
|
||||
}
|
||||
|
||||
// Ensure the ports are cleared no matter when Game/Unity UI exits
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
@ -166,9 +187,7 @@ public async Task ServerListenAsync()
|
||||
// socket has been closed
|
||||
break;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
catch (Exception) {}
|
||||
}
|
||||
}
|
||||
|
||||
@ -247,6 +266,7 @@ protected virtual void ProcessClientRequest(Request request, IPEndPoint endpoint
|
||||
AndroidJavaObject multicastLock;
|
||||
bool hasMulticastLock;
|
||||
#endif
|
||||
|
||||
void BeginMulticastLock()
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
|
@ -9,11 +9,12 @@ namespace Mirror
|
||||
[HelpURL("https://mirror-networking.gitbook.io/docs/guides/interest-management")]
|
||||
public abstract class InterestManagementBase : MonoBehaviour
|
||||
{
|
||||
// Awake configures InterestManagementBase in NetworkServer/Client
|
||||
// Configures InterestManagementBase in NetworkServer/Client
|
||||
// Do NOT check for active server or client here.
|
||||
// Awake must always set the static aoi references.
|
||||
// make sure to call base.Awake when overwriting!
|
||||
protected virtual void Awake()
|
||||
// OnEnable must always set the static aoi references.
|
||||
// make sure to call base.OnEnable when overwriting!
|
||||
// Previously used Awake()
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (NetworkServer.aoi == null)
|
||||
{
|
||||
|
@ -36,9 +36,14 @@ public abstract class NetworkBehaviour : MonoBehaviour
|
||||
/// <summary>sync interval for OnSerialize (in seconds)</summary>
|
||||
// hidden because NetworkBehaviourInspector shows it only if has OnSerialize.
|
||||
// [0,2] should be enough. anything >2s is too laggy anyway.
|
||||
//
|
||||
// NetworkServer & NetworkClient broadcast() are behind a sendInterval timer now.
|
||||
// it makes sense to keep every component's syncInterval setting at '0' by default.
|
||||
// otherwise, the overlapping timers could introduce unexpected latency.
|
||||
// careful: default of '0.1' may
|
||||
[Tooltip("Time in seconds until next change is synchronized to the client. '0' means send immediately if changed. '0.5' means only send changes every 500ms.\n(This is for state synchronization like SyncVars, SyncLists, OnSerialize. Not for Cmds, Rpcs, etc.)")]
|
||||
[Range(0, 2)]
|
||||
[HideInInspector] public float syncInterval = 0.1f;
|
||||
[HideInInspector] public float syncInterval = 0;
|
||||
internal double lastSyncTime;
|
||||
|
||||
/// <summary>True if this object is on the server and has been spawned.</summary>
|
||||
|
@ -438,9 +438,9 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: 363a8867bb9c7b845a73233566df8c1e, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
speed: 1
|
||||
fadeImage: {fileID: 1040404845}
|
||||
fadeColor: {r: 0, g: 0, b: 0, a: 1}
|
||||
stepRate: 2
|
||||
--- !u!114 &1300359892
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -7,20 +7,33 @@ namespace Mirror.Examples.AdditiveLevels
|
||||
public class FadeInOut : MonoBehaviour
|
||||
{
|
||||
// set these in the inspector
|
||||
[Range(1, 100), Tooltip("Speed of fade in / out: lower is slower")]
|
||||
public byte speed = 1;
|
||||
|
||||
[Tooltip("Reference to Image component on child panel")]
|
||||
public Image fadeImage;
|
||||
|
||||
[Tooltip("Color to use during scene transition")]
|
||||
public Color fadeColor = Color.black;
|
||||
|
||||
WaitForSeconds waitForSeconds;
|
||||
[Range(1, 100), Tooltip("Rate of fade in / out: higher is faster")]
|
||||
public byte stepRate = 2;
|
||||
|
||||
void Awake()
|
||||
float step;
|
||||
|
||||
void Start()
|
||||
{
|
||||
waitForSeconds = new WaitForSeconds(speed * 0.01f);
|
||||
// Convert user-friendly setting value to working value
|
||||
step = stepRate * 0.001f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates FadeIn / FadeOut time.
|
||||
/// </summary>
|
||||
/// <returns>Duration in seconds</returns>
|
||||
public float GetDuration()
|
||||
{
|
||||
float frames = 1 / step;
|
||||
float frameRate = Time.deltaTime;
|
||||
float duration = frames * frameRate * 0.1f;
|
||||
return duration;
|
||||
}
|
||||
|
||||
public IEnumerator FadeIn()
|
||||
@ -29,8 +42,8 @@ public IEnumerator FadeIn()
|
||||
|
||||
while (alpha < 1)
|
||||
{
|
||||
yield return waitForSeconds;
|
||||
alpha += 0.01f;
|
||||
yield return null;
|
||||
alpha += step;
|
||||
fadeColor.a = alpha;
|
||||
fadeImage.color = fadeColor;
|
||||
}
|
||||
@ -42,8 +55,8 @@ public IEnumerator FadeOut()
|
||||
|
||||
while (alpha > 0)
|
||||
{
|
||||
yield return waitForSeconds;
|
||||
alpha -= 0.01f;
|
||||
yield return null;
|
||||
alpha -= step;
|
||||
fadeColor.a = alpha;
|
||||
fadeImage.color = fadeColor;
|
||||
}
|
||||
|
@ -25,9 +25,6 @@ public void OnLabelTextChanged(string _, string newValue)
|
||||
label.text = labelText;
|
||||
}
|
||||
|
||||
// This is approximately the fade time
|
||||
WaitForSeconds waitForFade = new WaitForSeconds(2f);
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
labelText = Path.GetFileNameWithoutExtension(destinationScene);
|
||||
@ -68,7 +65,7 @@ IEnumerator SendPlayerToNewScene(GameObject player)
|
||||
// Tell client to unload previous subscene. No custom handling for this.
|
||||
conn.Send(new SceneMessage { sceneName = gameObject.scene.path, sceneOperation = SceneOperation.UnloadAdditive, customHandling = true });
|
||||
|
||||
yield return waitForFade;
|
||||
yield return new WaitForSeconds(AdditiveLevelsNetworkManager.singleton.fadeInOut.GetDuration());
|
||||
|
||||
NetworkServer.RemovePlayerForConnection(conn, false);
|
||||
|
||||
|
@ -501,12 +501,11 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: c761308e733c51245b2e8bb4201f46dc, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
secretHandshake: 1558261479176021378
|
||||
serverBroadcastListenPort: 47777
|
||||
enableActiveDiscovery: 1
|
||||
ActiveDiscoveryInterval: 3
|
||||
BroadcastAddress:
|
||||
transport: {fileID: 0}
|
||||
serverBroadcastListenPort: 47777
|
||||
ActiveDiscoveryInterval: 3
|
||||
transport: {fileID: 1556883244}
|
||||
OnServerFound:
|
||||
m_PersistentCalls:
|
||||
m_Calls:
|
||||
@ -522,6 +521,7 @@ MonoBehaviour:
|
||||
m_StringArgument:
|
||||
m_BoolArgument: 0
|
||||
m_CallState: 2
|
||||
secretHandshake: 1558261479176021378
|
||||
--- !u!1 &1611696151
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -9,18 +9,34 @@ using Mirror.Discovery;
|
||||
|
||||
public struct DiscoveryRequest : NetworkMessage
|
||||
{
|
||||
// Add properties for whatever information you want sent by clients
|
||||
// in their broadcast messages that servers will consume.
|
||||
// Add public fields (not properties) for whatever information you want
|
||||
// sent by clients in their broadcast messages that servers will use.
|
||||
}
|
||||
|
||||
public struct DiscoveryResponse : NetworkMessage
|
||||
{
|
||||
// Add properties for whatever information you want the server to return to
|
||||
// clients for them to display or consume for establishing a connection.
|
||||
// Add public fields (not properties) for whatever information you want the server
|
||||
// to return to clients for them to display or use for establishing a connection.
|
||||
}
|
||||
|
||||
public class #SCRIPTNAME# : NetworkDiscoveryBase<DiscoveryRequest, DiscoveryResponse>
|
||||
{
|
||||
#region Unity Callbacks
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public override void OnValidate()
|
||||
{
|
||||
base.OnValidate();
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
base.Start();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Server
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user