Acessar funçoes de outro script?
3 participantes
Página 1 de 1
Acessar funçoes de outro script?
Bom dia amigos estou com um problrminha aqui para chamar uma funçao de outro script
Esse script registra todos audios que voce adicionar a ele.
E estou usando outro script para chamar a musica que vai tocar com esses 2 comandos
O erro e esse quando chamo o comando
Esse script registra todos audios que voce adicionar a ele.
E estou usando outro script para chamar a musica que vai tocar com esses 2 comandos
- Código:
AudioProvider.Instance.PlayAudioShotInPlayer("Musica1");
GetComponent<AudioProvider> ().PlayAudioShotInPlayer("Musica1");
- Código:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[RequireComponent(typeof(AudioSource))]
public class AudioProvider : MonoBehaviour
{
public AudioSource playerAudioSource;
public AudioClip[] audioClips;
public static AudioProvider Instance { get; private set; }
public float Volume
{
get{ return GetComponent<AudioSource>().volume;}
set{ GetComponent<AudioSource>().volume = value;}
}
public bool Loop
{
get{ return GetComponent<AudioSource>().loop;}
set{ GetComponent<AudioSource>().loop = value;}
}
private Dictionary<string, AudioClip> registeredAudioClips;
void Awake()
{
Instance = this;
RegisteAudioClips();
}
void RegisteAudioClips()
{
registeredAudioClips = new Dictionary<string, AudioClip>();
for (int i = 0; i < audioClips.Length; i++)
{
registeredAudioClips.Add(audioClips [i].name, audioClips [i]);
}
}
public AudioClip GetRegisteredAudioClip(string audioClipName)
{
if (registeredAudioClips.ContainsKey(audioClipName))
return registeredAudioClips [audioClipName];
throw new UnityException("No audio clip registered");
}
public void PlayAudioShot(string audioClipName, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName))
GetComponent<AudioSource>().PlayOneShot(registeredAudioClips [audioClipName], volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayAudioShot(AudioSource source, string audioClipName, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName))
source.PlayOneShot(registeredAudioClips [audioClipName], volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayAudioShotInPlayer(string audioClipName, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName) &&
!playerAudioSource.isPlaying)
playerAudioSource.PlayOneShot(registeredAudioClips [audioClipName], volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayAudioShotInPosition(string audioClipName, Vector3 position, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName))
AudioSource.PlayClipAtPoint(registeredAudioClips [audioClipName], position, volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayBackgroundMusic(bool loop = true)
{
if (GetComponent<AudioSource>().clip == null)
{
Debug.LogException(new System.Exception("No AudioClip set in AudioSource Component."));
return;
}
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = loop;
}
public void ChangeBackgrundMusic(AudioClip newBackgroundMusic, bool loop = true)
{
GetComponent<AudioSource>().clip = newBackgroundMusic;
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = loop;
}
public void ChangeBackgrundMusic(string newBackgroundMusic, bool loop = true)
{
GetComponent<AudioSource>().clip = registeredAudioClips [newBackgroundMusic];
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = loop;
}
public void DecreaseAudio(float time, float newVolume = 0f)
{
float speed = Mathf.Abs(GetComponent<AudioSource>().volume - newVolume) / time;
StartCoroutine(DecreaseAudioCoroutine(speed, newVolume));
}
public void IncreaseAudio(float time, float newVolume = 1f)
{
float speed = Mathf.Abs(GetComponent<AudioSource>().volume - newVolume) / time;
StartCoroutine(IncreaseAudioCoroutine(speed, newVolume));
}
IEnumerator DecreaseAudioCoroutine(float speed, float newVolume)
{
yield return new WaitForSeconds(1f);
GetComponent<AudioSource>().volume -= speed;
if (GetComponent<AudioSource>().volume > newVolume)
StartCoroutine(DecreaseAudioCoroutine(speed, newVolume));
}
IEnumerator IncreaseAudioCoroutine(float speed, float newVolume)
{
yield return new WaitForSeconds(1f);
GetComponent<AudioSource>().volume += speed;
if (GetComponent<AudioSource>().volume < newVolume)
StartCoroutine(IncreaseAudioCoroutine(speed, newVolume));
}
}
O erro e esse quando chamo o comando
- Código:
Exception: AudioClip not found.
UnityEngine.Debug:LogException(Exception)
AudioProvider:PlayAudioShotInPlayer(String, Single) (at Assets/Scripts/Providers/AudioProvider.cs:74)
Controller:Start() (at Assets/Scripts/Behaviour/Controller.cs:28)
Re: Acessar funçoes de outro script?
O próprio nome já diz... O áudio clip não foi encontrado. Você não adicionou o áudio que é chamado ou o nome está diferente e por isso está dando o erro!Callyde Jr escreveu:Bom dia amigos estou com um problrminha aqui para chamar uma funçao de outro script
Esse script registra todos audios que voce adicionar a ele.
E estou usando outro script para chamar a musica que vai tocar com esses 2 comandos
- Código:
AudioProvider.Instance.PlayAudioShotInPlayer("Musica1");
GetComponent<AudioProvider> ().PlayAudioShotInPlayer("Musica1");
- Código:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
[RequireComponent(typeof(AudioSource))]
public class AudioProvider : MonoBehaviour
{
public AudioSource playerAudioSource;
public AudioClip[] audioClips;
public static AudioProvider Instance { get; private set; }
public float Volume
{
get{ return GetComponent<AudioSource>().volume;}
set{ GetComponent<AudioSource>().volume = value;}
}
public bool Loop
{
get{ return GetComponent<AudioSource>().loop;}
set{ GetComponent<AudioSource>().loop = value;}
}
private Dictionary<string, AudioClip> registeredAudioClips;
void Awake()
{
Instance = this;
RegisteAudioClips();
}
void RegisteAudioClips()
{
registeredAudioClips = new Dictionary<string, AudioClip>();
for (int i = 0; i < audioClips.Length; i++)
{
registeredAudioClips.Add(audioClips [i].name, audioClips [i]);
}
}
public AudioClip GetRegisteredAudioClip(string audioClipName)
{
if (registeredAudioClips.ContainsKey(audioClipName))
return registeredAudioClips [audioClipName];
throw new UnityException("No audio clip registered");
}
public void PlayAudioShot(string audioClipName, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName))
GetComponent<AudioSource>().PlayOneShot(registeredAudioClips [audioClipName], volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayAudioShot(AudioSource source, string audioClipName, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName))
source.PlayOneShot(registeredAudioClips [audioClipName], volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayAudioShotInPlayer(string audioClipName, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName) &&
!playerAudioSource.isPlaying)
playerAudioSource.PlayOneShot(registeredAudioClips [audioClipName], volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayAudioShotInPosition(string audioClipName, Vector3 position, float volume = 1f)
{
if (registeredAudioClips.ContainsKey(audioClipName))
AudioSource.PlayClipAtPoint(registeredAudioClips [audioClipName], position, volume);
else
Debug.LogException(new System.Exception("AudioClip not found."));
}
public void PlayBackgroundMusic(bool loop = true)
{
if (GetComponent<AudioSource>().clip == null)
{
Debug.LogException(new System.Exception("No AudioClip set in AudioSource Component."));
return;
}
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = loop;
}
public void ChangeBackgrundMusic(AudioClip newBackgroundMusic, bool loop = true)
{
GetComponent<AudioSource>().clip = newBackgroundMusic;
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = loop;
}
public void ChangeBackgrundMusic(string newBackgroundMusic, bool loop = true)
{
GetComponent<AudioSource>().clip = registeredAudioClips [newBackgroundMusic];
GetComponent<AudioSource>().Play();
GetComponent<AudioSource>().loop = loop;
}
public void DecreaseAudio(float time, float newVolume = 0f)
{
float speed = Mathf.Abs(GetComponent<AudioSource>().volume - newVolume) / time;
StartCoroutine(DecreaseAudioCoroutine(speed, newVolume));
}
public void IncreaseAudio(float time, float newVolume = 1f)
{
float speed = Mathf.Abs(GetComponent<AudioSource>().volume - newVolume) / time;
StartCoroutine(IncreaseAudioCoroutine(speed, newVolume));
}
IEnumerator DecreaseAudioCoroutine(float speed, float newVolume)
{
yield return new WaitForSeconds(1f);
GetComponent<AudioSource>().volume -= speed;
if (GetComponent<AudioSource>().volume > newVolume)
StartCoroutine(DecreaseAudioCoroutine(speed, newVolume));
}
IEnumerator IncreaseAudioCoroutine(float speed, float newVolume)
{
yield return new WaitForSeconds(1f);
GetComponent<AudioSource>().volume += speed;
if (GetComponent<AudioSource>().volume < newVolume)
StartCoroutine(IncreaseAudioCoroutine(speed, newVolume));
}
}
O erro e esse quando chamo o comando
- Código:
Exception: AudioClip not found.
UnityEngine.Debug:LogException(Exception)
AudioProvider:PlayAudioShotInPlayer(String, Single) (at Assets/Scripts/Providers/AudioProvider.cs:74)
Controller:Start() (at Assets/Scripts/Behaviour/Controller.cs:28)
Re: Acessar funçoes de outro script?
Muito estranho os erros so mostra no editor o jogo copiladonao mostra nada funciona perfeito?
Re: Acessar funçoes de outro script?
Usa um singleton, fiz um generico: https://github.com/augustobrit/SimpleUnitySingletonGeneric
Tópicos semelhantes
» Como acessar as funções de pós processamento por script?
» Como acessar variável de um script usando outro script (Entre e entenda).
» Acessar Outro Script C#
» Acessar dicioario de dados em outro scrpt
» COMO DESATIVAR O SCRIPT "FisrtPersonController" DO PLAYER ATRAVÉS DE OUTRO SCRIPT
» Como acessar variável de um script usando outro script (Entre e entenda).
» Acessar Outro Script C#
» Acessar dicioario de dados em outro scrpt
» COMO DESATIVAR O SCRIPT "FisrtPersonController" DO PLAYER ATRAVÉS DE OUTRO SCRIPT
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos