Browse Source

Fixed serialization problems

James Peret 2 years ago
parent
commit
8503a8f247
3 changed files with 18 additions and 10 deletions
  1. 9 4
      Runtime/GameActions/PlaySFXClipGameAction.cs
  2. 3 1
      Runtime/SFXModule.cs
  3. 6 5
      Runtime/SoundController.cs

+ 9 - 4
Runtime/GameActions/PlaySFXClipGameAction.cs

@@ -33,9 +33,13 @@ namespace KairoEngine.SFX.GameActions
         public override string GetTypeName() => "PlaySFXClipGameAction";
         public override string GetActionName() => "Play SFX Clip";
 
-        [FoldoutGroup("@name")]
+        [FoldoutGroup("@name"), NonSerialized, ShowInInspector, HideIf("@!string.IsNullOrEmpty(librarySfxClipName)")]
         public SFXClip sfxClip;
-        [FoldoutGroup("@name"), NonSerialized, ShowInInspector, ValueDropdown("possibleVariables", IsUniqueList = false)] public string parent;
+
+        [FoldoutGroup("@name"), HideIf("@sfxClip != null")]
+        public string librarySfxClipName = "";
+
+        [FoldoutGroup("@name"), ValueDropdown("possibleVariables", IsUniqueList = false)] public string parent;
         private IEnumerable possibleVariables = new ValueDropdownList<string>();
 
         public PlaySFXClipGameAction(GameActionsController controller) : base(controller)
@@ -55,7 +59,8 @@ namespace KairoEngine.SFX.GameActions
             GameObject parentObj = GetVariable<GameObject>(parent, null);
             Vector3 pos = new Vector3();
             if(parentObj != null) pos = parentObj.transform.position;
-            SoundController.EmmitSound(sfxClip, pos);
+            if(librarySfxClipName != null) SoundController.EmmitSound(librarySfxClipName, pos, parentObj.transform);
+            else if(sfxClip != null) SoundController.EmmitSound(sfxClip, pos, parentObj.transform);
             _done = true;
             _started = true;
         }
@@ -104,7 +109,7 @@ namespace KairoEngine.SFX.GameActions
         }
         public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth) 
         { 
-            
+            sfxClip = (SFXClip)serializer.DeserializeScriptableObject($"{depth}-{n}-Prefab");
         }
 
         private void GetCompatibleVariablenames()

+ 3 - 1
Runtime/SFXModule.cs

@@ -21,7 +21,7 @@ namespace KairoEngine.SFX
         public bool useObjectPooling = false;
         [HorizontalGroup("@name/pool", 0.015f), LabelText("Custom Object Pool"), ShowIf("@useObjectPooling")] public bool useCustomPool;
         [HorizontalGroup("@name/pool", 0.985f), HideLabel, ShowIf("@useCustomPool"), NonSerialized, ShowInInspector] public ObjectPool soundEmitterPool;
-        [FoldoutGroup("@name"), Tooltip("Optional sound library asset")] public SFXLibrary soundLibrary;
+        [FoldoutGroup("@name"), Tooltip("Optional sound library asset"), NonSerialized, ShowInInspector] public SFXLibrary soundLibrary;
         private SoundController soundController;
 
         public SFXModule(GameConfig config) : base(config)
@@ -110,12 +110,14 @@ namespace KairoEngine.SFX
         { 
             if(soundEmitterPrefab != null) serializer.AddGameObject("SFXModule_soundEmitterPrefab", soundEmitterPrefab);
             if(soundEmitterPool != null) serializer.AddObject("SFXModule_soundEmitterPool", soundEmitterPool);
+            if(soundLibrary != null) serializer.AddScriptableObject("SFXModule_SoundLibrary", soundLibrary);
         }
 
         public override void OnBeforeDeserialize(ObjectSerializer serializer) 
         { 
             soundEmitterPrefab = serializer.GetGameObject("SFXModule_soundEmitterPrefab");
             soundEmitterPool = (ObjectPool)serializer.GetObject("SFXModule_soundEmitterPool");
+            soundLibrary = (SFXLibrary)serializer.GetScriptableObject("SFXModule_SoundLibrary");
         }
     }
 }

+ 6 - 5
Runtime/SoundController.cs

@@ -61,14 +61,14 @@ namespace KairoEngine.SFX
             soundEmitters.Add(soundEmitter);
         }
 
-        public static void EmmitSound(string clipName, Vector3 position)
+        public static void EmmitSound(string clipName, Vector3 position, Transform parent = null)
         {
             if(instance == null) return;
             if(instance.soundLibrary == null) return;
             SFXClip clip = null;
             if (instance.soundLibrary.clips.TryGetValue(clipName, out clip))
             {
-                EmmitSound(clip, position);
+                EmmitSound(clip, position, parent);
             }
             else
             {
@@ -76,8 +76,9 @@ namespace KairoEngine.SFX
             }
         }
 
-        public static void EmmitSound(SFXClip clip, Vector3 position)
+        public static void EmmitSound(SFXClip clip, Vector3 position, Transform parent = null)
         {
+            if(parent == null) parent = soundsParent.transform;
             if(SoundController.instance == null)
             {
                 Debug.LogError("Missing SoundController component in scene.");
@@ -94,9 +95,9 @@ namespace KairoEngine.SFX
             else
             {
                 GameObject soundEmitterObj;
-                if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, soundsParent.transform);
+                if(instance.soundEmitterPrefab != null) soundEmitterObj = GameObject.Instantiate(instance.soundEmitterPrefab, parent);
                 else soundEmitterObj =  MasterObjectPooler.Instance.GetPool(soundEmitterPool).GetObject(position);
-                soundEmitterObj.transform.parent = soundsParent.transform;
+                soundEmitterObj.transform.parent = parent;
                 SoundEmitter soundEmitter = soundEmitterObj.GetComponent<SoundEmitter>();
                 soundEmitter.Initialize(clip, position);
                 soundEmitters.Add(soundEmitter);