BITKit/Packages/Runtime~/Unity/Common/Scripts/AudioSystem/NetworkAudioSource.cs

43 lines
1.0 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
#if MIRROR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
namespace BITKit
{
public class NetworkAudioSource : NetworkBehaviour
{
[Header(Constant.Header.Components)]
public AudioSource audioSource;
[Header(Constant.Header.InternalVariables)]
IGenericEvent<string> genericEvent;
public void Play(AudioSO so)
{
if (isOwned)
CmdPlay(so);
else if (isServer)
RpcPlay(so);
}
void Start()
{
TryGetComponent<IGenericEvent<string>>(out genericEvent);
}
[Command]
void CmdPlay(AudioSO so)
{
RpcPlay(so);
}
[ClientRpc]
void RpcPlay(AudioSO so)
{
audioSource.loop = so.loop;
audioSource.clip = so.Get();
audioSource.Play();
if (genericEvent is not null)
{
genericEvent.Invoke(so);
}
}
}
}
#endif