Browse Source

Initial commit

jamesperet 2 years ago
commit
888ac4ad5e

+ 71 - 0
.gitignore

@@ -0,0 +1,71 @@
+# This .gitignore file should be placed at the root of your Unity project directory
+#
+# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
+#
+/[Ll]ibrary/
+/[Tt]emp/
+/[Oo]bj/
+/[Bb]uild/
+/[Bb]uilds/
+/[Ll]ogs/
+/[Uu]ser[Ss]ettings/
+
+# MemoryCaptures can get excessive in size.
+# They also could contain extremely sensitive data
+/[Mm]emoryCaptures/
+
+# Asset meta data should only be ignored when the corresponding asset is also ignored
+!/[Aa]ssets/**/*.meta
+
+# Uncomment this line if you wish to ignore the asset store tools plugin
+# /[Aa]ssets/AssetStoreTools*
+
+# Autogenerated Jetbrains Rider plugin
+/[Aa]ssets/Plugins/Editor/JetBrains*
+
+# Visual Studio cache directory
+.vs/
+
+# Gradle cache directory
+.gradle/
+
+# Autogenerated VS/MD/Consulo solution and project files
+ExportedObj/
+.consulo/
+*.csproj
+*.unityproj
+*.sln
+*.suo
+*.tmp
+*.user
+*.userprefs
+*.pidb
+*.booproj
+*.svd
+*.pdb
+*.mdb
+*.opendb
+*.VC.db
+
+# Unity3D generated meta files
+*.pidb.meta
+*.pdb.meta
+*.mdb.meta
+
+# Unity3D generated file on crash reports
+sysinfo.txt
+
+# Builds
+*.apk
+*.aab
+*.unitypackage
+
+# Crashlytics generated file
+crashlytics-build.properties
+
+# Packed Addressables
+/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
+
+# Temporary auto-generated Android Assets
+/[Aa]ssets/[Ss]treamingAssets/aa.meta
+/[Aa]ssets/[Ss]treamingAssets/aa/*

+ 19 - 0
Readme.md

@@ -0,0 +1,19 @@
+# 📦 KairoEngine.Soundtrack.v0.1.1
+
+
+
+### 🛑Required packages
+
+### 📄Namespaces
+
+- `KairoEngine.SoundtrackSystem`
+
+### 📙Modules
+
+### 🔷Components
+
+### ✔Getting Started
+
+### 🧰Unity Functions
+
+### 🎈Back Log

+ 7 - 0
Readme.md.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 76c694e48e40e5d4bbb9a7776d0a35b7
+TextScriptImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 8 - 0
Runtime.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 574b53b0374d7a74ba258c07c65108d3
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 15 - 0
Runtime/KairoEngine.SoundtrackSystem.asmdef

@@ -0,0 +1,15 @@
+{
+    "name": "KairoEngine.Soundtrack",
+    "references": [
+        "GUID:7e5ae6a38d1532248b4c890eca668b06"
+    ],
+    "includePlatforms": [],
+    "excludePlatforms": [],
+    "allowUnsafeCode": false,
+    "overrideReferences": false,
+    "precompiledReferences": [],
+    "autoReferenced": true,
+    "defineConstraints": [],
+    "versionDefines": [],
+    "noEngineReferences": false
+}

+ 7 - 0
Runtime/KairoEngine.SoundtrackSystem.asmdef.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: 2c7b1da5b97d36c469d055b47ac410bc
+AssemblyDefinitionImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 19 - 0
Runtime/Playlist.cs

@@ -0,0 +1,19 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.SoundtrackSystem
+{
+
+    [CreateAssetMenu(fileName = "Playlist", menuName = "KairoEngine/Playlist", order = 3)]
+    public class Playlist : SerializedScriptableObject
+    {
+        public string title = "Untitled playlist";
+        public List<Soundtrack> soundtracks = new List<Soundtrack>();
+        public bool loop = true;
+
+    }
+}
+
+

+ 11 - 0
Runtime/Playlist.cs.meta

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

+ 27 - 0
Runtime/Soundtrack.cs

@@ -0,0 +1,27 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.SoundtrackSystem
+{
+    public enum SoundtrackType
+    {
+        Single,
+        Layered
+    }
+
+    [CreateAssetMenu(fileName = "Soundtrack", menuName = "KairoEngine/Soundtrack", order = 3)]
+    public class Soundtrack : SerializedScriptableObject
+    {
+        public SoundtrackType soundtrackType;
+        [ShowIf("@soundtrackType == SoundtrackType.Single")] public AudioClip audioClip;
+        [ShowIf("@soundtrackType == SoundtrackType.Layered")] public List<AudioClip> audioClips;
+
+        public bool loop = true;
+        public bool fadeIn = false;
+        [ShowIf("@fadeIn == true")] public float fadeTime = 1f;
+    }
+}
+
+

+ 11 - 0
Runtime/Soundtrack.cs.meta

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

+ 161 - 0
Runtime/SoundtrackManager.cs

@@ -0,0 +1,161 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+using UnityEngine.SceneManagement;
+using KairoEngine.Core;
+
+namespace KairoEngine.SoundtrackSystem
+{
+    public class SoundtrackManager : MonoBehaviour
+    {
+        #region Singleton
+        private static SoundtrackManager soundtrackManager;
+        public static SoundtrackManager instance
+        {
+            get {
+                if(!soundtrackManager)
+                {
+                    soundtrackManager = FindObjectOfType (typeof(SoundtrackManager)) as SoundtrackManager;
+                    if(!soundtrackManager)
+                    {
+                        //Debug.LogError("There need to one active SountrackManager script on the scene.");
+                        return null;
+                    }
+                }
+                return soundtrackManager;
+            }
+        }
+        #endregion
+
+        public AudioSource audioSource1;
+        public AudioSource audioSource2;
+        public Soundtrack currentTrack;
+        public Playlist playlist;
+        public int currentTrackIndex;
+        public bool playOnStart = true;
+        public bool showDebug = false;
+
+        private int currentAudioSourceIndex = 1;
+        private bool isPlaying = false;
+        private bool playlistEnded = false;
+
+        void Start()
+        {
+            if(playOnStart) 
+            {
+                if(playlist != null) StartPlaylist(playlist, currentTrackIndex);
+                else if(currentTrack != null) PlayTrack(currentTrack);
+            }
+        }
+
+        void FixedUpdate()
+        {
+            if(!isPlaying) return;
+            AudioSource currentAudioSource = GetAudioSource(currentAudioSourceIndex);
+            if (!currentAudioSource.isPlaying && !playlistEnded)
+            {
+                // Song endend in current audio source
+                
+                if(playlist == null) return;
+                currentTrackIndex += 1;
+                if(playlist.soundtracks.Count <= currentTrackIndex)
+                {
+                    // Reached the end of the playlist
+                    playlistEnded = true;
+                    if(showDebug) Debug.Log($"Reached the end of the playlist \'{playlist.title}\'");
+                }
+                else
+                {
+                    // Play next song
+                    PlayTrack(playlist.soundtracks[currentTrackIndex]);
+                }
+            }
+        }
+
+        public static void StartPlaylist(Playlist newPlaylist, int index = 0)
+        {
+            if(instance == null) return;
+            if(newPlaylist == null)
+            {
+                Debug.LogWarning("Missing playlist... music wont play");
+                return;
+            }
+            if(instance.showDebug) Debug.Log($"Starting playlist \'{newPlaylist.title}\'");
+            instance.playlist = newPlaylist;
+            instance.currentTrackIndex = index;
+            instance.playlistEnded = false;
+            PlayTrack(instance.playlist.soundtracks[instance.currentTrackIndex]);
+        }
+
+        public static void PlayTrack(Soundtrack soundtrack)
+        {
+            if(instance == null) return;
+            if(instance.showDebug) Debug.Log($"{(soundtrack.fadeIn ? "Fading in" : "Playing")} song \'{soundtrack.name}\'");
+            instance.isPlaying = true;
+            instance.currentTrack = soundtrack;
+            instance.SwitchAudioSource();
+            instance.Play(soundtrack, instance.currentAudioSourceIndex);
+            if(soundtrack.fadeIn) instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, soundtrack.fadeTime, 1f, false));
+            else instance.audioSource1.volume = 1f;
+        }
+
+        public static void FadeOutSoundtrack(float time = 1f)
+        {
+            if(instance == null) return;
+            if(instance.showDebug) Debug.Log($"Fading out song \'{instance.playlist.soundtracks[instance.currentTrackIndex].name}\'");
+            instance.StartCoroutine(SoundtrackManager.StartFade(instance.currentAudioSourceIndex, time, 0f, true));
+        }
+
+        public static IEnumerator StartFade(int audioSourceIndex, float duration, float targetVolume, bool stop = false)
+        {
+            if(instance == null) yield break;
+            
+            AudioSource audioSource = instance.GetAudioSource(audioSourceIndex);
+            if(stop == false) audioSource.Play();
+            float currentTime = 0;
+            float start = audioSource.volume;
+
+            while (currentTime < duration)
+            {
+                currentTime += Time.deltaTime;
+                audioSource.volume = Mathf.Lerp(start, targetVolume, currentTime / duration);
+                yield return null;
+            }
+            if(stop == true) 
+            {   
+                audioSource.Stop();
+                audioSource.clip = null;
+            }
+            if(instance.showDebug) Debug.Log($"Song \'{instance.currentTrack.name}\' ended");
+            yield break;
+        }
+
+        private void Play(Soundtrack soundtrack, int index)
+        {
+            if(index == 1)
+            {
+                audioSource1.clip = soundtrack.audioClip;
+                audioSource1.Play();
+            }
+            else if(index == 2)
+            {
+                audioSource2.clip = soundtrack.audioClip;
+                audioSource2.Play();
+            }
+        }
+
+        private AudioSource GetAudioSource(int index)
+        {
+            if(index == 1) return audioSource1;
+            else return audioSource2;
+        }
+
+        private void SwitchAudioSource()
+        {
+            if(currentAudioSourceIndex == 1) currentAudioSourceIndex = 2;
+            else if(currentAudioSourceIndex == 2) currentAudioSourceIndex = 1;
+        }
+    }
+}
+

+ 11 - 0
Runtime/SoundtrackManager.cs.meta

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

+ 35 - 0
Runtime/SoundtrackTrigger.cs

@@ -0,0 +1,35 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Sirenix.OdinInspector;
+
+namespace KairoEngine.SoundtrackSystem
+{
+    public class SoundtrackTrigger : MonoBehaviour
+    {
+        public Soundtrack soundtrack;
+        public bool playOnStart = true;
+        public bool endOnSceneChange = true;
+
+        [ShowIf("@hasLayers()")] public string playLayer = "";
+
+        private bool hasLayers()
+        {
+            if(soundtrack != null)
+            {
+                if(soundtrack.soundtrackType == SoundtrackType.Layered) return true;
+            }
+            return false;
+        }
+
+        void Start()
+        {
+            if(playOnStart)
+            {
+                // changed this....
+                SoundtrackManager.PlayTrack(soundtrack);
+            }
+        }
+    }
+}
+

+ 11 - 0
Runtime/SoundtrackTrigger.cs.meta

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

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+    "name": "at.kairoscope.kairoengine.soundtrack",
+    "displayName": "KairoEngine Soundtrack System",
+    "version": "0.1.1",
+    "unity": "2020.3",
+    "description": "Kairoengine soundtrack library. ",
+    "dependencies": {
+      "at.kairoscope.kairoengine.core":"0.1.6"
+    },
+    "repository": {
+      "type": "git",
+      "url": "https://git.kairoscope.net/kairoengine/soundtrack.git"
+    },
+    "author": "Kairoscope"
+  }

+ 7 - 0
package.json.meta

@@ -0,0 +1,7 @@
+fileFormatVersion: 2
+guid: bb6332c8a9866594d9693a9212066ab6
+TextScriptImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: