created audio manager and Sound class

This commit is contained in:
DerTyp187
2022-01-02 14:01:18 +01:00
parent 8c6d063e53
commit be50ec5eed
10 changed files with 189 additions and 6 deletions

View File

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

View File

@@ -0,0 +1,51 @@
using UnityEditor.Audio;
using System;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public Sound[] sounds;
public static AudioManager instance;
void Awake()
{
if(instance == null)
instance = this;
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach(Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
private void Start()
{
// Enter Music here
}
public void Play(string name)
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if(s == null)
{
Debug.LogWarning("Sound: " + s.name + " not found!");
return;
}
s.source.Play();
}
}

View File

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

View File

@@ -0,0 +1,21 @@
using UnityEngine.Audio;
using UnityEngine;
[System.Serializable]
public class Sound
{
public string name;
public AudioClip clip;
[Range(0f, 1f)]
public float volume;
[Range (.1f, 3f)]
public float pitch;
[HideInInspector]
public AudioSource source;
public bool loop;
}

View File

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

View File

@@ -67,6 +67,7 @@ public class HomeSceneBtnScript : MonoBehaviour, IPointerEnterHandler, IPointerE
public void OnPointerEnter(PointerEventData eventData)
{
isHovering = true;
FindObjectOfType<AudioManager>().Play("test");
}
public void OnPointerExit(PointerEventData eventData)