*
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using Debug = UnityEngine.Debug;
|
||||
|
||||
public class AiSound : MonoBehaviour {
|
||||
public AudioSource audio;
|
||||
private bool _isPlaying;
|
||||
private float _startTime;
|
||||
public float rangeScale = 1.0f;
|
||||
public float StartTime {
|
||||
get { return _startTime; }
|
||||
}
|
||||
public bool IsPlaying {
|
||||
get { return _isPlaying; }
|
||||
}
|
||||
|
||||
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
private void BroadcastBeginPlaying() {
|
||||
_startTime = Time.time;
|
||||
|
||||
var overlapped = Physics2D.OverlapCircleAll(transform.position, audio.maxDistance * rangeScale);
|
||||
var listeners = overlapped.Select(v => v.gameObject.GetComponent<AiSoundListener>()).Where(v => v != null).Distinct().ToList();
|
||||
|
||||
if (listeners.Count() != 0) {
|
||||
foreach (var listener in listeners) {
|
||||
listener.OnSoundStart(this);
|
||||
Debug.Log("Broadcast begin playing to object: " + listener.name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Debug.Log("AiSound started but no listeners found");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void BroadcastEndPlaying() {
|
||||
Debug.Log("EndPlaying");
|
||||
}
|
||||
|
||||
|
||||
void Update () {
|
||||
if (audio != null) {
|
||||
if (_isPlaying != audio.isPlaying) {
|
||||
if (!_isPlaying) {
|
||||
BroadcastBeginPlaying();
|
||||
}
|
||||
else {
|
||||
BroadcastEndPlaying();
|
||||
}
|
||||
}
|
||||
_isPlaying = audio.isPlaying;
|
||||
}
|
||||
else {
|
||||
Debug.LogWarning("AiSound AudioSource is null!!!", this);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDrawGizmos() {
|
||||
if (audio != null) {
|
||||
AdvancedGizmo.DrawCircle(transform.position, Vector3.forward, audio.maxDistance * rangeScale, Color.green);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fd9d1ef233396e4a9fdb2019f8f8c19
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class AiSoundListener : MonoBehaviour {
|
||||
|
||||
private List<AiSound> _sensibleSounds = new List<AiSound>();
|
||||
private AiSound _latestSound;
|
||||
public AiSound LatestSound {
|
||||
get { return _latestSound; }
|
||||
}
|
||||
public void OnSoundStart(AiSound sound) {
|
||||
if (sound == null) {
|
||||
return;
|
||||
}
|
||||
if (!_sensibleSounds.Contains(sound)) {
|
||||
_sensibleSounds.Add(sound);
|
||||
}
|
||||
_latestSound = FindLatest();
|
||||
}
|
||||
|
||||
public AiSound FindLatest(float volumeTreshold = 0) {
|
||||
if (_sensibleSounds.Count == 0) {
|
||||
return null;
|
||||
}
|
||||
return _sensibleSounds.OrderByDescending(v => v.StartTime).First();
|
||||
}
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
void Update () {
|
||||
_sensibleSounds.RemoveAll(v => !v.IsPlaying);
|
||||
if (!_sensibleSounds.Contains(_latestSound)) {
|
||||
_latestSound = FindLatest();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 687dc430974c4b642a8e3118eb2f9628
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
Reference in New Issue
Block a user