refactored scripts to avoid execution order issues
This commit is contained in:
@ -107,7 +107,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
StartListening();
|
||||
StartCoroutine(WaitAndSubscribe());
|
||||
|
||||
// Immediately try to assign an enabled NPC
|
||||
if (useActiveNPC && targetNPC == null)
|
||||
@ -227,13 +227,6 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
|
||||
try
|
||||
{
|
||||
// Wait for shared listener to be ready
|
||||
if (SharedUDPListener.Instance == null)
|
||||
{
|
||||
ConvaiLogger.Error("SharedUDPListener not found! Make sure it's in the scene.", ConvaiLogger.LogCategory.Character);
|
||||
return;
|
||||
}
|
||||
|
||||
// Subscribe to shared listener
|
||||
SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||
_isListening = true;
|
||||
@ -246,6 +239,22 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
ConvaiLogger.Error($"Stack trace: {ex.StackTrace}", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator WaitAndSubscribe()
|
||||
{
|
||||
float timeout = 3f;
|
||||
while (SharedUDPListener.Instance == null && timeout > 0f)
|
||||
{
|
||||
timeout -= Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
if (SharedUDPListener.Instance == null)
|
||||
{
|
||||
ConvaiLogger.Error("SharedUDPListener not ready after wait.", ConvaiLogger.LogCategory.Character);
|
||||
yield break;
|
||||
}
|
||||
StartListening();
|
||||
}
|
||||
|
||||
public void StopListening()
|
||||
{
|
||||
|
||||
@ -144,7 +144,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
{
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
StartListening();
|
||||
StartCoroutine(WaitAndSubscribe());
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
@ -209,12 +209,6 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
|
||||
try
|
||||
{
|
||||
// Wait for shared listener to be ready
|
||||
if (SharedUDPListener.Instance == null)
|
||||
{
|
||||
ConvaiLogger.Error("SharedUDPListener not found! Make sure it's in the scene.", ConvaiLogger.LogCategory.Character);
|
||||
return;
|
||||
}
|
||||
|
||||
// Subscribe to shared listener
|
||||
SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||
@ -227,6 +221,22 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
ConvaiLogger.Error($"❌ FAILED to subscribe Speech Receiver: {ex.Message}", ConvaiLogger.LogCategory.Character);
|
||||
}
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator WaitAndSubscribe()
|
||||
{
|
||||
float timeout = 3f;
|
||||
while (SharedUDPListener.Instance == null && timeout > 0f)
|
||||
{
|
||||
timeout -= Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
if (SharedUDPListener.Instance == null)
|
||||
{
|
||||
ConvaiLogger.Error("SharedUDPListener not ready after wait.", ConvaiLogger.LogCategory.Character);
|
||||
yield break;
|
||||
}
|
||||
StartListening();
|
||||
}
|
||||
|
||||
public void StopListening()
|
||||
{
|
||||
|
||||
@ -12,6 +12,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
/// Shared UDP Listener - Single UDP socket that dispatches packets to appropriate handlers
|
||||
/// Solves the port binding issue where multiple components tried to bind to the same port
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(-10000)]
|
||||
public class SharedUDPListener : MonoBehaviour
|
||||
{
|
||||
// Singleton
|
||||
@ -43,6 +44,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
||||
return;
|
||||
}
|
||||
_instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
|
||||
@ -98,7 +98,7 @@ public class UDPAvatarReceiver : MonoBehaviour
|
||||
|
||||
if (enableReceiver)
|
||||
{
|
||||
StartUDPListener();
|
||||
StartCoroutine(WaitAndSubscribe());
|
||||
}
|
||||
|
||||
if (showDebugInfo)
|
||||
@ -199,14 +199,6 @@ public class UDPAvatarReceiver : MonoBehaviour
|
||||
{
|
||||
try
|
||||
{
|
||||
// Wait for shared listener to be ready
|
||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
||||
{
|
||||
Debug.LogError("SharedUDPListener not found! Make sure it's in the scene.");
|
||||
enableReceiver = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Subscribe to shared listener
|
||||
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||
|
||||
@ -219,6 +211,23 @@ public class UDPAvatarReceiver : MonoBehaviour
|
||||
enableReceiver = false;
|
||||
}
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator WaitAndSubscribe()
|
||||
{
|
||||
float timeout = 3f;
|
||||
while (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null && timeout > 0f)
|
||||
{
|
||||
timeout -= Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
||||
{
|
||||
Debug.LogError("SharedUDPListener not ready after wait.");
|
||||
enableReceiver = false;
|
||||
yield break;
|
||||
}
|
||||
StartUDPListener();
|
||||
}
|
||||
|
||||
void HandlePacketReceived(byte[] data, IPEndPoint senderEndPoint)
|
||||
{
|
||||
|
||||
@ -101,7 +101,7 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
|
||||
|
||||
if (enableReceiver)
|
||||
{
|
||||
StartUDPListener();
|
||||
StartCoroutine(WaitAndSubscribe());
|
||||
}
|
||||
|
||||
if (showDebugInfo)
|
||||
@ -202,14 +202,6 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
|
||||
{
|
||||
try
|
||||
{
|
||||
// Wait for shared listener to be ready
|
||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
||||
{
|
||||
Debug.LogError("SharedUDPListener not found! Make sure it's in the scene.");
|
||||
enableReceiver = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Subscribe to shared listener
|
||||
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||
|
||||
@ -222,6 +214,23 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
|
||||
enableReceiver = false;
|
||||
}
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator WaitAndSubscribe()
|
||||
{
|
||||
float timeout = 3f;
|
||||
while (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null && timeout > 0f)
|
||||
{
|
||||
timeout -= Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
||||
{
|
||||
Debug.LogError("SharedUDPListener not ready after wait.");
|
||||
enableReceiver = false;
|
||||
yield break;
|
||||
}
|
||||
StartUDPListener();
|
||||
}
|
||||
|
||||
void HandlePacketReceived(byte[] data, IPEndPoint senderEndPoint)
|
||||
{
|
||||
|
||||
@ -64,7 +64,7 @@ public class VRExperimentController : MonoBehaviour
|
||||
}
|
||||
|
||||
InitializeObjectMaps();
|
||||
StartUDPListener();
|
||||
StartCoroutine(WaitAndSubscribe());
|
||||
|
||||
// Initially deactivate all objects and avatars
|
||||
DeactivateAllObjects();
|
||||
@ -193,13 +193,6 @@ public class VRExperimentController : MonoBehaviour
|
||||
{
|
||||
try
|
||||
{
|
||||
// Wait for shared listener to be ready
|
||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
||||
{
|
||||
LogError("SharedUDPListener not found! Make sure it's in the scene.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Subscribe to shared listener
|
||||
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||
isListening = true;
|
||||
@ -212,6 +205,22 @@ public class VRExperimentController : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator WaitAndSubscribe()
|
||||
{
|
||||
float timeout = 3f;
|
||||
while (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null && timeout > 0f)
|
||||
{
|
||||
timeout -= Time.unscaledDeltaTime;
|
||||
yield return null;
|
||||
}
|
||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
||||
{
|
||||
LogError("SharedUDPListener not ready after wait.");
|
||||
yield break;
|
||||
}
|
||||
StartUDPListener();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle packet received from shared listener
|
||||
/// </summary>
|
||||
|
||||
7
Unity-Master/Assets/Scripts/script-overview.md.meta
Normal file
7
Unity-Master/Assets/Scripts/script-overview.md.meta
Normal file
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0b725a59a21efb044ba2d8b4ef29095c
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
BIN
Unity-Master/ProjectSettings/EditorBuildSettings.asset
(Stored with Git LFS)
BIN
Unity-Master/ProjectSettings/EditorBuildSettings.asset
(Stored with Git LFS)
Binary file not shown.
BIN
Unity-Master/ProjectSettings/ProjectSettings.asset
(Stored with Git LFS)
BIN
Unity-Master/ProjectSettings/ProjectSettings.asset
(Stored with Git LFS)
Binary file not shown.
Reference in New Issue
Block a user