在 unity中可以使用的直接设置音量大小的方法

参考网址

在unity中很多的音量控制dll都没有办法使用,调用"user.dll"话只是模拟电脑的快捷键,而且不能够直接设置对应的数值.

需要使用C++创建Dll
生成C++ dll的教程

打包好的dll

#include "stdafx.h"
#include 
#include 
#include 

#define DLLExport __declspec(dllexport)

extern "C"
{
    enum class VolumeUnit {
        Decibel,
        Scalar
    };

    //Gets volume
    DLLExport float GetSystemVolume(VolumeUnit vUnit) {
        HRESULT hr;

        // -------------------------
        CoInitialize(NULL);
        IMMDeviceEnumerator *deviceEnumerator = NULL;
        hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
        IMMDevice *defaultDevice = NULL;

        hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
        deviceEnumerator->Release();
        deviceEnumerator = NULL;

        IAudioEndpointVolume *endpointVolume = NULL;
        hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
        defaultDevice->Release();
        defaultDevice = NULL;

        float currentVolume = 0;
        if (vUnit == VolumeUnit::Decibel) {
            //Current volume in dB
            hr = endpointVolume->GetMasterVolumeLevel(¤tVolume);
        }

        else if (vUnit == VolumeUnit::Scalar) {
            //Current volume as a scalar
            hr = endpointVolume->GetMasterVolumeLevelScalar(¤tVolume);
        }
        endpointVolume->Release();
        CoUninitialize();

        return currentVolume;
    }

    //Sets volume
    DLLExport void SetSystemVolume(double newVolume, VolumeUnit vUnit) {
        HRESULT hr;

        // -------------------------
        CoInitialize(NULL);
        IMMDeviceEnumerator *deviceEnumerator = NULL;
        hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
        IMMDevice *defaultDevice = NULL;

        hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice);
        deviceEnumerator->Release();
        deviceEnumerator = NULL;

        IAudioEndpointVolume *endpointVolume = NULL;
        hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
        defaultDevice->Release();
        defaultDevice = NULL;

        if (vUnit == VolumeUnit::Decibel)
            hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);

        else    if (vUnit == VolumeUnit::Scalar)
            hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);

        endpointVolume->Release();

        CoUninitialize();
    }
}

C#控制脚本


using System.Runtime.InteropServices;
using UnityEngine;

public static class VolumeCtrl
{
    #region 引用C++音量控制库
    //获取音量的时候所使用的单位
    public enum VolumeUnit
    {
        //分贝
        Decibel,
        //大小
        Scalar
    }

    /// 
    /// 获取当前音量
    /// 
    /// 当前音量的单位
    [DllImport("ChangeVolumeWindows")]
    public static extern float GetSystemVolume(VolumeUnit vUnit);
    /// 
    ///设置当前音量
    /// 
    /// 设置的音量小
    /// 音量的单位
    [DllImport("ChangeVolumeWindows")]
    public static extern void SetSystemVolume(double newVolume, VolumeUnit vUnit);

    #endregion

    /// 
    /// 设置音量
    /// 
    /// 设置的音量大小
    /// 
    public static float SetVolume(int value)
    {
        float valueVolume = (float)value / 100;
        SetSystemVolume(valueVolume, VolumeUnit.Scalar);
        return valueVolume;
    }

    /// 
    /// 提升或下降音量
    /// 
    /// 音量下降的梯度大小
    /// 
    public static float ChangeVolume(int num = 2)
    {
        float nowVolume = GetSystemVolume(VolumeUnit.Scalar);
        nowVolume += (float)num / 100;
        SetSystemVolume(nowVolume, VolumeUnit.Scalar);
        return nowVolume;
    }


    static double _saveMuteVolume = 0;

    /// 
    /// 设置静音
    /// 
    /// 
    public static bool MuteVolume()
    {
        float nowVolume = GetSystemVolume(VolumeUnit.Scalar);
        if (nowVolume > 0)
        {
            _saveMuteVolume = nowVolume;
            Debug.Log(_saveMuteVolume);
            SetSystemVolume(0, VolumeUnit.Scalar);
            return true;
        }
        else
        {
            SetSystemVolume(_saveMuteVolume, VolumeUnit.Scalar);
            return false;
        }
    }
}

你可能感兴趣的:(unity,游戏引擎)