Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to manually toggle STEAMAUDIO_DISABLED define #283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,4 @@ public static void BuildSteamAudio()
AssetDatabase.ExportPackage(assets, fileName, ExportPackageOptions.Recurse);
}
}

[InitializeOnLoad]
public static class Defines
{
// Define the constant STEAMAUDIO_ENABLED for all platforms that are supported by
// Steam Audio. User scripts should check if this constant is defined
// (using #if STEAMAUDIO_ENABLED) before using any of the Steam Audio C# classes.
static Defines()
{
#if UNITY_2021_2_OR_NEWER
NamedBuildTarget[] supportedPlatforms = {
NamedBuildTarget.Standalone,
NamedBuildTarget.Android,
};

foreach (var supportedPlatform in supportedPlatforms)
{
PlayerSettings.SetScriptingDefineSymbols(supportedPlatform, "STEAMAUDIO_ENABLED");
}
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace SteamAudio
* Custom editor GUI for SOFAFile assets.
*/
[CustomEditor(typeof(SOFAFile))]
public class SOFAFileEditor : Editor
public class SOFAFileEditor : SteamAudioEditor
{
public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("sofaName"));
EditorGUILayout.LabelField("Size", Common.HumanReadableDataSize(serializedObject.FindProperty("data").arraySize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace SteamAudio
{
[CustomEditor(typeof(SerializedData))]
public class SerializedDataInspector : Editor
public class SerializedDataInspector : SteamAudioEditor
{
SerializedProperty mData;

Expand All @@ -17,7 +17,7 @@ private void OnEnable()
mData = serializedObject.FindProperty("data");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
serializedObject.Update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SteamAudio
{
[CustomEditor(typeof(SteamAudioAmbisonicSource))]
[CanEditMultipleObjects]
public class SteamAudioAmbisonicSourceInspector : Editor
public class SteamAudioAmbisonicSourceInspector : SteamAudioEditor
{
SerializedProperty mApplyHRTF;

Expand All @@ -18,7 +18,7 @@ private void OnEnable()
mApplyHRTF = serializedObject.FindProperty("applyHRTF");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
if (SteamAudioSettings.Singleton.audioEngine != AudioEngineType.Unity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
namespace SteamAudio
{
[CustomEditor(typeof(SteamAudioBakedListener))]
public class SteamAudioBakedListenerInspector : Editor
public class SteamAudioBakedListenerInspector : SteamAudioEditor
{
#if STEAMAUDIO_ENABLED
SerializedProperty mInfluenceRadius;
SerializedProperty mUseAllProbeBatches;
SerializedProperty mProbeBatches;
Expand All @@ -26,7 +25,7 @@ private void OnEnable()
mProbeBatches = serializedObject.FindProperty("probeBatches");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
serializedObject.Update();

Expand Down Expand Up @@ -76,11 +75,5 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}
#else
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("Steam Audio is not supported for the target platform or STEAMAUDIO_ENABLED define symbol is missing.", MessageType.Warning);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
namespace SteamAudio
{
[CustomEditor(typeof(SteamAudioBakedSource))]
public class SteamAudioBakedSourceInspector : Editor
public class SteamAudioBakedSourceInspector : SteamAudioEditor
{
#if STEAMAUDIO_ENABLED
SerializedProperty mInfluenceRadius;
SerializedProperty mUseAllProbeBatches;
SerializedProperty mProbeBatches;
Expand All @@ -26,7 +25,7 @@ private void OnEnable()
mProbeBatches = serializedObject.FindProperty("probeBatches");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
serializedObject.Update();

Expand Down Expand Up @@ -76,11 +75,5 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}
#else
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("Steam Audio is not supported for the target platform or STEAMAUDIO_ENABLED define symbol is missing.", MessageType.Warning);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
namespace SteamAudio
{
[CustomEditor(typeof(SteamAudioDynamicObject))]
public class SteamAudioDynamicObjectInspector : Editor
public class SteamAudioDynamicObjectInspector : SteamAudioEditor
{
#if STEAMAUDIO_ENABLED
SerializedProperty mAsset;

private void OnEnable()
{
mAsset = serializedObject.FindProperty("asset");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
serializedObject.Update();

Expand Down Expand Up @@ -54,11 +53,5 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}
#else
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("Steam Audio is not supported for the target platform or STEAMAUDIO_ENABLED define symbol is missing.", MessageType.Warning);
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//
// Copyright 2017 Valve Corporation. All rights reserved. Subject to the following license:
// https://valvesoftware.github.io/steam-audio/license.html
//

using System.Linq;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;

namespace SteamAudio
{
public abstract class SteamAudioEditor : Editor
{
private static class Styles
{
public static readonly GUIContent sDisableButton = new GUIContent("Disable Steam Audio", $"Click this button to automatically add the STEAMAUDIO_DISABLED define symbol for this target");
public static readonly GUIContent sEnableButton = new GUIContent("Enable Steam Audio", "Click this button to automatically remove the STEAMAUDIO_DISABLED define symbol for this target");
}

static bool? sNativePluginAvailableForTarget = null;
public static bool NativePluginAvailableForTarget
{
get
{
if (sNativePluginAvailableForTarget != null)
return sNativePluginAvailableForTarget.Value;

sNativePluginAvailableForTarget = AssetDatabase.FindAssets("phonon t:DefaultAsset")
.Select(guid => AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(guid)) as PluginImporter)
.Where(plugin => plugin != null)
.Any(IsNativePluginCompatibleWithActiveTarget);

return sNativePluginAvailableForTarget.Value;
}
}

private static bool IsNativePluginCompatibleWithActiveTarget(PluginImporter plugin)
{
// A standalone plugin will be considered as compatible to all standalone targets.
// Since Steam Audio provides binary for all standalone platform, telling them apart is not a concern.
// https://github.com/Unity-Technologies/UnityCsReference/blob/2021.3/Modules/AssetPipelineEditor/ImportSettings/PluginImporterInspector.cs#L28

if (!plugin.isNativePlugin) return false;
if (!plugin.GetCompatibleWithPlatform(EditorUserBuildSettings.activeBuildTarget)) return false;
return true;
}

#if UNITY_2021_2_OR_NEWER
public static NamedBuildTarget GetActiveNamedBuildTarget()
{
#if UNITY_SERVER
return NamedBuildTarget.Server;
#endif
var targetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
return NamedBuildTarget.FromBuildTargetGroup(targetGroup);
}

public static void AddDefineSymbolsToTarget(string define)
{
var target = GetActiveNamedBuildTarget();
var defines = PlayerSettings.GetScriptingDefineSymbols(target);
var defineList = defines.Split(';').ToList();
if (defineList.IndexOf(define) < 0)
{
PlayerSettings.SetScriptingDefineSymbols(target, defines + ";" + define);
}
}

public static void RemoveDefineSymbolsToTarget(string define)
{
var target = GetActiveNamedBuildTarget();
var defines = PlayerSettings.GetScriptingDefineSymbols(target).Split(';').ToList();
defines.Remove(define);
PlayerSettings.SetScriptingDefineSymbols(target, string.Join(';', defines));
}
#else
public static void AddDefineSymbolsToTarget(string define)
{
var target = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(target).Split(';').ToList();
if (defines.IndexOf(define) < 0)
{
PlayerSettings.SetScriptingDefineSymbolsForGroup(target, defines + ";" + define);
}
}

public static void RemoveDefineSymbolsToTarget(string define)
{
var target = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);
var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(target).Split(';').ToList();
defines.Remove(define);
PlayerSettings.SetScriptingDefineSymbolsForGroup(target, string.Join(';', defines));
}
#endif

protected virtual bool AlwaysShowSteamAudioToggle { get; set; } = false;

public override void OnInspectorGUI()
{
if (EditorApplication.isCompiling) return;

#if STEAMAUDIO_DISABLED
if (NativePluginAvailableForTarget)
{
EditorGUILayout.HelpBox("Steam Audio is disabled for {EditorUserBuildSettings.activeBuildTarget}.\nSteam Audio native plugin found for {EditorUserBuildSettings.activeBuildTarget}.\nYou can enable Steam Audio by removing the STEAMAUDIO_DISABLED define symbol.", MessageType.Info);
if (GUILayout.Button(Styles.sEnableButton))
{
RemoveDefineSymbolsToTarget("STEAMAUDIO_DISABLED");
}
}
else
{
EditorGUILayout.HelpBox("Steam Audio is disabled for {EditorUserBuildSettings.activeBuildTarget}.", AlwaysShowSteamAudioToggle ? MessageType.Info : MessageType.None);
if (AlwaysShowSteamAudioToggle && GUILayout.Button(Styles.sEnableButton))
{
RemoveDefineSymbolsToTarget("STEAMAUDIO_DISABLED");
}
}
#else
if (!NativePluginAvailableForTarget)
{
EditorGUILayout.HelpBox($"Steam Audio native plugin is not available for {EditorUserBuildSettings.activeBuildTarget}.\nYou can disable Steam Audio by adding a STEAMAUDIO_DISABLED define symbol to prevent Steam Audio from loading on this platform.", MessageType.Warning);
if (GUILayout.Button(Styles.sDisableButton))
{
AddDefineSymbolsToTarget("STEAMAUDIO_DISABLED");
}
}
else if (AlwaysShowSteamAudioToggle)
{
// In case user want to disable Steam Audio on supported platform,
// this disable button will only show up in Steam Audio Settings.
EditorGUILayout.HelpBox($"Steam Audio is enabled for {EditorUserBuildSettings.activeBuildTarget}.", MessageType.Info);
if (GUILayout.Button(Styles.sDisableButton))
{
AddDefineSymbolsToTarget("STEAMAUDIO_DISABLED");
}
}
#endif
OnSteamAudioGUI();
}

protected virtual void OnSteamAudioGUI() {}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ namespace SteamAudio
{
[CustomEditor(typeof(SteamAudioGeometry))]
[CanEditMultipleObjects]
public class SteamAudioGeometryInspector : Editor
public class SteamAudioGeometryInspector : SteamAudioEditor
{
#if STEAMAUDIO_ENABLED
SerializedProperty mMaterial;
SerializedProperty mExportAllChildren;
SerializedProperty mTerrainSimplificationLevel;
Expand All @@ -24,7 +23,7 @@ private void OnEnable()
mTerrainSimplificationLevel = serializedObject.FindProperty("terrainSimplificationLevel");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
serializedObject.Update();

Expand All @@ -49,11 +48,5 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}
#else
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("Steam Audio is not supported for the target platform or STEAMAUDIO_ENABLED define symbol is missing.", MessageType.Warning);
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
namespace SteamAudio
{
[CustomEditor(typeof(SteamAudioListener))]
public class SteamAudioListenerInspector : Editor
public class SteamAudioListenerInspector : SteamAudioEditor
{
#if STEAMAUDIO_ENABLED
SerializedProperty mCurrentBakedListener;
SerializedProperty mApplyReverb;
SerializedProperty mReverbType;
Expand All @@ -30,7 +29,7 @@ private void OnEnable()
mProbeBatches = serializedObject.FindProperty("probeBatches");
}

public override void OnInspectorGUI()
protected override void OnSteamAudioGUI()
{
serializedObject.Update();

Expand Down Expand Up @@ -87,11 +86,5 @@ public override void OnInspectorGUI()

serializedObject.ApplyModifiedProperties();
}
#else
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("Steam Audio is not supported for the target platform or STEAMAUDIO_ENABLED define symbol is missing.", MessageType.Warning);
}
#endif
}
}
Loading