EasyVivoxUtilities.cs

EasyVivoxUtilities contains Helper/Utility methods that don't require dependencies

All libraries this script depends on

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
#if UNITY_ANDROID
 using UnityEngine.Android;
#elif UNITY_IOS
 using UnityEngine.iOS;
#endif

The code below is called a preprocessor directive and is only valid if the build is for Android platform or else the compiler will ignore this code. Read more about it here

#if PLATFORM_ANDROID
using UnityEngine.Android;
#elif UNITY_IOS
 using UnityEngine.iOS;
#endif

Android

RequestAndroidMicPermission() is used to request microphone access if the destination build is Android

        public static void RequestAndroidMicPermission()
        {
#if PLATFORM_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
        {
            Permission.RequestUserPermission(Permission.Microphone);
            
        }
#endif
        }

iOS

RequestIOSMicrophoneAccess() is used to request Microphone access for iOS.

    public static void RequestIOSMicrophoneAccess()
    {
        // todo update and research docs / have someone without IOS test it
        // Refer to Vivox Documentation on how to implement this method. Currently a work in progress.NOT SURE IF IT WORKS
        // make sure you change the info.plist refer to Vivox documentation for this to work
        // Make sure NSCameraUsageDescription and NSMicrophoneUsageDescription
        // are in the Info.plist.
        #if UNITY_IOS 
        Application.RequestUserAuthorization(UserAuthorization.Microphone); 
        #endif 
    }

In order to access Microphone on iOS you need to edit Info.plist. Unity provides easy access in the Player Settings. Read more about it here

Filter User/Channel Names

FilterChannelAndUserName is used to filter out unsupported characters in Usernames and Channel names when using Vivox. This method is used under the hood in EasyCode automatically, but you can call this method yourself

This does not filter out bad words or racial slurs, recommended to implement your own filter or use a 3rd party asset/service such as Language and Profanity Filter (Not Sponsored) or implement your own using this list of bad words courtesy of ShutterStock (Not Sponsored)

This method is automatically implemented for you in the Login() and JoinChannel() Methods


    public static bool FilterChannelAndUserName(string nameToFilter)
    {
        char[] allowedChars = new char[] { '0','1','2','3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '!', '(', ')', '+','-', '.', '=', '_', '~'};

        List<char> allowed = new List<char>(allowedChars);
        foreach (char c in nameToFilter)
        {
            if (!allowed.Contains(c))
            {
                if (c == ' ')
                {
                    Debug.Log($"Can't join channel, Channel name has space in it '{c}'");
                }
                else
                {
                    Debug.Log($"Can't join channel, Channel name has invalid character '{c}'");
                }
                return false;
            }
        }
        return true;
    }

This code can probably be optimized for slightly better performance, but it will only be called a few times throughout the lifetime of your game / app. Or you can implement Regex (there is a performance hit with Regex as well, but you don't have to type every character you're trying to filter for. Weigh the costs ⚖️)

Last updated