daily pastebin goal
2%
SHARE
TWEET

Noisy.cs

mvaganov Feb 8th, 2018 (edited) 273 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // author: mvaganov@hotmail.com
  6. // license: Copyfree, public domain. This is free code! Great artists, steal this code!
  7. // latest version at: https://pastebin.com/hGU8et8s -- added: initial version (2018/02/08)
  8. public class Noisy : MonoBehaviour {
  9.     [System.Serializable]
  10.     public class NoisesOfKind {
  11.         public string name;
  12.         public AudioClip[] sounds;
  13.         [HideInInspector]
  14.         /// <summary>The last noise played, used to encourage variety in noises being played</summary>
  15.         public int lastNoisePlayed = -1;
  16.         public class Comparer : IComparer<NoisesOfKind> {
  17.             public int Compare(NoisesOfKind x, NoisesOfKind y) { return x.name.CompareTo(y.name); }
  18.         }
  19.         public static Comparer compare = new Comparer();
  20.     }
  21.     [Tooltip("kinds of noises to manage")]
  22.     public NoisesOfKind[] kindsOfNoises;
  23.  
  24.     private static NoisesOfKind searched = new NoisesOfKind();
  25.     public void PlayKindOfSound(string kind, Vector3 p, bool is3D) {
  26.         NoisesOfKind noisesOfKind = null;
  27.         searched.name = kind;
  28.         // assume kinds-of-noises list is sorted for faster search
  29.         int i = System.Array.BinarySearch (kindsOfNoises, searched, NoisesOfKind.compare);
  30.         if (i >= 0) {
  31.             noisesOfKind = kindsOfNoises [i];
  32.             PlaySoundFromArray (noisesOfKind.sounds, ref noisesOfKind.lastNoisePlayed, p, is3D);
  33.         }
  34.     }
  35.  
  36.     [Tooltip("the default background music")]
  37.     public AudioClip bgmusic;
  38.     public float bgmusicVolume = .1f;
  39.     private static AudioSource bgMusicPlayer = null;
  40.  
  41.     public void UseBackGroundMusic(AudioClip song) {
  42.         if (bgMusicPlayer != null && bgMusicPlayer.clip != song) {
  43.             bgMusicPlayer.Stop ();
  44.             Destroy (bgMusicPlayer.gameObject);
  45.             bgMusicPlayer = null;
  46.         }
  47.         if (song != null && (bgMusicPlayer == null || !bgMusicPlayer.isPlaying)) {
  48.             if (bgMusicPlayer != null) {
  49.                 Destroy (bgMusicPlayer.gameObject);
  50.             }
  51.             bgMusicPlayer = PlaySound (song, Vector3.zero, false, true, true);
  52.             bgMusicPlayer.volume = bgmusicVolume;
  53.         }
  54.     }
  55.  
  56.     void Start() {
  57.         // sort noises for faster access later
  58.         System.Array.Sort(kindsOfNoises, NoisesOfKind.compare);
  59.         if (bgMusicPlayer == null) {
  60.             UseBackGroundMusic (bgmusic);
  61.         }
  62.     }
  63.  
  64.     private static AudioSource s_asrc;
  65.     public static AudioSource PlaySound(AudioClip noise, Vector3 p = default(Vector3), bool is3D = false, bool independent = true, bool isLooped = false) {
  66.         AudioSource asrc = null;
  67.         if (!independent) {
  68.             asrc = s_asrc;
  69.         }
  70.         if (asrc == null) {
  71.             GameObject go = new GameObject ("<Noise: "+noise.name+">");
  72.             asrc = go.AddComponent<AudioSource> ();
  73.             if (!independent) {
  74.                 s_asrc = asrc;
  75.             }
  76.             asrc.transform.SetParent (Instance ().transform);
  77.         } else {
  78.             asrc.Stop ();
  79.         }
  80.         asrc.clip = noise;
  81.         asrc.spatialBlend = (!is3D) ? 0 : 1;
  82.         if (independent && !isLooped) {
  83.             Destroy (asrc.gameObject, noise.length);
  84.         }
  85.         asrc.loop = isLooped;
  86.         asrc.Play ();
  87.         return asrc;
  88.     }
  89.     public static int RandomNumberThatIsnt(int minInclusive, int maxExclusive, int andNot = -1) {
  90.         int index = minInclusive - 1;
  91.         do { index = Random.Range (minInclusive, maxExclusive); } while(index == andNot && (maxExclusive-minInclusive) > 1);
  92.         return index;
  93.     }
  94.     private void PlaySoundFromArray(AudioClip[] clipArray, ref int index, Vector3 p, bool is3D){
  95.         if (clipArray != null && clipArray.Length > 0) {
  96.             index = RandomNumberThatIsnt(0, clipArray.Length, index);
  97.             PlaySound (clipArray [index], p, is3D);
  98.         }
  99.     }
  100.  
  101.     private static Noisy instance;
  102.     public static Noisy Instance() {
  103.         if(instance == null) {
  104.             if((instance = FindObjectOfType(typeof(Noisy)) as Noisy) == null) {
  105.                 GameObject g = new GameObject("<" + typeof(Noisy).Name + ">");
  106.                 instance = g.AddComponent<Noisy>();
  107.             }
  108.         }
  109.         return instance;
  110.     }
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top