add ckg
plantilla base para movimiento básico
This commit is contained in:
438
Assets/Game Kit Controller/Scripts/Devices/Radio/radioSystem.cs
Normal file
438
Assets/Game Kit Controller/Scripts/Devices/Radio/radioSystem.cs
Normal file
@@ -0,0 +1,438 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using GameKitController.Audio;
|
||||
|
||||
public class radioSystem : MonoBehaviour
|
||||
{
|
||||
[Header ("Main Settings")]
|
||||
[Space]
|
||||
|
||||
public float radioVolume;
|
||||
|
||||
public bool playSongsOnStart;
|
||||
|
||||
public bool playSongsOnActive;
|
||||
|
||||
public bool playSongsRandomly;
|
||||
public bool repeatList;
|
||||
|
||||
public string mainManagerName = "Songs Manager";
|
||||
|
||||
[Space]
|
||||
[Header ("Debug")]
|
||||
[Space]
|
||||
|
||||
public bool playingSong;
|
||||
public bool songPaused;
|
||||
public float currentSongLength;
|
||||
|
||||
public bool radioActive;
|
||||
|
||||
public bool movingSongLenghtSlider;
|
||||
|
||||
public List<AudioClip> clips = new List<AudioClip> ();
|
||||
public List<AudioElement> audioElements = new List<AudioElement> ();
|
||||
|
||||
public int currentIndex = 0;
|
||||
|
||||
[Space]
|
||||
[Header ("Components")]
|
||||
[Space]
|
||||
|
||||
public Scrollbar volumeScrollbar;
|
||||
|
||||
public Text currentSongNameText;
|
||||
|
||||
public Slider songLenghtSlider;
|
||||
|
||||
public GameObject playButton;
|
||||
public GameObject pauseButton;
|
||||
|
||||
public GameObject songListContent;
|
||||
public GameObject songListElementParent;
|
||||
public GameObject songListElement;
|
||||
|
||||
public AudioSource source;
|
||||
|
||||
public songsManager mainSongsManager;
|
||||
|
||||
|
||||
bool songsLoaded;
|
||||
|
||||
bool radioCanBeUsed = true;
|
||||
|
||||
public AudioElement _currentSongAudioElement = new AudioElement ();
|
||||
|
||||
bool _currentSongAudioElementAssigned;
|
||||
|
||||
|
||||
private void InitializeAudioElements ()
|
||||
{
|
||||
if (source == null) {
|
||||
source = gameObject.AddComponent<AudioSource> ();
|
||||
}
|
||||
|
||||
if (clips != null && clips.Count > 0) {
|
||||
audioElements = new List<AudioElement> ();
|
||||
|
||||
foreach (var clip in clips) {
|
||||
audioElements.Add (new AudioElement { clip = clip });
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var audioElement in audioElements) {
|
||||
if (source != null) {
|
||||
audioElement.audioSource = source;
|
||||
}
|
||||
}
|
||||
|
||||
if (source != null) {
|
||||
_currentSongAudioElement.audioSource = source;
|
||||
|
||||
_currentSongAudioElementAssigned = _currentSongAudioElement.audioSource != null;
|
||||
}
|
||||
}
|
||||
|
||||
void Start ()
|
||||
{
|
||||
songListElement.SetActive (false);
|
||||
|
||||
InitializeAudioElements ();
|
||||
|
||||
volumeScrollbar.value = radioVolume;
|
||||
|
||||
bool mainSongsManagerLocated = mainSongsManager != null;
|
||||
|
||||
if (!mainSongsManagerLocated) {
|
||||
mainSongsManager = songsManager.Instance;
|
||||
|
||||
mainSongsManagerLocated = mainSongsManager != null;
|
||||
}
|
||||
|
||||
if (!mainSongsManagerLocated) {
|
||||
GKC_Utils.instantiateMainManagerOnSceneWithTypeOnApplicationPlaying (songsManager.getMainManagerName (), typeof(songsManager), true);
|
||||
|
||||
mainSongsManager = songsManager.Instance;
|
||||
|
||||
mainSongsManagerLocated = mainSongsManager != null;
|
||||
}
|
||||
|
||||
if (!mainSongsManagerLocated) {
|
||||
|
||||
mainSongsManager = FindObjectOfType<songsManager> ();
|
||||
|
||||
mainSongsManagerLocated = mainSongsManager != null;
|
||||
}
|
||||
|
||||
if (!mainSongsManagerLocated) {
|
||||
radioCanBeUsed = false;
|
||||
}
|
||||
|
||||
currentSongNameText.text = "...";
|
||||
}
|
||||
|
||||
void Update ()
|
||||
{
|
||||
if (!radioCanBeUsed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (radioActive) {
|
||||
if (playingSong) {
|
||||
if (!movingSongLenghtSlider) {
|
||||
songLenghtSlider.value = _currentSongAudioElement.audioSource.time / currentSongLength;
|
||||
}
|
||||
|
||||
if (!songPaused) {
|
||||
if ((_currentSongAudioElement.audioSource.time / currentSongLength) > 0.99f) {
|
||||
if (currentIndex == audioElements.Count - 1) {
|
||||
if (repeatList) {
|
||||
setNextSong ();
|
||||
} else {
|
||||
stopCurrentSong ();
|
||||
|
||||
currentIndex = 0;
|
||||
|
||||
setPlayPauseButtonState (true);
|
||||
}
|
||||
} else {
|
||||
if (playSongsRandomly) {
|
||||
setRandomSong ();
|
||||
} else {
|
||||
setNextSong ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!songsLoaded) {
|
||||
if (mainSongsManager.allSongsLoaded ()) {
|
||||
getSongsList ();
|
||||
|
||||
songsLoaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (playSongsOnStart) {
|
||||
if (songsLoaded) {
|
||||
setRadioActiveState (true);
|
||||
|
||||
playSongsOnStart = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void playCurrentSong ()
|
||||
{
|
||||
if (playingSong) {
|
||||
if (_currentSongAudioElementAssigned) {
|
||||
AudioPlayer.Play (_currentSongAudioElement, gameObject);
|
||||
|
||||
currentSongLength = _currentSongAudioElement.clip.length;
|
||||
|
||||
songLenghtSlider.value = _currentSongAudioElement.audioSource.time / currentSongLength;
|
||||
}
|
||||
} else {
|
||||
PlayCurrent ();
|
||||
}
|
||||
|
||||
songPaused = false;
|
||||
}
|
||||
|
||||
public void stopCurrentSong ()
|
||||
{
|
||||
if (_currentSongAudioElementAssigned) {
|
||||
AudioPlayer.Stop (_currentSongAudioElement, gameObject);
|
||||
}
|
||||
|
||||
playingSong = false;
|
||||
|
||||
songLenghtSlider.value = 0;
|
||||
}
|
||||
|
||||
public void pauseCurrentSong ()
|
||||
{
|
||||
songPaused = true;
|
||||
|
||||
if (_currentSongAudioElementAssigned) {
|
||||
AudioPlayer.Pause (_currentSongAudioElement, gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void setNextSong ()
|
||||
{
|
||||
if (audioElements.Count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (playSongsRandomly) {
|
||||
setRandomSongIndex ();
|
||||
} else {
|
||||
currentIndex = (currentIndex + 1) % audioElements.Count;
|
||||
}
|
||||
|
||||
setPlayPauseButtonState (false);
|
||||
|
||||
PlayCurrent ();
|
||||
}
|
||||
|
||||
public void setPreviousSong ()
|
||||
{
|
||||
if (audioElements.Count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (playSongsRandomly) {
|
||||
setRandomSongIndex ();
|
||||
} else {
|
||||
currentIndex--;
|
||||
if (currentIndex < 0) {
|
||||
currentIndex = audioElements.Count - 1;
|
||||
}
|
||||
}
|
||||
|
||||
setPlayPauseButtonState (false);
|
||||
|
||||
PlayCurrent ();
|
||||
}
|
||||
|
||||
public void setRandomSong ()
|
||||
{
|
||||
setRandomSongIndex ();
|
||||
|
||||
PlayCurrent ();
|
||||
}
|
||||
|
||||
public void setRandomSongIndex ()
|
||||
{
|
||||
int nextIndex = 0;
|
||||
|
||||
int loop = 0;
|
||||
|
||||
while (nextIndex == currentIndex) {
|
||||
nextIndex = (int)UnityEngine.Random.Range (0, audioElements.Count);
|
||||
|
||||
loop++;
|
||||
|
||||
if (loop > 100) {
|
||||
print ("loop error");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
currentIndex = nextIndex;
|
||||
}
|
||||
|
||||
public void setRadioVolume ()
|
||||
{
|
||||
radioVolume = volumeScrollbar.value;
|
||||
|
||||
if (_currentSongAudioElementAssigned) {
|
||||
_currentSongAudioElement.audioSource.volume = radioVolume;
|
||||
}
|
||||
}
|
||||
|
||||
void PlayCurrent ()
|
||||
{
|
||||
if (audioElements.Count <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_currentSongAudioElementAssigned) {
|
||||
_currentSongAudioElement = audioElements [currentIndex];
|
||||
|
||||
_currentSongAudioElement.audioSource = source;
|
||||
|
||||
_currentSongAudioElement.audioSource.time = 0;
|
||||
|
||||
AudioPlayer.Play (_currentSongAudioElement, gameObject);
|
||||
|
||||
string songName = audioElements [currentIndex].clip.name;
|
||||
|
||||
int extensionIndex = songName.IndexOf (".");
|
||||
|
||||
if (extensionIndex > -1) {
|
||||
songName = songName.Substring (0, extensionIndex);
|
||||
}
|
||||
|
||||
currentSongNameText.text = songName;
|
||||
|
||||
playingSong = true;
|
||||
|
||||
currentSongLength = _currentSongAudioElement.clip.length;
|
||||
|
||||
songLenghtSlider.value = _currentSongAudioElement.audioSource.time / currentSongLength;
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlaySongsRandomlyState (bool state)
|
||||
{
|
||||
playSongsRandomly = state;
|
||||
}
|
||||
|
||||
public void setRepeatListState (bool state)
|
||||
{
|
||||
repeatList = state;
|
||||
}
|
||||
|
||||
public void setMovingSongLenghtSliderState (bool state)
|
||||
{
|
||||
if (playingSong) {
|
||||
movingSongLenghtSlider = state;
|
||||
}
|
||||
}
|
||||
|
||||
public void setSongPart ()
|
||||
{
|
||||
if (playingSong) {
|
||||
if (_currentSongAudioElementAssigned) {
|
||||
_currentSongAudioElement.audioSource.time = _currentSongAudioElement.clip.length * songLenghtSlider.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setRadioActiveState (bool state)
|
||||
{
|
||||
radioActive = state;
|
||||
|
||||
if (radioActive) {
|
||||
if (playSongsOnActive) {
|
||||
if (playSongsRandomly) {
|
||||
setRandomSongIndex ();
|
||||
}
|
||||
|
||||
PlayCurrent ();
|
||||
|
||||
setPlayPauseButtonState (false);
|
||||
}
|
||||
} else {
|
||||
if (playingSong) {
|
||||
stopCurrentSong ();
|
||||
|
||||
setPlayPauseButtonState (true);
|
||||
}
|
||||
|
||||
songListContent.SetActive (false);
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnlyRadioActiveState (bool state)
|
||||
{
|
||||
radioActive = state;
|
||||
}
|
||||
|
||||
public void setPlayPauseButtonState (bool state)
|
||||
{
|
||||
playButton.SetActive (state);
|
||||
|
||||
pauseButton.SetActive (!state);
|
||||
}
|
||||
|
||||
public void selectSongOnList (GameObject songButtonPressed)
|
||||
{
|
||||
string songNameToCheck = songButtonPressed.GetComponentInChildren<Text> ().text;
|
||||
|
||||
for (int i = 0; i < audioElements.Count; i++) {
|
||||
if (audioElements [i].clip.name.Contains (songNameToCheck)) {
|
||||
currentIndex = i;
|
||||
|
||||
PlayCurrent ();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void getSongsList ()
|
||||
{
|
||||
//print (mainSongsManager.getSongsList ().Count);
|
||||
audioElements = mainSongsManager.getSongsList ();
|
||||
|
||||
for (int i = 0; i < audioElements.Count; i++) {
|
||||
string songName = audioElements [i].clip.name;
|
||||
|
||||
int extensionIndex = songName.IndexOf (".");
|
||||
|
||||
if (extensionIndex > -1) {
|
||||
songName = songName.Substring (0, extensionIndex);
|
||||
}
|
||||
|
||||
GameObject newSongListElement = (GameObject)Instantiate (songListElement, Vector3.zero, songListElement.transform.rotation);
|
||||
newSongListElement.SetActive (true);
|
||||
newSongListElement.transform.SetParent (songListElementParent.transform);
|
||||
newSongListElement.transform.localScale = Vector3.one;
|
||||
newSongListElement.transform.localPosition = Vector3.zero;
|
||||
newSongListElement.name = "Song List Element (" + songName + ")";
|
||||
|
||||
newSongListElement.GetComponentInChildren<Text> ().text = songName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e568e1cbf66712543b01a2006f479ff8
|
||||
timeCreated: 1530480668
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 40995
|
||||
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
|
||||
packageVersion: 3.77g
|
||||
assetPath: Assets/Game Kit Controller/Scripts/Devices/Radio/radioSystem.cs
|
||||
uploadId: 814740
|
||||
231
Assets/Game Kit Controller/Scripts/Devices/Radio/songsManager.cs
Normal file
231
Assets/Game Kit Controller/Scripts/Devices/Radio/songsManager.cs
Normal file
@@ -0,0 +1,231 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using GameKitController.Audio;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public class songsManager : MonoBehaviour
|
||||
{
|
||||
[Header ("Main Settings")]
|
||||
[Space]
|
||||
|
||||
public bool loadSoundsInFolderEnabled = true;
|
||||
|
||||
public string absolutePathBuild = ".";
|
||||
|
||||
public string absolutePathEditor = "Music";
|
||||
|
||||
public List<string> validExtensions = new List<string> { ".ogg", ".wav" };
|
||||
|
||||
[Space]
|
||||
[Header ("Internal Songs List")]
|
||||
[Space]
|
||||
|
||||
public bool useInternalSongsList;
|
||||
public List<AudioClip> internalClips = new List<AudioClip> ();
|
||||
public List<AudioElement> internalAudioElements = new List<AudioElement> ();
|
||||
|
||||
[Space]
|
||||
[Header ("Debug")]
|
||||
[Space]
|
||||
|
||||
public bool showDebugPrint;
|
||||
|
||||
public bool songsLoaded;
|
||||
|
||||
public List<AudioClip> clips = new List<AudioClip> ();
|
||||
public List<AudioElement> audioElements = new List<AudioElement> ();
|
||||
|
||||
[Space]
|
||||
[Header ("Components")]
|
||||
[Space]
|
||||
|
||||
public gameManager gameSystemManager;
|
||||
|
||||
string absolutePath;
|
||||
|
||||
int numberOfSongs;
|
||||
|
||||
|
||||
public const string mainManagerName = "Songs Manager";
|
||||
|
||||
public static string getMainManagerName ()
|
||||
{
|
||||
return mainManagerName;
|
||||
}
|
||||
|
||||
private static songsManager _songsManagerInstance;
|
||||
|
||||
public static songsManager Instance { get { return _songsManagerInstance; } }
|
||||
|
||||
bool instanceInitialized;
|
||||
|
||||
|
||||
public void getComponentInstance ()
|
||||
{
|
||||
if (instanceInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_songsManagerInstance != null && _songsManagerInstance != this) {
|
||||
Destroy (this.gameObject);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_songsManagerInstance = this;
|
||||
|
||||
instanceInitialized = true;
|
||||
}
|
||||
|
||||
void Awake ()
|
||||
{
|
||||
getComponentInstance ();
|
||||
}
|
||||
|
||||
|
||||
void Start ()
|
||||
{
|
||||
InitializeAudioElements ();
|
||||
|
||||
bool mainGameManagerLocated = gameSystemManager != null;
|
||||
|
||||
if (!mainGameManagerLocated) {
|
||||
gameSystemManager = gameManager.Instance;
|
||||
|
||||
mainGameManagerLocated = gameSystemManager != null;
|
||||
}
|
||||
|
||||
if (!mainGameManagerLocated) {
|
||||
gameSystemManager = FindObjectOfType<gameManager> ();
|
||||
|
||||
gameSystemManager.getComponentInstanceOnApplicationPlaying ();
|
||||
|
||||
mainGameManagerLocated = gameSystemManager != null;
|
||||
}
|
||||
|
||||
if (mainGameManagerLocated) {
|
||||
if (gameSystemManager.isLoadGameEnabled () || loadSoundsInFolderEnabled) {
|
||||
if (Application.isEditor) {
|
||||
absolutePath = absolutePathEditor;
|
||||
} else {
|
||||
absolutePath = absolutePathBuild;
|
||||
}
|
||||
|
||||
ReloadSounds ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeAudioElements ()
|
||||
{
|
||||
if (clips != null && clips.Count > 0) {
|
||||
audioElements = new List<AudioElement> ();
|
||||
|
||||
foreach (var clip in clips) {
|
||||
audioElements.Add (new AudioElement { clip = clip });
|
||||
}
|
||||
}
|
||||
|
||||
if (internalClips != null && internalClips.Count > 0) {
|
||||
internalAudioElements = new List<AudioElement> ();
|
||||
|
||||
foreach (var clip in internalClips) {
|
||||
internalAudioElements.Add (new AudioElement { clip = clip });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<AudioElement> getSongsList ()
|
||||
{
|
||||
return audioElements;
|
||||
}
|
||||
|
||||
void ReloadSounds ()
|
||||
{
|
||||
audioElements.Clear ();
|
||||
|
||||
if (Directory.Exists (absolutePath)) {
|
||||
//print ("directory found");
|
||||
absolutePath += "/";
|
||||
|
||||
// get all valid files
|
||||
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo (absolutePath);
|
||||
var fileInfo = info.GetFiles ()
|
||||
.Where (f => IsValidFileType (f.Name))
|
||||
.ToArray ();
|
||||
|
||||
numberOfSongs = fileInfo.Length;
|
||||
|
||||
if (useInternalSongsList) {
|
||||
numberOfSongs += internalAudioElements.Count;
|
||||
}
|
||||
|
||||
foreach (FileInfo s in fileInfo) {
|
||||
StartCoroutine (LoadFile (s.FullName));
|
||||
}
|
||||
} else {
|
||||
if (showDebugPrint) {
|
||||
print ("Directory with song files doesn't exist. If you want to use the radio system, place some .wav files on the folder " + absolutePathEditor + " inside the project folder.");
|
||||
}
|
||||
}
|
||||
|
||||
if (useInternalSongsList) {
|
||||
audioElements.AddRange (internalAudioElements);
|
||||
}
|
||||
}
|
||||
|
||||
bool IsValidFileType (string fileName)
|
||||
{
|
||||
return validExtensions.Contains (Path.GetExtension (fileName));
|
||||
// Alternatively, you could go fileName.SubString(fileName.LastIndexOf('.') + 1); that way you don't need the '.' when you add your extensions
|
||||
}
|
||||
|
||||
IEnumerator LoadFile (string path)
|
||||
{
|
||||
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip ("file://" + path, AudioType.WAV)) {
|
||||
yield return www.SendWebRequest ();
|
||||
|
||||
if (www.error != null) {
|
||||
print ("some issue happening when trying to load file " + path);
|
||||
} else {
|
||||
AudioClip clip = DownloadHandlerAudioClip.GetContent (www);
|
||||
|
||||
clip.name = Path.GetFileName (path);
|
||||
|
||||
audioElements.Add (new AudioElement { clip = clip });
|
||||
|
||||
if (audioElements.Count == numberOfSongs) {
|
||||
songsLoaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//WWW www = new WWW ("file://" + path);
|
||||
//// print ("loading " + path);
|
||||
|
||||
//AudioClip clip = www.GetAudioClip (false);
|
||||
|
||||
//while (clip.loadState != AudioDataLoadState.Loaded) {
|
||||
// yield return www;
|
||||
//}
|
||||
|
||||
////print ("done loading");
|
||||
//clip.name = Path.GetFileName (path);
|
||||
|
||||
//audioElements.Add (new AudioElement { clip = clip });
|
||||
|
||||
//if (audioElements.Count == numberOfSongs) {
|
||||
// songsLoaded = true;
|
||||
//}
|
||||
}
|
||||
|
||||
public bool allSongsLoaded ()
|
||||
{
|
||||
return songsLoaded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1344dbec6d5e14b4cb12f397ce539e24
|
||||
timeCreated: 1531189848
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 40995
|
||||
packageName: Game Kit Controller - Shooter Melee Adventure Creator 3D + 2.5D
|
||||
packageVersion: 3.77g
|
||||
assetPath: Assets/Game Kit Controller/Scripts/Devices/Radio/songsManager.cs
|
||||
uploadId: 814740
|
||||
Reference in New Issue
Block a user