|
| 1 | +//------------------------------------------------------------------------------------- |
| 2 | +// ServiceLocatorPatternExample.cs |
| 3 | +//------------------------------------------------------------------------------------- |
| 4 | + |
| 5 | +using UnityEngine; |
| 6 | +using System.Collections; |
| 7 | +using System; |
| 8 | + |
| 9 | +namespace ServiceLocatorPatternExample |
| 10 | +{ |
| 11 | + public class ServiceLocatorPatternExample : MonoBehaviour |
| 12 | + { |
| 13 | + void Start() |
| 14 | + { |
| 15 | + //注册到服务定位器 |
| 16 | + TheAudioPlayer audio = new TheAudioPlayer(); |
| 17 | + ServiceLocator.RegisterService(audio); |
| 18 | + } |
| 19 | + |
| 20 | + void Update() |
| 21 | + { |
| 22 | + //播放声音 |
| 23 | + if (Input.GetKeyDown(KeyCode.Alpha1)) |
| 24 | + { |
| 25 | + var audio=ServiceLocator.GetAudioService(); |
| 26 | + if (audio!=null) |
| 27 | + { |
| 28 | + audio.PlaySound(1); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + //结束声音 |
| 33 | + if (Input.GetKeyDown(KeyCode.Alpha2)) |
| 34 | + { |
| 35 | + var audio = ServiceLocator.GetAudioService(); |
| 36 | + if (audio != null) |
| 37 | + { |
| 38 | + audio.StopSound(1); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + //结束所有声音 |
| 43 | + if (Input.GetKeyDown(KeyCode.Alpha3)) |
| 44 | + { |
| 45 | + var audio = ServiceLocator.GetAudioService(); |
| 46 | + if (audio != null) |
| 47 | + { |
| 48 | + audio.StopAllSounds(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + //注册日志音频类 |
| 53 | + if (Input.GetKeyDown(KeyCode.Alpha4)) |
| 54 | + { |
| 55 | + ServiceLocator.EnableAudioLogging(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// 服务定位器管理类 |
| 66 | + /// </summary> |
| 67 | + public class ServiceLocator |
| 68 | + { |
| 69 | + static IAudio AudioService_; |
| 70 | + static NullAudio NullAudioService_; |
| 71 | + |
| 72 | + public static IAudio GetAudioService() { return AudioService_; } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// 注册服务 |
| 76 | + /// </summary> |
| 77 | + /// <param name="service"></param> |
| 78 | + public static void RegisterService(IAudio service) |
| 79 | + { |
| 80 | + if (service == null) |
| 81 | + { |
| 82 | + // Revert to null service. |
| 83 | + AudioService_ = NullAudioService_; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + AudioService_ = service; |
| 88 | + } |
| 89 | + Debug.Log("[ServiceLocator]Finish Register Service!"); |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// 注册带日志的音频类 |
| 94 | + /// </summary> |
| 95 | + public static void EnableAudioLogging() |
| 96 | + { |
| 97 | + // Decorate the existing service. |
| 98 | + IAudio service = new LoggedAudio(ServiceLocator.GetAudioService()); |
| 99 | + |
| 100 | + // Swap it in. |
| 101 | + RegisterService(service); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + ///音频接口 |
| 110 | + /// </summary> |
| 111 | + public interface IAudio |
| 112 | + { |
| 113 | + void PlaySound(int soundID); |
| 114 | + void StopSound(int soundID); |
| 115 | + void StopAllSounds(); |
| 116 | + }; |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// 实际的播放音频的实现类 |
| 120 | + /// </summary> |
| 121 | + public class TheAudioPlayer : IAudio |
| 122 | + { |
| 123 | + public void PlaySound(int soundID) |
| 124 | + { |
| 125 | + // Play sound using console audio api... |
| 126 | + Debug.Log("Play Sound ! ID = "+soundID.ToString()); |
| 127 | + } |
| 128 | + |
| 129 | + public void StopSound(int soundID) |
| 130 | + { |
| 131 | + // Stop sound using console audio api... |
| 132 | + Debug.Log("Stop Sound ! ID = " + soundID.ToString()); |
| 133 | + } |
| 134 | + |
| 135 | + public void StopAllSounds() |
| 136 | + { |
| 137 | + // Stop all sounds using console audio api... |
| 138 | + Debug.Log("Stop All Sound ! "); |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// null音频类 |
| 144 | + /// </summary> |
| 145 | + public class NullAudio : IAudio |
| 146 | + { |
| 147 | + public void PlaySound(int soundID) { /* Do nothing. */ } |
| 148 | + public void StopSound(int soundID) { /* Do nothing. */ } |
| 149 | + public void StopAllSounds() { /* Do nothing. */ } |
| 150 | + }; |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// 带日志的音频类 |
| 154 | + /// </summary> |
| 155 | + class LoggedAudio : IAudio |
| 156 | + { |
| 157 | + |
| 158 | + IAudio wrapped_; |
| 159 | + public LoggedAudio(IAudio wrapped) |
| 160 | + { |
| 161 | + wrapped_ = wrapped; |
| 162 | + } |
| 163 | + |
| 164 | + public void PlaySound(int soundID) |
| 165 | + { |
| 166 | + Log("[LoggedAudio]Play sound!"); |
| 167 | + wrapped_.PlaySound(soundID); |
| 168 | + } |
| 169 | + |
| 170 | + public void StopSound(int soundID) |
| 171 | + { |
| 172 | + Log("[LoggedAudio]Stop sound!"); |
| 173 | + wrapped_.StopSound(soundID); |
| 174 | + } |
| 175 | + |
| 176 | + public void StopAllSounds() |
| 177 | + { |
| 178 | + Log("[LoggedAudio]Stop all sounds!"); |
| 179 | + wrapped_.StopAllSounds(); |
| 180 | + } |
| 181 | + |
| 182 | + private void Log(string message) |
| 183 | + { |
| 184 | + Debug.LogError(message); |
| 185 | + // Code to log message... |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +} |
| 192 | + |
| 193 | + |
| 194 | + |
0 commit comments