can u write similar scriptt but for
radio like thing
i mean radio have int 0 - 1337 waves, for example there many players, and htey have radio
i wanna they have ability select some wave int audio and every was able only talk and hear via raadio audiosource only clients that have same wave int on radio
there can be few teams, every have its own wave
if someone get nubmer he can connect
i mean i need daio like taht thing
also u must use and make some modifications in that VoiceDataHandler
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
using UnityEngine;
using FishNet.Object;
using FishNet.Object.Synchronizing;
public class VoiceDataHandler : NetworkBehaviour
{
[SerializeField] private VoiceRecorder voiceRecorder;
[SerializeField] private VoiceStream voiceStream;
code
public override void OnStartClient()
{
if (voiceRecorder != null)
{
voiceRecorder.evtVoiceStream.AddListener(SendVoiceData);
}
else
{
Debug.LogError(“VoiceRecorder is not assigned.”);
}
if (voiceStream == null)
{
Debug.LogError(“VoiceStream is not assigned.”);
}
}
// Method to be called by VoiceRecorder when data is ready to be sent
public void SendVoiceData(byte[] data)
{
// if (IsOwner)
// {
SendVoiceDataToServer(data);
//}
}
[ServerRpc(RequireOwnership = false, Channel = FishNet.Transporting.Channel.Unreliable)]
public void SendVoiceDataToServer(byte[] data)
{
ReceiveVoiceDataOnClients(data);
}
[ObserversRpc(ExcludeOwner = true, Channel = FishNet.Transporting.Channel.Unreliable)]
public void ReceiveVoiceDataOnClients(byte[] data)
{
//if (!IsOwner && voiceStream != null)
// {
voiceStream.PlayVoiceData(data);
// }
}
}
Assistant
User
can u writee scripts to make voice hcatting using fishhnet netwrokinn?
i wanna have push keycode C
to start voice talking on localClient
make all features that require
Assistant
Sure, I'll modify the code while introducing the concept of a "radio wave" to enable communications on specific channels. Each player will have a radio wave integer, and they can only receive and transmit voice data to others on the same wave.
Here's the adjusted VoiceDataHandler class with the radio wave concept implemented:
csharp
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
using FishNet.Object;
using FishNet.Object.Synchronizing;
public class VoiceDataHandler : NetworkBehaviour
{
[SerializeField] private VoiceRecorder voiceRecorder;
[SerializeField] private VoiceStream voiceStream;
[SerializeField, SyncVar] private int radioWave = 0; // Variable to store the player's radio wave
public override void OnStartClient()
{
if (voiceRecorder != null)
{
voiceRecorder.evtVoiceStream.AddListener(SendVoiceData);
}
else
{
Debug.LogError("VoiceRecorder is not assigned.");
}
if (voiceStream == null)
{
Debug.LogError("VoiceStream is not assigned.");
}
}
// Method to be called by VoiceRecorder when data is ready to be sent
public void SendVoiceData(byte[] data)
{
SendVoiceDataToServer(data, radioWave);
}
[ServerRpc(RequireOwnership = false, Channel = FishNet.Transporting.Channel.Unreliable)]
public void SendVoiceDataToServer(byte[] data, int wave)
{
ReceiveVoiceDataOnClients(data, wave);
}
[ObserversRpc(ExcludeOwner = true, Channel = FishNet.Transporting.Channel.Unreliable)]
public void ReceiveVoiceDataOnClients(byte[] data, int wave)
{
if (radioWave == wave && voiceStream != null)
{
voiceStream.PlayVoiceData(data);
}
}
public void SetRadioWave(int newWave)
{
CmdSetRadioWave(newWave);
}
[ServerRpc]
private void CmdSetRadioWave(int newWave)
{
radioWave = newWave;
}
}
Explanation
Radio Wave Property: I've added a new synchronized variable radioWave that stores the player's current radio wave.
SendVoiceData: This method now sends the radio wave along with the voice data to the server.
Server and Client Communication:
SendVoiceDataToServer: This method now accepts the radio wave as a parameter and forwards it to clients.
ReceiveVoiceDataOnClients: This method checks if the recipient's radioWave matches the transmitted wave before playing the voice data.
SetRadioWave Method: A new method SetRadioWave is added to allow the player to change their radio wave. It calls a server-side method CmdSetRadioWave to update the synchronized radioWave variable.
Error Handling: The script ensures that both voiceRecorder and voiceStream are assigned and will log errors if they are not.
This setup ensures that only players tuned into the same radio wave can hear and talk to each other, mimicking the functionality of a team-based radio communication system. Adjust and further debug as necessary to fit the specifics of your project and ensure seamless in-game voice communication.
Sure, I'll walk you through the creation of a voice chatting feature using FishNet networking in Unity, including push-to-talk functionality. For simplicity, I'll split this into multiple scripts to keep things organized.
Prerequisites
Before diving in, ensure that your project includes:
FishNet Networking system installed.
Microphone access for recording voice data.
AudioSource for playing back received voice data.
1. VoiceRecorder Script
This script is responsible for capturing voice from the microphone.
csharp
using UnityEngine;
using UnityEngine.Events;
public class VoiceRecorder : MonoBehaviour
{
public UnityEvent<byte[]> evtVoiceStream = new UnityEvent<byte[]>();
private AudioClip audioClip;
private const int sampleRate = 44100;
private bool isRecording = false;
public void StartRecording()
{
if (Microphone.devices.Length > 0)
{
audioClip = Microphone.Start(null, true, 1, sampleRate);
isRecording = true;
StartCoroutine(SendVoiceData());
}
else
{
Debug.LogError("No microphone devices found.");
}
}
public void StopRecording()
{
if (isRecording)
{
Microphone.End(null);
isRecording = false;
}
}
private IEnumerator SendVoiceData()
{
while (isRecording)
{
yield return new WaitForSeconds(0.1f);
int pos = Microphone.GetPosition(null);
float[] samples = new float[audioClip.samples];
audioClip.GetData(samples, 0);
byte[] byteData = FloatArrayToByteArray(samples);
evtVoiceStream.Invoke(byteData);
}
}
private byte[] FloatArrayToByteArray(float[] floatArray)
{
byte[] byteArray = new byte[floatArray.Length * 4];
for (int i = 0; i < floatArray.Length; i++)
{
byte[] temp = System.BitConverter.GetBytes(floatArray[i]);
System.Buffer.BlockCopy(temp, 0, byteArray, i * 4, temp.Length);
}
return byteArray;
}
}
2. VoiceStream Script
This script is responsible for playing voice data received from the network.
csharp
using UnityEngine;
public class VoiceStream : MonoBehaviour
{
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
if (audioSource == null)
{
audioSource = gameObject.AddComponent<AudioSource>();
}
}
public void PlayVoiceData(byte[] data)
{
float[] floatArray = ByteArrayToFloatArray(data);
AudioClip audioClip = AudioClip.Create("VoiceClip", floatArray.Length, 1, 44100, false);
audioClip.SetData(floatArray, 0);
audioSource.PlayOneShot(audioClip);
}
private float[] ByteArrayToFloatArray(byte[] byteArray)
{
float[] floatArray = new float[byteArray.Length / 4];
for (int i = 0; i < floatArray.Length; i++)
{
floatArray[i] = System.BitConverter.ToSingle(byteArray, i * 4);
}
return floatArray;
}
}
3. VoiceDataHandler Script
This script handles sending and receiving voice data over the network.
csharp
using UnityEngine;
using FishNet.Object;
using FishNet.Object.Synchronizing;
public class VoiceDataHandler : NetworkBehaviour
{
[SerializeField] private VoiceRecorder voiceRecorder;
[SerializeField] private VoiceStream voiceStream;
public override void OnStartClient()
{
if (voiceRecorder != null)
{
voiceRecorder.evtVoiceStream.AddListener(SendVoiceData);
}
else
{
Debug.LogError("VoiceRecorder is not assigned.");
}
if (voiceStream == null)
{
Debug.LogError("VoiceStream is not assigned.");
}
}
public void SendVoiceData(byte[] data)
{
SendVoiceDataToServer(data);
}
[ServerRpc(RequireOwnership = false, Channel = FishNet.Transporting.Channel.Unreliable)]
public void SendVoiceDataToServer(byte[] data)
{
ReceiveVoiceDataOnClients(data);
}
[ObserversRpc(ExcludeOwner = true, Channel = FishNet.Transporting.Channel.Unreliable)]
public void ReceiveVoiceDataOnClients(byte[] data)
{
voiceStream.PlayVoiceData(data);
}
}
4. VoiceChatController Script
This script manages the push-to-talk functionality.
csharp
using UnityEngine;
using FishNet.Object;
public class VoiceChatController : NetworkBehaviour
{
[SerializeField] private KeyCode pushToTalkKey = KeyCode.C;
private VoiceDataHandler voiceDataHandler;
public override void OnStartClient()
{
voiceDataHandler = GetComponent<VoiceDataHandler>();
}
private void Update()
{
if (IsOwner)
{
if (Input.GetKeyDown(pushToTalkKey))
{
voiceDataHandler.voiceRecorder.StartRecording();
}
else if (Input.GetKeyUp(pushToTalkKey))
{
voiceDataHandler.voiceRecorder.StopRecording();
}
}
}
}
Integrating the Scripts
Create a GameObject in your scene for the voice chat system.
Attach the Scripts:
Add VoiceDataHandler, VoiceRecorder, VoiceStream, and VoiceChatController scripts.
Ensure the VoiceStream component has an AudioSource attached.
Link the VoiceRecorder and VoiceStream components to the VoiceDataHandler.
FishNet Configuration:
Ensure the FishNet Manager (NetworkManager) is correctly configured and active in your scene.
Microphone Permissions:
Ensure your Unity project has the necessary permissions to access the microphone.
Now, the player will be able to chat using voice communication by holding the C key. Players can only hear each other if they are connected to the same network instance and meet the conditions defined in the scripts. Adjust as needed to fit your specific game and networking requirements.