Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity Multiplayer A Free Script For Voice Chat in uNet

Discussion in 'Connected Games' started by Prommer-Games, Jan 26, 2018.

  1. Prommer-Games

    Prommer-Games

    Joined:
    Dec 8, 2015
    Posts:
    188
    Hello!
    I don't see many voice solutions out there for uNet that are readily available so here is a snippet of code I improved from this post
    Just attach to your player object!
    The Higher the frequency the more packets sent so be careful!


    Code (CSharp):
    1.  
    2. //Attach to player object
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using System.Collections;
    6.  
    7. public class Voice : NetworkBehaviour
    8. {
    9.  
    10.     int lastSample;
    11.     AudioClip c;
    12.     int FREQUENCY = 8000; //Default 44100
    13.     int length = 50;
    14.  
    15.     void Start()
    16.     {
    17.         if (isLocalPlayer)
    18.         {
    19.             microphoneIcon = GameObject.Find("MicrophoneHolder").GetComponent<HoldGameobject>().Holder01;
    20.             c = Microphone.Start(null, true, length, FREQUENCY);
    21.             while (Microphone.GetPosition(null) < 0) { }
    22.         }
    23.     }
    24.  
    25.     void FixedUpdate()
    26.     {
    27.         if (isLocalPlayer)
    28.         {
    29.             if (Input.GetKey(KeyCode.V))
    30.             {
    31.                 int pos = Microphone.GetPosition(null);
    32.                 int diff = pos - lastSample;
    33.                 if (diff > 0)
    34.                 {
    35.                     float[] samples = new float[diff * c.channels];
    36.                     c.GetData(samples, lastSample);
    37.                     byte[] ba = ToByteArray(samples);
    38.                  
    39.                     Cmd_SendRPC(ba, c.channels);
    40.                 }
    41.                 lastSample = pos;
    42.             }
    43.  
    44.         }
    45.     }
    46.  
    47.     [ClientRpc]
    48.     public void Rpc_Send(byte[] ba, int chan)
    49.     {
    50.         ReciveData(ba, chan);
    51.     }
    52.  
    53.     [Command]
    54.     public void Cmd_SendRPC(byte[] ba, int chan)
    55.     {
    56.         ReciveData(ba, chan);
    57.         Rpc_Send(ba, chan);
    58.     }
    59.  
    60.     void ReciveData(byte[] ba, int chan)
    61.     {
    62.         float[] f = ToFloatArray(ba);
    63.         GetComponent<AudioSource>().clip = AudioClip.Create("test", f.Length, chan, FREQUENCY, true, false);
    64.         GetComponent<AudioSource>().clip.SetData(f, 0);
    65.         if (!GetComponent<AudioSource>().isPlaying) GetComponent<AudioSource>().Play();
    66.     }
    67.  
    68.     public byte[] ToByteArray(float[] floatArray)
    69.     {
    70.         int len = floatArray.Length * 4;
    71.         byte[] byteArray = new byte[len];
    72.         int pos = 0;
    73.         foreach (float f in floatArray)
    74.         {
    75.             byte[] data = System.BitConverter.GetBytes(f);
    76.             System.Array.Copy(data, 0, byteArray, pos, 4);
    77.             pos += 4;
    78.         }
    79.         return byteArray;
    80.     }
    81.  
    82.     public float[] ToFloatArray(byte[] byteArray)
    83.     {
    84.         int len = byteArray.Length / 4;
    85.         float[] floatArray = new float[len];
    86.         for (int i = 0; i < byteArray.Length; i += 4)
    87.         {
    88.             floatArray[i / 4] = System.BitConverter.ToSingle(byteArray, i);
    89.         }
    90.         return floatArray;
    91.     }
    92. }
    93.  
    94.  
    Hope this helps someone!
     
    Last edited: Jan 27, 2018
  2. Prommer-Games

    Prommer-Games

    Joined:
    Dec 8, 2015
    Posts:
    188
    Improved and made so voice only active when pressing V
    27/01/18
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    2,602
    Let me express my gratitude by bumping this. Interesting approach I will add to my new set of networking tools and mechanics.
     
  4. abdiabdirisak6

    abdiabdirisak6

    Joined:
    Mar 13, 2018
    Posts:
    1

    Thank you for this amazing code, but i really hope you can help me out with a problem!

    When adding this code i dont know if my player already should have a audiosource or audiolistener.
    When testing multiplayer we cant hear eachother but when we add audisource and audiolisteners we can hear eachother but we also hear ourselves? I hear my own voice?

    If you could help me out it would be amazing, i really dont know how to solve this :(
     
  5. Prommer-Games

    Prommer-Games

    Joined:
    Dec 8, 2015
    Posts:
    188
    Hey!

    Yep the player needs an audio source and an audio listener.
    To stop hearing yourself you can make a unique int for a player and send it through the rpc and check if its yours if it is then dont play the sound else play it.
     
  6. OhMyWow

    OhMyWow

    Joined:
    Feb 27, 2017
    Posts:
    1
    Hey, thank you for this great code, but for me it keeps giving me this exception: UnityException: NetworkWriter used buffer is too big! This only goes away when I set the frequency to lower than 2000, which is way too low to be understandable. Do you have any idea why this would be happening?

    Full exception:
    UnityException: NetworkWriter used buffer is too big!
    UnityEngine.Networking.NetworkServer.SendWriterToReady (UnityEngine.GameObject contextObj, UnityEngine.Networking.NetworkWriter writer, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:452)
    UnityEngine.Networking.NetworkBehaviour.SendRPCInternal (UnityEngine.Networking.NetworkWriter writer, Int32 channelId, System.String rpcName) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkBehaviour.cs:101)
    Voice.CallRpc_Send (System.Byte[] ba, Int32 chan)
    Voice.Cmd_SendRPC (System.Byte[] ba, Int32 chan) (at Assets/Standard Assets/Voice.cs:55)
    Voice.CallCmd_SendRPC (System.Byte[] ba, Int32 chan)
    Voice.FixedUpdate () (at Assets/Standard Assets/Voice.cs:37)
     
  7. Prommer-Games

    Prommer-Games

    Joined:
    Dec 8, 2015
    Posts:
    188
    Try fideling with the length making it lower
     
  8. WolleW

    WolleW

    Joined:
    Mar 16, 2018
    Posts:
    2
    Hey, I don't realy get what this line is for:
    microphoneIcon = GameObject.Find("MicrophoneHolder").GetComponent<HoldGameobject>().Holder01;
     
  9. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,000
    Seems to be some left over Icon indication code from when he posted the script.
     
  10. Prommer-Games

    Prommer-Games

    Joined:
    Dec 8, 2015
    Posts:
    188
    @TwoTen @WolleW Yeah sorry about that just left over from some UI stuff I removed most of the other stuff
     
  11. kami_shadow

    kami_shadow

    Joined:
    Apr 18, 2015
    Posts:
    1
    Thanks a lot. Your code is very helpful, Though I have a little problem with the quality of voice. There is so much noise that I only hear some vague words and sounds. So do have any idea about how can I fix this?
     
  12. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    855
    get a better microphone.
     
  13. redouaneelalami0

    redouaneelalami0

    Joined:
    Apr 20, 2018
    Posts:
    3
    Hi guys Thanks for script
    I've tried this code and i can't hear the client + there is much noise
    I really appreciate your help