using UnityEngine;
namespace Wwise.Wooduan.Tools
{
#region ConsoleEnums
///
/// What kind of audio object the message is related to.
///
public enum AudioObjectType
{
SFX,
Music,
Voice,
Switch,
State,
RTPC,
Trigger,
SoundBank,
AudioPackage,
AuxBus,
Bus,
Emitter,
Listener,
Room,
Portal,
Geometry,
Occlusion,
Obstruction,
Language,
Component
}
///
/// How severe the audio message is.
///
public enum AudioLogVerbosity
{
Notification,
Warning,
Error,
None
}
///
/// In which way an audio event is triggered from.
///
public enum AudioTriggerSource
{
Code,
Config,
SoundCaster,
Initialization,
AnimationSound,
AkAmbient,
AkAudioListener,
AkBank,
AkRoom,
AkOutdoor,
AkRoomPortal,
AkSurfaceReflector,
ParticleSound,
AkState,
AkSwitch
}
public enum AudioAction
{
PostEvent,
StopEvent,
Seek,
Pause,
Resume,
Mute,
Unmute,
Load,
Unload,
Reload,
SetValue,
GetValue,
Activate,
Deactivate,
Cull,
Reveal
}
#if UNITY_EDITOR
public struct AudioConsoleLog
{
public AudioLogVerbosity AudioLogVerbosity;
public string Time;
public AudioObjectType ObjectType;
public AudioAction Action;
public AudioTriggerSource TriggerFrom;
public string ObjectName;
public GameObject GameObject;
public string GameObjectName;
public string Message;
}
#endif
#endregion
public static class AkWooduanHelper
{
///
/// Get or create a ScriptableObject asset. Save to path in editor mode.
///
public static T GetOrCreateAsset(string path) where T : ScriptableObject
{
#if UNITY_EDITOR
var directory = System.IO.Path.GetDirectoryName(path);
if (!System.IO.Directory.Exists(directory))
System.IO.Directory.CreateDirectory(directory);
var asset = UnityEditor.AssetDatabase.LoadAssetAtPath(path);
if (!asset)
{
asset = ScriptableObject.CreateInstance();
UnityEditor.AssetDatabase.CreateAsset(asset, path);
}
#else
var asset = ScriptableObject.CreateInstance();
Debug.LogWarning(typeof(T).Name + " config not found, creating an empty one instead");
#endif
return asset;
}
///
/// Get a component from game object. Add component if not found.
///
public static T GetOrAddComponent(GameObject gameObject) where T : Component
{
var component = gameObject.GetComponent();
if (component == null)
component = gameObject.AddComponent();
return component;
}
public static bool DeleteComponent(GameObject gameObject) where T : Component
{
var component = gameObject.GetComponent();
if (component)
{
Object.DestroyImmediate(component);
return true;
}
return false;
}
#region Console
public static AudioLogVerbosity DebugLogLevel = AudioLogVerbosity.Error;
#if UNITY_EDITOR
public static System.Action ConsoleCallback;
#endif
///
/// Output a message to AudioConsole in editor mode. Output to console in build.
///
internal static void DebugToConsole(AudioLogVerbosity verbosity, AudioObjectType objectType, AudioTriggerSource triggerFrom,
AudioAction action, string objectName, GameObject gameObject = null, string message = "")
{
#if UNITY_EDITOR
// if AudioConsole is opened, just send message to it
if (ConsoleCallback != null)
{
var newMessage = new AudioConsoleLog
{
AudioLogVerbosity = verbosity,
Time = Time.time.ToString("0.000"),
ObjectType = objectType,
Action = action,
TriggerFrom = triggerFrom,
ObjectName = objectName,
GameObject = gameObject,
GameObjectName = gameObject ? gameObject.name : "Global or Null",
Message = message
};
ConsoleCallback.Invoke(newMessage);
return;
}
#endif
#if DEBUG
// only output messages that are more severe than the level set
if (verbosity >= DebugLogLevel && Debug.unityLogger.logEnabled)
{
var log = string.Format("Audio{0}: {1}_{2}\tName: {3}\tTrigger: {4}\tGameObject: {5}\tMessage: {6}", verbosity, objectType, action, objectName, triggerFrom, gameObject ? gameObject.name : "Global or Null", message);
switch (verbosity)
{
case AudioLogVerbosity.Error:
Debug.LogError(log);
break;
case AudioLogVerbosity.Warning:
Debug.LogWarning(log);
break;
case AudioLogVerbosity.Notification:
Debug.Log(log);
break;
}
}
#endif
}
#endregion
}
}