Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SDG.Unturned;
- using UnityEditor;
- using UnityEditor.Events;
- using UnityEngine;
- using UnityEngine.Events;
- public static class OneShotAudioEffectGenerator
- {
- [MenuItem("Assets/Create/Unturned/Effect from One Shot Audio Def")]
- public static void CreateEffectFromOneShotAudio()
- {
- var oneShotAudioDef = Selection.GetFiltered<OneShotAudioDefinition>(SelectionMode.Assets)[0];
- GameObject effect = new GameObject("Effect");
- effect.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
- effect.tag = "Debris";
- effect.layer = 12;
- var audioSource = effect.AddComponent<AudioSource>();
- audioSource.playOnAwake = false;
- audioSource.volume = oneShotAudioDef.volumeMultiplier;
- audioSource.spatialBlend = 1f;
- audioSource.rolloffMode = AudioRolloffMode.Linear;
- audioSource.maxDistance = 16f;
- var activation = effect.AddComponent<ActivationEventHook>();
- activation.OnEnabled = new UnityEvent();
- if (oneShotAudioDef.clips.Count == 1)
- {
- UnityEventTools.AddObjectPersistentListener(activation.OnEnabled, audioSource.PlayOneShot, oneShotAudioDef.clips[0]);
- return;
- }
- var binaryRandom = effect.AddComponent<BinaryRandomComponent>();
- UnityEventTools.AddVoidPersistentListener(activation.OnEnabled, binaryRandom.TriggerDefault);
- for (int i = 0; i < oneShotAudioDef.clips.Count - 1; i++)
- {
- binaryRandom.DefaultProbability = 1f / (float)(oneShotAudioDef.clips.Count - i);
- binaryRandom.OnTrue = new UnityEvent();
- UnityEventTools.AddObjectPersistentListener(binaryRandom.OnTrue, audioSource.PlayOneShot, oneShotAudioDef.clips[i]);
- if (i >= oneShotAudioDef.clips.Count - 2)
- {
- binaryRandom.OnFalse = new UnityEvent();
- UnityEventTools.AddObjectPersistentListener(binaryRandom.OnFalse, audioSource.PlayOneShot, oneShotAudioDef.clips[i + 1]);
- }
- else
- {
- var lastBinaryRandom = binaryRandom;
- binaryRandom = effect.AddComponent<BinaryRandomComponent>();
- lastBinaryRandom.OnFalse = new UnityEvent();
- UnityEventTools.AddVoidPersistentListener(lastBinaryRandom.OnFalse, binaryRandom.TriggerDefault);
- }
- }
- }
- [MenuItem("Assets/Create/Unturned/Effect from One Shot Audio Def", validate = true)]
- public static bool CreateEffectFromOneShotAudio_Validate()
- {
- var oneShotAudioDefs = Selection.GetFiltered<OneShotAudioDefinition>(SelectionMode.Assets);
- return oneShotAudioDefs.Length == 1 && oneShotAudioDefs[0].clips.Count > 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment