fixed port conflicts

This commit is contained in:
tom.hempel
2025-09-21 23:54:42 +02:00
parent 4fbc109b89
commit e5580ac741
6 changed files with 54 additions and 10 deletions

View File

@ -66,7 +66,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
var cfg = networkConfigAsset != null ? networkConfigAsset : NetworkConfig.Instance;
if (cfg != null)
{
listenPort = cfg.port;
listenPort = cfg.multiplayerAudioPort;
}
}
InitializeNetwork();
@ -78,6 +78,16 @@ namespace Convai.Scripts.Runtime.Multiplayer
ConvaiNPCManager.Instance.OnActiveNPCChanged += HandleActiveNPCChanged;
}
}
private void OnEnable()
{
// When re-enabled, ensure listener is running
if (_cancellationTokenSource == null)
{
_cancellationTokenSource = new CancellationTokenSource();
}
StartListening();
}
private void OnDestroy()
{
@ -91,6 +101,12 @@ namespace Convai.Scripts.Runtime.Multiplayer
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
}
private void OnDisable()
{
// Free the UDP port when this NPC gets disabled
StopListening();
}
private void Update()
{
@ -115,8 +131,13 @@ namespace Convai.Scripts.Runtime.Multiplayer
private void InitializeConvai()
{
// Get target NPC
if (useActiveNPC)
// Prefer local ConvaiNPC on the same GameObject, then fall back to active NPC
var localNPC = GetComponent<ConvaiNPC>();
if (localNPC != null)
{
targetNPC = localNPC;
}
else if (useActiveNPC)
{
targetNPC = ConvaiNPCManager.Instance?.GetActiveConvaiNPC();
}