Start implementation of diapason mechanics
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using Unheimlich.Audio;
|
||||
|
||||
namespace Unheimlich.Editor
|
||||
{
|
||||
public class AudioListenableBatchSetup : EditorWindow
|
||||
{
|
||||
private AudioEmitterType _selectedType = AudioEmitterType.Wall;
|
||||
private LayerMask _targetLayer;
|
||||
private bool _overwriteExisting = false;
|
||||
|
||||
[MenuItem("Tools/Unheimlich/Audio Listenable Batch Setup")]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
GetWindow<AudioListenableBatchSetup>("Audio Batch Setup");
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
GUILayout.Label("Batch Audio Listenable Setup", EditorStyles.boldLabel);
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
EditorGUILayout.HelpBox(
|
||||
"This tool adds AudioListenable components to all selected GameObjects in the scene.",
|
||||
MessageType.Info
|
||||
);
|
||||
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
_selectedType = (AudioEmitterType)EditorGUILayout.EnumPopup("Emitter Type", _selectedType);
|
||||
_overwriteExisting = EditorGUILayout.Toggle("Overwrite Existing", _overwriteExisting);
|
||||
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
if (GUILayout.Button("Add to Selected Objects", GUILayout.Height(30)))
|
||||
{
|
||||
AddToSelectedObjects();
|
||||
}
|
||||
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
EditorGUILayout.LabelField("Quick Presets", EditorStyles.boldLabel);
|
||||
|
||||
if (GUILayout.Button("Setup All Walls in Scene"))
|
||||
{
|
||||
SetupObjectsByName("wall", AudioEmitterType.Wall);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Setup All Doors in Scene"))
|
||||
{
|
||||
SetupObjectsByName("door", AudioEmitterType.Wood);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddToSelectedObjects()
|
||||
{
|
||||
GameObject[] selectedObjects = Selection.gameObjects;
|
||||
|
||||
if (selectedObjects.Length == 0)
|
||||
{
|
||||
EditorUtility.DisplayDialog("No Selection", "Please select at least one GameObject in the scene.", "OK");
|
||||
return;
|
||||
}
|
||||
|
||||
int addedCount = 0;
|
||||
int skippedCount = 0;
|
||||
|
||||
foreach (GameObject obj in selectedObjects)
|
||||
{
|
||||
AudioListenable existing = obj.GetComponent<AudioListenable>();
|
||||
|
||||
if (existing != null && !_overwriteExisting)
|
||||
{
|
||||
skippedCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
Undo.AddComponent<AudioListenable>(obj);
|
||||
existing = obj.GetComponent<AudioListenable>();
|
||||
}
|
||||
|
||||
ConfigureListenable(existing, _selectedType);
|
||||
addedCount++;
|
||||
}
|
||||
|
||||
EditorUtility.DisplayDialog(
|
||||
"Batch Setup Complete",
|
||||
$"Added/Updated: {addedCount}\nSkipped: {skippedCount}",
|
||||
"OK"
|
||||
);
|
||||
}
|
||||
|
||||
private void SetupObjectsByName(string nameContains, AudioEmitterType type)
|
||||
{
|
||||
GameObject[] allObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
|
||||
int count = 0;
|
||||
|
||||
foreach (GameObject obj in allObjects)
|
||||
{
|
||||
if (obj.name.ToLower().Contains(nameContains.ToLower()))
|
||||
{
|
||||
AudioListenable existing = obj.GetComponent<AudioListenable>();
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
Undo.AddComponent<AudioListenable>(obj);
|
||||
existing = obj.GetComponent<AudioListenable>();
|
||||
}
|
||||
|
||||
ConfigureListenable(existing, type);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
EditorUtility.DisplayDialog(
|
||||
"Setup Complete",
|
||||
$"Configured {count} objects containing '{nameContains}' as {type}",
|
||||
"OK"
|
||||
);
|
||||
}
|
||||
|
||||
private void ConfigureListenable(AudioListenable listenable, AudioEmitterType type)
|
||||
{
|
||||
SerializedObject so = new SerializedObject(listenable);
|
||||
|
||||
so.FindProperty("_emitterType").enumValueIndex = (int)type;
|
||||
so.FindProperty("_useProceduralAudio").boolValue = true;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case AudioEmitterType.Wall:
|
||||
so.FindProperty("_baseFrequency").floatValue = 200f;
|
||||
so.FindProperty("_frequencyVariation").floatValue = 20f;
|
||||
so.FindProperty("_volumeMin").floatValue = 0.2f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.4f;
|
||||
break;
|
||||
|
||||
case AudioEmitterType.Metal:
|
||||
so.FindProperty("_baseFrequency").floatValue = 800f;
|
||||
so.FindProperty("_frequencyVariation").floatValue = 100f;
|
||||
so.FindProperty("_volumeMin").floatValue = 0.4f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.7f;
|
||||
break;
|
||||
|
||||
case AudioEmitterType.Wood:
|
||||
so.FindProperty("_baseFrequency").floatValue = 300f;
|
||||
so.FindProperty("_frequencyVariation").floatValue = 50f;
|
||||
so.FindProperty("_volumeMin").floatValue = 0.3f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.6f;
|
||||
break;
|
||||
|
||||
case AudioEmitterType.Glass:
|
||||
so.FindProperty("_baseFrequency").floatValue = 1200f;
|
||||
so.FindProperty("_frequencyVariation").floatValue = 150f;
|
||||
so.FindProperty("_volumeMin").floatValue = 0.3f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.6f;
|
||||
break;
|
||||
|
||||
case AudioEmitterType.Stone:
|
||||
so.FindProperty("_baseFrequency").floatValue = 250f;
|
||||
so.FindProperty("_frequencyVariation").floatValue = 30f;
|
||||
so.FindProperty("_volumeMin").floatValue = 0.3f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.5f;
|
||||
break;
|
||||
|
||||
case AudioEmitterType.Mechanical:
|
||||
so.FindProperty("_baseFrequency").floatValue = 500f;
|
||||
so.FindProperty("_frequencyVariation").floatValue = 80f;
|
||||
so.FindProperty("_volumeMin").floatValue = 0.4f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.7f;
|
||||
break;
|
||||
}
|
||||
|
||||
so.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(listenable);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5d813501fc2b446419db87d892d79ce3
|
||||
@@ -0,0 +1,106 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using Unheimlich.Audio;
|
||||
|
||||
namespace Unheimlich.Editor
|
||||
{
|
||||
[CustomEditor(typeof(AudioListenable))]
|
||||
public class AudioListenableEditor : UnityEditor.Editor
|
||||
{
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
DrawDefaultInspector();
|
||||
|
||||
AudioListenable listenable = (AudioListenable)target;
|
||||
|
||||
EditorGUILayout.Space(10);
|
||||
EditorGUILayout.LabelField("Quick Setup", EditorStyles.boldLabel);
|
||||
|
||||
if (GUILayout.Button("Setup as Wall"))
|
||||
SetupAsType(listenable, AudioEmitterType.Wall, 200f, 20f, true);
|
||||
|
||||
if (GUILayout.Button("Setup as Metal Object"))
|
||||
SetupAsType(listenable, AudioEmitterType.Metal, 800f, 100f, false);
|
||||
|
||||
if (GUILayout.Button("Setup as Wood Object"))
|
||||
SetupAsType(listenable, AudioEmitterType.Wood, 300f, 50f, false);
|
||||
|
||||
if (GUILayout.Button("Setup as Glass"))
|
||||
SetupAsType(listenable, AudioEmitterType.Glass, 1200f, 150f, false);
|
||||
|
||||
if (GUILayout.Button("Setup as Mechanical"))
|
||||
SetupAsType(listenable, AudioEmitterType.Mechanical, 500f, 80f, false);
|
||||
|
||||
EditorGUILayout.Space(5);
|
||||
|
||||
if (GUILayout.Button("Test Sound (Play Mode Only)", GUILayout.Height(30)))
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
listenable.StartListening(Camera.main != null ? Camera.main.transform.position : Vector3.zero);
|
||||
}
|
||||
else
|
||||
{
|
||||
EditorGUILayout.HelpBox("Enter Play Mode to test sounds", MessageType.Info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupAsType(AudioListenable listenable, AudioEmitterType type, float baseFreq, float freqVar, bool isGeneric)
|
||||
{
|
||||
SerializedObject so = new SerializedObject(listenable);
|
||||
|
||||
so.FindProperty("_emitterType").enumValueIndex = (int)type;
|
||||
so.FindProperty("_useProceduralAudio").boolValue = true;
|
||||
so.FindProperty("_baseFrequency").floatValue = baseFreq;
|
||||
so.FindProperty("_frequencyVariation").floatValue = freqVar;
|
||||
|
||||
if (isGeneric)
|
||||
{
|
||||
so.FindProperty("_volumeMin").floatValue = 0.2f;
|
||||
so.FindProperty("_volumeMax").floatValue = 0.4f;
|
||||
}
|
||||
|
||||
so.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(listenable);
|
||||
}
|
||||
}
|
||||
|
||||
[CanEditMultipleObjects]
|
||||
[CustomEditor(typeof(Transform))]
|
||||
public class TransformEditorExtension : UnityEditor.Editor
|
||||
{
|
||||
private UnityEditor.Editor defaultEditor;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
defaultEditor = CreateEditor(targets, System.Type.GetType("UnityEditor.TransformInspector, UnityEditor"));
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (defaultEditor != null)
|
||||
DestroyImmediate(defaultEditor);
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
if (defaultEditor != null)
|
||||
defaultEditor.OnInspectorGUI();
|
||||
|
||||
EditorGUILayout.Space(10);
|
||||
|
||||
if (targets.Length == 1)
|
||||
{
|
||||
Transform t = (Transform)target;
|
||||
if (t.GetComponent<AudioListenable>() == null)
|
||||
{
|
||||
if (GUILayout.Button("Add Audio Listenable", GUILayout.Height(25)))
|
||||
{
|
||||
Undo.AddComponent<AudioListenable>(t.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b606ceac9567f9746b716e63b0e644ee
|
||||
Reference in New Issue
Block a user