Hello
my game is very much dependent on sound, and I want to display a hint to turn up the volume if it’s below 50% at the beginning of the game. Is there a way to get the iPhone system volume?
It’s possible in ObjectiveC / Swift as pointed out here:
https://stackoverflow.com/questions/7255006/get-system-volume-ios
Anyone know whether this is somehow possible in Unity?
Thank you!
Dominik
AFAIK you would still need a small ObjectionableC code snippet to do it.
Here’s an example that reads the current screen brightness as a proxy for light sensor:
// GetNativeScreenBrightness.mm
//
// Cross platform notes:
//
// For iOS this file is included directly in the Unity project under a
// /Plugins/iOS folder, causing Unity to copy it into XCode for compilation.
//
// For other targets, you can make your own plugin; I just didn't do it yet. :)
#import <CoreGraphics/CoreGraphics.h>
extern "C" {
// references drawn from:
// https://stackoverflow.com/questions/6309643/reading-the-iphones-ambient-light-sensor
// technically this is not the light sensor; it's the screen brightness
// that responds to light sensor when auto-brightness is on. But it works!!
float GetNativeScreenBrightness(void)
{
return [[UIScreen mainScreen] brightness];
}
}
From C# side the interop call looks like:
// at the top:
using System.Runtime.InteropServices;
// somewhere within the class:
#if UNITY_IOS
[DllImport ("__Internal")]
private static extern float GetNativeScreenBrightness();
#endif
// and to call it:
void Update ()
{
float lightLevel = GetNativeScreenBrightness();
TextOutput.text = System.String.Format( "Screen brightness: {0:0.00}", lightLevel);
}
Why not just give them a notice no matter what at load time? I’ve seen several hidden object games that simply give you a notice at load time “This game best experienced with sound.” or something along those lines. After all, this way the user will hopefully not mute your game after hitting the title screen.
@Kurt-Dekker thank you, I will try that and report back whether it worked!
@Brathnann right I will probably do that, although I might also want to show that indication again briefly if the user switches off sound during the game.