50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class NetworkUsableHandItemInteractable : NetworkHandItemInteractable
|
|
{
|
|
[Tooltip("Component on this prefab implementing IHandItemUseAction.")]
|
|
[SerializeField] private MonoBehaviour useActionComponent;
|
|
|
|
private IHandItemUseAction UseAction => useActionComponent as IHandItemUseAction;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
ResolveUseAction();
|
|
}
|
|
|
|
private void OnValidate() => ResolveUseAction();
|
|
|
|
protected override void UseOnServer(NetworkObject playerObject)
|
|
{
|
|
if (UseAction == null)
|
|
{
|
|
Debug.LogWarning(
|
|
$"{name} was used but has no IHandItemUseAction component assigned.",
|
|
this);
|
|
return;
|
|
}
|
|
|
|
UseAction.TryUseOnServer(playerObject);
|
|
}
|
|
|
|
private void ResolveUseAction()
|
|
{
|
|
if (useActionComponent is IHandItemUseAction)
|
|
return;
|
|
|
|
foreach (MonoBehaviour component in GetComponents<MonoBehaviour>())
|
|
{
|
|
if (component is IHandItemUseAction)
|
|
{
|
|
useActionComponent = component;
|
|
return;
|
|
}
|
|
}
|
|
|
|
useActionComponent = null;
|
|
}
|
|
}
|