forked from cgvr/DeltaVR
deltavr multiplayer 2.0
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using FishNet.Object;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
namespace _PROJECT.Multiplayer
|
||||
{
|
||||
public class XRMultiplayerInteractionManager : XRInteractionManager
|
||||
{
|
||||
private XRMultiplayerInteractionManagerBehavior _networkBehavior;
|
||||
|
||||
private new void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
_networkBehavior = GetComponentInChildren<XRMultiplayerInteractionManagerBehavior>();
|
||||
}
|
||||
|
||||
public override void SelectEnter(IXRSelectInteractor interactor, IXRSelectInteractable interactable)
|
||||
{
|
||||
Debug.Log(interactor);
|
||||
Debug.Log(interactable);
|
||||
Debug.Log("SelectEnter called on " + interactable.transform.name + " by " + interactor.transform.name);
|
||||
NetworkObject nInteractable = GetNetworkObject(interactable);
|
||||
if (nInteractable == null)
|
||||
{
|
||||
// Interactable is not a network object, so we can just select it.
|
||||
base.SelectEnter(interactor, interactable);
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkObject nInteractor = GetNetworkObject(interactor);
|
||||
if (nInteractor == null)
|
||||
{
|
||||
// Interactable is a network object, but interactor is not.
|
||||
base.SelectEnter(interactor, interactable);
|
||||
return;
|
||||
}
|
||||
|
||||
// Both interactable and interactor are network objects, so we should broadcast selectEnter to all clients.
|
||||
_networkBehavior.RequestSelectEnterBroadcastRPC(nInteractor, nInteractable);
|
||||
}
|
||||
|
||||
public override void SelectExit(IXRSelectInteractor interactor, IXRSelectInteractable interactable)
|
||||
{
|
||||
Debug.Log("SelectExit called on " + interactable.transform.name + " by " + interactor.transform.name);
|
||||
NetworkObject nInteractable = GetNetworkObject(interactable);
|
||||
if (nInteractable == null)
|
||||
{
|
||||
// Interactable is not a network object, so we can just unselect it.
|
||||
base.SelectExit(interactor, interactable);
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkObject nInteractor = GetNetworkObject(interactor);
|
||||
if (nInteractor == null)
|
||||
{
|
||||
// Interactable is a network object, but interactor is not.
|
||||
base.SelectExit(interactor, interactable);
|
||||
return;
|
||||
}
|
||||
|
||||
// Both interactable and interactor are network objects, so we should broadcast selectExit to all clients.
|
||||
_networkBehavior.RequestSelectExitBroadcastRPC(nInteractor, nInteractable);
|
||||
}
|
||||
|
||||
private static NetworkObject GetNetworkObject(IXRInteractable interactable)
|
||||
{
|
||||
return interactable.transform.GetComponent<NetworkObject>();
|
||||
}
|
||||
|
||||
private static NetworkObject GetNetworkObject(IXRInteractor interactor)
|
||||
{
|
||||
return interactor.transform.GetComponent<NetworkObject>();
|
||||
}
|
||||
|
||||
public void BaseSelectEnter(IXRSelectInteractor iInteractor, IXRSelectInteractable iInteractable)
|
||||
{
|
||||
base.SelectEnter(iInteractor, iInteractable);
|
||||
}
|
||||
|
||||
public void BaseSelectExit(IXRSelectInteractor iInteractor, IXRSelectInteractable iInteractable)
|
||||
{
|
||||
base.SelectExit(iInteractor, iInteractable);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8ff406473ac40c988cd296f4d8c4f62
|
||||
timeCreated: 1679223018
|
||||
@@ -0,0 +1,97 @@
|
||||
using FishNet.Connection;
|
||||
using FishNet.Object;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
|
||||
namespace _PROJECT.Multiplayer
|
||||
{
|
||||
public class XRMultiplayerInteractionManagerBehavior : NetworkBehaviour
|
||||
{
|
||||
private XRMultiplayerInteractionManager _manager;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_manager = GetComponentInParent<XRMultiplayerInteractionManager>();
|
||||
}
|
||||
|
||||
[ServerRpc(RequireOwnership = false)]
|
||||
public void RequestSelectEnterBroadcastRPC(NetworkObject interactor, NetworkObject interactable, NetworkConnection conn = null)
|
||||
{
|
||||
Debug.Log("[SERVER] Received SelectEnter for " + interactable.name + " to " + interactor.name);
|
||||
|
||||
// Get interactable and interactor.
|
||||
IXRSelectInteractable iInteractable = interactable.transform.GetComponent<IXRSelectInteractable>();
|
||||
IXRSelectInteractor iInteractor = interactor.transform.GetComponent<IXRSelectInteractor>();
|
||||
|
||||
// If interactable or interactor is null, return.
|
||||
if (iInteractable == null || iInteractor == null)
|
||||
{
|
||||
Debug.LogWarning("[SERVER] Unexpected null interactable or interactor in RequestSelectEnterBroadcastRPC.");
|
||||
return;
|
||||
};
|
||||
|
||||
// Broadcast select enter to all clients.
|
||||
_manager.BaseSelectEnter(iInteractor, iInteractable);
|
||||
SelectEnterBroadcastRPC(interactor, interactable);
|
||||
}
|
||||
|
||||
[ObserversRpc]
|
||||
public void SelectEnterBroadcastRPC(NetworkObject interactor, NetworkObject interactable)
|
||||
{
|
||||
Debug.Log("[CLIENT] Recieved SelectEnter from server for " + interactable.name + " to " + interactor.name);
|
||||
|
||||
// Get interactable and interactor.
|
||||
IXRSelectInteractable iInteractable = interactable.transform.GetComponent<IXRSelectInteractable>();
|
||||
IXRSelectInteractor iInteractor = interactor.transform.GetComponent<IXRSelectInteractor>();
|
||||
|
||||
// If interactable or interactor is null, return.
|
||||
if (iInteractable == null || iInteractor == null)
|
||||
{
|
||||
Debug.LogWarning("[CLIENT] Unexpected null interactable or interactor in SelectEnterBroadcastRPC.");
|
||||
return;
|
||||
};
|
||||
|
||||
_manager.BaseSelectEnter(iInteractor, iInteractable);
|
||||
}
|
||||
|
||||
[ServerRpc(RequireOwnership = false)]
|
||||
public void RequestSelectExitBroadcastRPC(NetworkObject nInteractor, NetworkObject nInteractable)
|
||||
{
|
||||
Debug.Log("[SERVER] Received SelectExit for " + nInteractable.name + " to " + nInteractor.name);
|
||||
|
||||
// Get interactable and interactor.
|
||||
IXRSelectInteractable iInteractable = nInteractable.transform.GetComponent<IXRSelectInteractable>();
|
||||
IXRSelectInteractor iInteractor = nInteractor.transform.GetComponent<IXRSelectInteractor>();
|
||||
|
||||
// If interactable or interactor is null, return.
|
||||
if (iInteractable == null || iInteractor == null)
|
||||
{
|
||||
Debug.LogWarning("[SERVER] Unexpected null interactable or interactor in RequestSelectExitBroadcastRPC.");
|
||||
return;
|
||||
};
|
||||
|
||||
// Broadcast select exit to all clients.
|
||||
_manager.BaseSelectExit(iInteractor, iInteractable);
|
||||
}
|
||||
|
||||
[ObserversRpc]
|
||||
public void SelectExitBroadcastRPC(NetworkObject nInteractor, NetworkObject nInteractable)
|
||||
{
|
||||
Debug.Log("[CLIENT] Recieved SelectExit from server for " + nInteractable.name + " to " + nInteractor.name);
|
||||
|
||||
// Get interactable and interactor.
|
||||
IXRSelectInteractable iInteractable = nInteractable.transform.GetComponent<IXRSelectInteractable>();
|
||||
IXRSelectInteractor iInteractor = nInteractor.transform.GetComponent<IXRSelectInteractor>();
|
||||
|
||||
// If interactable or interactor is null, return.
|
||||
if (iInteractable == null || iInteractor == null)
|
||||
{
|
||||
Debug.LogWarning("[CLIENT] Unexpected null interactable or interactor in SelectExitBroadcastRPC.");
|
||||
return;
|
||||
};
|
||||
|
||||
_manager.BaseSelectExit(iInteractor, iInteractable);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ba3011b0b004a8089e25da0e59cf7e0
|
||||
timeCreated: 1679225525
|
||||
Reference in New Issue
Block a user