Explorar o código

Added SFX Game Module

James Peret %!s(int64=3) %!d(string=hai) anos
pai
achega
051739aabe

+ 0 - 0
Prefabs.meta → Resources.meta


+ 77 - 0
Resources/SoundControllerPrefab.prefab

@@ -0,0 +1,77 @@
+%YAML 1.1
+%TAG !u! tag:unity3d.com,2011:
+--- !u!1 &1492283199006220069
+GameObject:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  serializedVersion: 6
+  m_Component:
+  - component: {fileID: 1492283199006220070}
+  - component: {fileID: 1492283199006220071}
+  m_Layer: 0
+  m_Name: SoundControllerPrefab
+  m_TagString: Untagged
+  m_Icon: {fileID: 0}
+  m_NavMeshLayer: 0
+  m_StaticEditorFlags: 0
+  m_IsActive: 1
+--- !u!4 &1492283199006220070
+Transform:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1492283199006220069}
+  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
+  m_LocalPosition: {x: 0, y: 0, z: 0}
+  m_LocalScale: {x: 1, y: 1, z: 1}
+  m_Children: []
+  m_Father: {fileID: 0}
+  m_RootOrder: 0
+  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!114 &1492283199006220071
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1492283199006220069}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: e006f8fc9ad837c418b1c10b05fa03ee, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
+  serializationData:
+    SerializedFormat: 2
+    SerializedBytes: 
+    ReferencedUnityObjects: []
+    SerializedBytesString: 
+    Prefab: {fileID: 0}
+    PrefabModificationsReferencedUnityObjects: []
+    PrefabModifications: []
+    SerializationNodes:
+    - Name: soundLibrary
+      Entry: 7
+      Data: 0|System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[KairoEngine.SFX.SFXClip,
+        KairoEngine.SFX]], mscorlib
+    - Name: comparer
+      Entry: 7
+      Data: 1|System.Collections.Generic.GenericEqualityComparer`1[[System.String,
+        mscorlib]], mscorlib
+    - Name: 
+      Entry: 8
+      Data: 
+    - Name: 
+      Entry: 12
+      Data: 0
+    - Name: 
+      Entry: 13
+      Data: 
+    - Name: 
+      Entry: 8
+      Data: 
+  soundEmitterPrefab: {fileID: 8894808049857426241, guid: 11f3d36bb078e034aa99a08d5a801014, type: 3}
+  _soundEmitterPool: 
+  _soundsParent: {fileID: 1492283199006220069}

+ 7 - 0
Resources/SoundControllerPrefab.prefab.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 3ff60e39a6cd84c45bea6f1b18a332ff
+PrefabImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 0 - 0
Prefabs/SoundEmmiterPrefab.prefab → Resources/SoundEmmiterPrefab.prefab


+ 0 - 0
Prefabs/SoundEmmiterPrefab.prefab.meta → Resources/SoundEmmiterPrefab.prefab.meta


+ 13 - 0
Runtime/SFXLibrary.cs

@@ -0,0 +1,13 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.SFX
+{
+    [CreateAssetMenu(menuName = "KairoEngine/SFX/Library", fileName = "SFX Library", order = 3), HideMonoScript]
+    public class SFXLibrary : SerializedScriptableObject
+    {
+        public Dictionary<string, SFXClip> clips = new Dictionary<string, SFXClip>();
+    }
+}

+ 11 - 0
Runtime/SFXLibrary.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: b80ff80a95de97a4890f41e372d7ab32
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 77 - 0
Runtime/SFXModule.cs

@@ -0,0 +1,77 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using KairoEngine.Core;
+using KairoEngine.Core.ModuleSystem;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.SFX
+{
+    [Serializable, HideReferenceObjectPicker]
+    public class SFXModule : GameModuleBase
+    {
+        public override string name => "SFX Module";
+
+        [Tooltip("Use a custom sound emitter prefab or load the default prefab")]
+        [HorizontalGroup("@name/emitter", 0.015f), LabelText("Custom Sound Emitter")] public bool useCustomSoundEmitter;
+        [HorizontalGroup("@name/emitter", 0.985f), HideLabel, ShowIf("@useCustomSoundEmitter")] public GameObject soundEmitterPrefab;
+        [FoldoutGroup("@name"), Tooltip("Optional sound library asset")] public SFXLibrary soundLibrary;
+
+        public SFXModule(GameConfig config) : base(config)
+        {
+            this.gameConfig = config;
+            this.className = this.GetType().AssemblyQualifiedName;
+            this.typeName = "SFXModule";
+        }
+
+        public override void Load(Transform parent)
+        {
+            if(!useCustomSoundEmitter) soundEmitterPrefab = Resources.Load("SoundEmmiterPrefab") as GameObject;
+            GameObject soundControllerPrefab = Resources.Load("SoundControllerPrefab") as GameObject;
+            GameObject soundControllerObj = GameObject.Instantiate(soundControllerPrefab, parent);
+            SoundController soundController = soundControllerObj.GetComponent<SoundController>();
+            if(soundController == null)
+            {
+                Debug.LogError($"SoundControllerPrefab has no SoundController component", soundControllerObj);
+                return;
+            }
+            soundController.soundEmitterPrefab = soundEmitterPrefab;
+            if(soundLibrary != null) soundController.soundLibrary = soundLibrary;
+            soundControllerObj.name = "SoundController";
+        }
+
+        public override void Reset()
+        {
+            
+        }
+
+        public override void Destroy()
+        {
+            
+        }
+
+        public static SFXModule JSONToSFXModule(string data)
+        {
+            try
+            {
+                return JsonUtility.FromJson<SFXModule>(data);
+            }
+            catch (System.Exception e)
+            {
+                Debug.LogError($"Could not deserialize SFXModule: \n{e}");
+                return new SFXModule(null);
+            }
+        }
+
+        public override void OnBeforeSerialize(ObjectSerializer serializer) 
+        { 
+            if(soundEmitterPrefab != null) serializer.AddGameObject("SFXModule_soundEmitterPrefab", soundEmitterPrefab);
+        }
+
+        public override void OnBeforeDeserialize(ObjectSerializer serializer) 
+        { 
+            soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab");
+        }
+    }
+}

+ 11 - 0
Runtime/SFXModule.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 314111f6d04e195478cfdf02f5e82896
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 6 - 3
Runtime/SoundController.cs

@@ -12,6 +12,7 @@ namespace KairoEngine.SFX
     /// <summary>
     /// Controls playing SFX clips.
     /// </summary>
+    [HideMonoScript]
     public class SoundController : SerializedMonoBehaviour
     {
         
@@ -26,8 +27,8 @@ namespace KairoEngine.SFX
         [Tooltip("Container GameObject for the instantiated sound emitter prefabs.")]
         public  GameObject _soundsParent;
 
-        [Tooltip("A dictionary of sound SFX clips and their names")]
-        public Dictionary<string, SFXClip> soundLibrary = new Dictionary<string, SFXClip>();
+        [Tooltip("A dictionary of sound SFX clips and their names"), InlineEditor(InlineEditorObjectFieldModes.Boxed)]
+        public SFXLibrary soundLibrary;
 
         [ShowInInspector, Tooltip("A list of instantiated sound emitters. They are deleted when their audio stops playing.")]
         public static List<SoundEmitter> soundEmitters = new List<SoundEmitter>();
@@ -62,8 +63,10 @@ namespace KairoEngine.SFX
 
         public static void EmmitSound(string clipName, Vector3 position)
         {
+            if(instance == null) return;
+            if(instance.soundLibrary == null) return;
             SFXClip clip = null;
-            if (instance.soundLibrary.TryGetValue(clipName, out clip))
+            if (instance.soundLibrary.clips.TryGetValue(clipName, out clip))
             {
                 EmmitSound(clip, position);
             }