Gamingtoday093

Unturned OneShotAudioDef Effect Generator

Aug 10th, 2025 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using SDG.Unturned;
  2. using UnityEditor;
  3. using UnityEditor.Events;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6.  
  7. public static class OneShotAudioEffectGenerator
  8. {
  9.     [MenuItem("Assets/Create/Unturned/Effect from One Shot Audio Def")]
  10.     public static void CreateEffectFromOneShotAudio()
  11.     {
  12.         var oneShotAudioDef = Selection.GetFiltered<OneShotAudioDefinition>(SelectionMode.Assets)[0];
  13.  
  14.         GameObject effect = new GameObject("Effect");
  15.         effect.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
  16.         effect.tag = "Debris";
  17.         effect.layer = 12;
  18.  
  19.         var audioSource = effect.AddComponent<AudioSource>();
  20.         audioSource.playOnAwake = false;
  21.         audioSource.volume = oneShotAudioDef.volumeMultiplier;
  22.         audioSource.spatialBlend = 1f;
  23.         audioSource.rolloffMode = AudioRolloffMode.Linear;
  24.         audioSource.maxDistance = 16f;
  25.  
  26.         var activation = effect.AddComponent<ActivationEventHook>();
  27.         activation.OnEnabled = new UnityEvent();
  28.         if (oneShotAudioDef.clips.Count == 1)
  29.         {
  30.             UnityEventTools.AddObjectPersistentListener(activation.OnEnabled, audioSource.PlayOneShot, oneShotAudioDef.clips[0]);
  31.             return;
  32.         }
  33.        
  34.         var binaryRandom = effect.AddComponent<BinaryRandomComponent>();
  35.         UnityEventTools.AddVoidPersistentListener(activation.OnEnabled, binaryRandom.TriggerDefault);
  36.         for (int i = 0; i < oneShotAudioDef.clips.Count - 1; i++)
  37.         {
  38.             binaryRandom.DefaultProbability = 1f / (float)(oneShotAudioDef.clips.Count - i);
  39.             binaryRandom.OnTrue = new UnityEvent();
  40.             UnityEventTools.AddObjectPersistentListener(binaryRandom.OnTrue, audioSource.PlayOneShot, oneShotAudioDef.clips[i]);
  41.  
  42.             if (i >= oneShotAudioDef.clips.Count - 2)
  43.             {
  44.                 binaryRandom.OnFalse = new UnityEvent();
  45.                 UnityEventTools.AddObjectPersistentListener(binaryRandom.OnFalse, audioSource.PlayOneShot, oneShotAudioDef.clips[i + 1]);
  46.             }
  47.             else
  48.             {
  49.                 var lastBinaryRandom = binaryRandom;
  50.                 binaryRandom = effect.AddComponent<BinaryRandomComponent>();
  51.                 lastBinaryRandom.OnFalse = new UnityEvent();
  52.                 UnityEventTools.AddVoidPersistentListener(lastBinaryRandom.OnFalse, binaryRandom.TriggerDefault);
  53.             }
  54.         }
  55.     }
  56.  
  57.     [MenuItem("Assets/Create/Unturned/Effect from One Shot Audio Def", validate = true)]
  58.     public static bool CreateEffectFromOneShotAudio_Validate()
  59.     {
  60.         var oneShotAudioDefs = Selection.GetFiltered<OneShotAudioDefinition>(SelectionMode.Assets);
  61.         return oneShotAudioDefs.Length == 1 && oneShotAudioDefs[0].clips.Count > 0;
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment