You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			55 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
		
		
			
		
	
	
			55 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
| 
											2 years ago
										 | using UnityEngine; | ||
|  | 
 | ||
|  | namespace Game | ||
|  | { | ||
|  |     /// <summary> | ||
|  |     ///     音效池化管理 | ||
|  |     /// </summary> | ||
|  |     public class AudioPoolItem : ObjectPoolItemBase | ||
|  |     { | ||
|  |         public AudioSource AudioSource; | ||
|  |         public GameObject GameObject; | ||
|  |         protected override void OnDestroy() | ||
|  |         { | ||
|  |             base.OnDestroy(); | ||
|  |             GameObject.transform.SetParent(AudioPoolManager.Instance.Root.transform); | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public class AudioPoolManager : ObjectPoolBase<AudioPoolManager> | ||
|  |     { | ||
|  |         public override void Update() | ||
|  |         { | ||
|  |             foreach (var pool in ObjectPool) | ||
|  |             foreach (var item in pool.Value) | ||
|  |             { | ||
|  |                 if (!item.IsAlive) continue; | ||
|  |                 if (((AudioPoolItem)item).AudioSource.isPlaying) item.Destroy(); | ||
|  |             } | ||
|  |         } | ||
|  |         public void CreateSound(Transform transform, AudioClip audioClip) | ||
|  |         { | ||
|  |             var audioNew = Create<AudioPoolItem>($"Audio{audioClip.name}", item => | ||
|  |             { | ||
|  |                 item.GameObject = new GameObject("Audio"); | ||
|  |                 item.AudioSource = item.GameObject.AddComponent<AudioSource>(); | ||
|  |             }); | ||
|  | 
 | ||
|  |             audioNew.GameObject.transform.parent = transform; | ||
|  |             var audioSource = audioNew.GameObject.GetComponent<AudioSource>(); | ||
|  |             audioSource.PlayOneShot(audioClip); | ||
|  |         } | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | namespace Game | ||
|  | { | ||
|  |     public abstract partial class Util | ||
|  |     { | ||
|  |         public static void CreateSound(GameEntity entity, AudioClip audioClip) | ||
|  |         { | ||
|  |             var trans = entity.view.TransformViewOther; | ||
|  |             AudioPoolManager.Instance.CreateSound(trans, audioClip); | ||
|  |         } | ||
|  |     } | ||
|  | } |