Unity之可寻址资源之初始化,下载
以下代码实现了Addressable Assets的初始化,登陆时下载,静默下载,如下:
1 using ClientData; 2 using Common; 3 using System; 4 using System.Collections; 5 using System.Collections.Generic; 6 using System.IO; 7 using System.Text; 8 using UnityEngine; 9 using UnityEngine.AddressableAssets; 10 using UnityEngine.ResourceManagement.AsyncOperations; 11 using UnityEngine.ResourceManagement.ResourceLocations; 12 using UnityEngine.ResourceManagement.ResourceProviders; 13 14 namespace FashionBeat 15 { 16 public class AddressableABManager : GameBehaviour 17 { 18 public static int 19 MaxLoginDownloadingThread = 10, 20 MinHotPatchMB = 10; 21 public static float 22 ProgressBarSpeed = 0.05f; 23 public static bool 24 //IsOpenLoginDownload = true, 25 IsOpenQuietlyDownload = true; 26 27 const string 28 DefaultRampName = "model_ramp_default", 29 LoginDownloadKeysFile = "Address/t_download_login", 30 QuietlyDownloadKeysFile = "Address/t_download_quietly"; 31 32 const float SizeScale = 1f / 1024 / 1024; 33 static readonly StringBuilder s_sb = new StringBuilder(); 34 35 string m_mainABFolder, m_localABFolder; 36 float m_loginDownloadedMB, m_timerLoginDownloading, m_quietlyDownloadedMB, m_timerQuietlyDownloading; 37 Queue<object> m_loginDownloadKeys, m_quietlyDownloadKeys; 38 List<object> m_loginDownloadingKeys; 39 float m_completeProgressWhenInit; 40 41 // Singleton 42 static AddressableABManager s_singleton; 43 public static AddressableABManager Singleton 44 { 45 get 46 { 47 if (!s_singleton) 48 { 49 GameObject go = new GameObject(typeof(AddressableABManager).ToString()); 50 go.transform.parent = GameApp.Singleton.transform; 51 s_singleton = go.AddComponent<AddressableABManager>(); 52 } 53 return s_singleton; 54 } 55 } 56 57 58 public enum Stage 59 { 60 None, 61 Initializing, 62 Initialized, 63 64 StartLoginDownload, 65 LoginDownloading, 66 LoginDownloaded, 67 68 QuietlyDownloading, 69 QuietlyDownloadPause, 70 QuietlyDownloaded, 71 } 72 73 74 #region Property 75 76 static string PlatformFolder 77 { 78 get 79 { 80 #if UNITY_IPHONE 81 return "iOS"; 82 #else 83 return "Android"; 84 #endif 85 } 86 } 87 88 public Texture DefaultRamp { get; private set; } 89 public float PercentComplete { get; private set; } 90 public Stage CurStage { get; private set; } 91 public string Messenge { get; private set; } 92 93 94 // 登陆时下载 95 public float LoginNeedDownloadMB { get; private set; } 96 public float LoginDownloadedMB { get; private set; } 97 public static bool IsOpenLoginDownload => Analysis.AnalysisManager.IsOpenLoginDownload(); 98 public bool LoginNeedHotPatch => IsOpenLoginDownload && LoginNeedDownloadMB > MinHotPatchMB; 99 public float LoginDownloadSpeedMB { get; private set; } 100 public float LoginDownloadedSeconds => m_timerLoginDownloading; 101 public float LoginDownloadLeftSeconds { get; private set; } 102 103 104 // 静默下载 105 public bool NeedQuietlyDownload => IsOpenQuietlyDownload && m_quietlyDownloadKeys != null && m_quietlyDownloadKeys.Count > 0 && QuietlyNeedDownloadMB > 0; 106 public float QuietlyNeedDownloadMB { get; private set; } 107 public float QuietlyDownloadedMB { get; private set; } 108 public float QuietlyDownloadSpeedMB { get; private set; } 109 110 #endregion 111 112 113 void Awake() 114 { 115 PercentComplete = 0; 116 LoginNeedDownloadMB = 0; 117 CurStage = Stage.None; 118 m_completeProgressWhenInit = 0; 119 } 120 121 122 #region 初始化 123 124 public void Init(string mainCatalogName) 125 { 126 IEnumerator etor = InitAsync(mainCatalogName); 127 GameApp.Singleton.GameMain.StartCoroutine(etor); 128 } 129 130 IEnumerator InitAsync(string mainCatalogName) 131 { 132 CurStage = Stage.Initializing; 133 PercentComplete = 0; 134 LoginNeedDownloadMB = 0; 135 QuietlyNeedDownloadMB = 0; 136 137 string ftpServer = PackageConfig.Singleton.Config.f_ftpServer; 138 string platform = PlatformFolder; 139 string versionDay = PackageConfig.Singleton.VersionDay; 140 string catalogRootPath = $"{ftpServer}/{versionDay}/{platform}"; 141 m_mainABFolder = $"{catalogRootPath}/Main/"; 142 m_localABFolder = $"{Addressables.RuntimePath}/{platform}/"; 143 string mainCatalogPath = m_mainABFolder + mainCatalogName; 144 145 Debug.Log("ftp ab url: " + m_mainABFolder); 146 Debug.Log("local ab url: " + m_localABFolder); 147 148 // 第一次装游戏尝试从包里解压 catalog 文件 149 Messenge = Localization.Get("正在尝试从包中解压资源列表"); 150 yield return null; 151 TryCompressLocalCatalog(mainCatalogName); 152 yield return new WaitForEndOfFrame(); 153 154 // 等待初始化完毕,否则无法热更 155 Messenge = Localization.Get("正在初始化资源管理器"); 156 yield return Addressables.InitializeAsync(); 157 158 // url 重定向 159 Addressables.InternalIdTransformFunc = RedirectUrl; 160 161 // 初始化热更 162 if (Config_t_fb_game_config.Singleton.IsOpenHotPatch) 163 { 164 var etor = InitHotpatchAsync(mainCatalogPath); 165 while (etor.MoveNext()) 166 yield return null; 167 } 168 169 Messenge = Localization.Get("初始化完成"); 170 PercentComplete = 1; 171 yield return null; 172 CurStage = Stage.Initialized; 173 OnInitFinished(); 174 } 175 176 IEnumerator InitHotpatchAsync(string mainCatalogPath) 177 { 178 Addressables.ClearResourceLocators(); 179 180 // 加载 main catalog 181 Messenge = Localization.Get("正在加载资源列表"); 182 m_completeProgressWhenInit = 0.9f; 183 yield return null; 184 185 var handle_main = Addressables.LoadContentCatalogAsync(mainCatalogPath, true); 186 Debug.Log("Start load catalog: " + mainCatalogPath); 187 while (!handle_main.IsDone) 188 { 189 //PercentComplete = m_completeProgressWhenInit + handle_main.PercentComplete * 0.2f; 190 yield return null; 191 } 192 PercentComplete = m_completeProgressWhenInit; 193 Debug.Log("Load catalog finished: " + mainCatalogPath); 194 UserDataAnalyze.TryMark(UserDataAnalyze.ActionType.LoadCatalog_Main); 195 196 // 检查资源更新 197 Messenge = Localization.Get("正在检查哪些资源需要更新"); 198 m_completeProgressWhenInit = 1f; 199 yield return null; 200 201 var reqLoginDownloadList = Resources.LoadAsync<TextAsset>(LoginDownloadKeysFile); 202 while (reqLoginDownloadList.isDone) 203 yield return null; 204 var reqQuietlyDownloadList = Resources.LoadAsync<TextAsset>(QuietlyDownloadKeysFile); 205 while (reqQuietlyDownloadList.isDone) 206 yield return null; 207 208 GetDownloadKeys(reqLoginDownloadList.asset, reqQuietlyDownloadList.asset); 209 210 // 计算资源大小 211 Messenge = Localization.Get("正在计算需要更新的资源的大小"); 212 yield return null; 213 214 if (m_loginDownloadKeys.Count > 0) 215 { 216 var handleSize = Addressables.GetDownloadSizeAsync(m_loginDownloadKeys); 217 while (!handleSize.IsDone) 218 { 219 PercentComplete = 0.95f + handleSize.PercentComplete * 0.05f; 220 yield return null; 221 } 222 LoginNeedDownloadMB = handleSize.Result * SizeScale; 223 Addressables.Release(handleSize); 224 Debug.Log($"Need login download: {LoginNeedDownloadMB}MB, count: {m_loginDownloadKeys.Count}"); 225 } 226 227 if (m_quietlyDownloadKeys.Count > 0) 228 { 229 var handleSize = Addressables.GetDownloadSizeAsync(m_quietlyDownloadKeys); 230 while (!handleSize.IsDone) 231 yield return null; 232 QuietlyNeedDownloadMB = handleSize.Result * SizeScale; 233 Addressables.Release(handleSize); 234 Debug.Log($"Need quietly download: {QuietlyNeedDownloadMB}MB, count: {m_quietlyDownloadKeys.Count}"); 235 } 236 } 237 238 void TryCompressLocalCatalog(string mainCatalogName) 239 { 240 string catalogCacheFolder = $"{Application.persistentDataPath}/com.unity.addressables/"; 241 string catalogCachePath_json = $"{catalogCacheFolder}{mainCatalogName}"; 242 string catalogCachePath_hash = catalogCachePath_json.Replace(".json", ".hash"); 243 244 if (File.Exists(catalogCachePath_json) && File.Exists(catalogCachePath_hash)) 245 return; 246 247 if (!Directory.Exists(catalogCacheFolder)) 248 Directory.CreateDirectory(catalogCacheFolder); 249 250 TextAsset taJson = Resources.Load<TextAsset>("Address/json"); 251 TextAsset taHash = Resources.Load<TextAsset>("Address/hash"); 252 File.WriteAllText(catalogCachePath_json, taJson.text); 253 File.WriteAllText(catalogCachePath_hash, taHash.text); 254 } 255 256 void GetDownloadKeys(UnityEngine.Object loginDownloadList, UnityEngine.Object quietlyDownloadList) 257 { 258 m_loginDownloadKeys = new Queue<object>(); 259 TextAsset loginDownloadListTA = loginDownloadList as TextAsset; 260 if (loginDownloadListTA != null) 261 { 262 string[] loginDownloadAssets = loginDownloadListTA.text.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); 263 for (int i = 0; i < loginDownloadAssets.Length; i++) 264 m_loginDownloadKeys.Enqueue(loginDownloadAssets[i]); 265 } 266 267 m_quietlyDownloadKeys = new Queue<object>(); 268 TextAsset quietlyDownloadListTA = quietlyDownloadList as TextAsset; 269 if (quietlyDownloadListTA != null) 270 { 271 string[] quietlyDownloadAssets = quietlyDownloadListTA.text.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); 272 for (int i = 0; i < quietlyDownloadAssets.Length; i++) 273 m_quietlyDownloadKeys.Enqueue(quietlyDownloadAssets[i]); 274 } 275 } 276 277 278 // 重定向ab包地址 279 string RedirectUrl(IResourceLocation loc) 280 { 281 string oldUrl = loc.InternalId.Replace("\\", "/"); 282 if (!(loc.Data is AssetBundleRequestOptions)) 283 return oldUrl; 284 285 string abFolderPath = Path.GetDirectoryName(oldUrl); 286 string abFolderName = Path.GetFileNameWithoutExtension(abFolderPath); 287 string abFolder = abFolderName switch 288 { 289 "Main" => m_mainABFolder, 290 _ => m_localABFolder, 291 }; 292 293 if (abFolder == null) 294 { 295 return oldUrl; 296 } 297 298 string abName = Path.GetFileName(oldUrl); 299 s_sb.Length = 0; 300 s_sb.Append(abFolder); 301 s_sb.Append(abName); 302 return s_sb.ToString(); 303 //Debug.Log($"oldUrl: {oldUrl}, new url: {newUrl}, abFolder: {abFolder}, ab name: {abName}, abFolderName: {abFolderName}"); 304 } 305 306 void OnInitFinished() 307 { 308 // 加载默认 ramp 309 var assetRamp = new ImageAssetItem(ImageAssetHelp.Subfolder_Ramp, DefaultRampName); 310 assetRamp.Load(() => 311 { 312 DefaultRamp = assetRamp.TexObject; 313 }); 314 315 // 加载粒子图集 316 //ParticleAtlasLoader.Initialize(); 317 } 318 319 #endregion 320 321 322 #region 登陆时下载 323 324 public void LoginDownload(Action onFinished) 325 { 326 if (!LoginNeedHotPatch) 327 return; 328 329 CurStage = Stage.StartLoginDownload; 330 PercentComplete = 0; 331 m_loginDownloadedMB = 0; 332 m_loginDownloadingKeys = new List<object>(); 333 LoginDownloadedMB = 0; 334 m_timerLoginDownloading = 0; 335 336 for (int i = 0; i < MaxLoginDownloadingThread; i++) 337 { 338 StartCoroutine(LoginDownloadAsync(onFinished)); 339 } 340 } 341 342 void Update() 343 { 344 if (CurStage == Stage.StartLoginDownload) 345 { 346 if (LoginDownloadedMB > 0) 347 CurStage = Stage.LoginDownloading; 348 } 349 else if (CurStage == Stage.LoginDownloading) 350 { 351 m_timerLoginDownloading += Time.deltaTime; 352 LoginDownloadSpeedMB = LoginDownloadedMB / m_timerLoginDownloading; 353 LoginDownloadLeftSeconds = (LoginNeedDownloadMB - LoginDownloadedMB) / LoginDownloadSpeedMB; 354 } 355 else if (CurStage == Stage.Initializing) 356 { 357 if (PercentComplete < m_completeProgressWhenInit) 358 { 359 PercentComplete += Time.deltaTime * ProgressBarSpeed; 360 } 361 if (PercentComplete > 1) 362 { 363 PercentComplete = 1; 364 } 365 } 366 else if (CurStage == Stage.QuietlyDownloading) 367 { 368 m_timerQuietlyDownloading += Time.deltaTime; 369 QuietlyDownloadSpeedMB = QuietlyDownloadedMB / m_timerQuietlyDownloading; 370 } 371 } 372 373 IEnumerator LoginDownloadAsync(Action onFinished) 374 { 375 while (m_loginDownloadKeys.Count > 0) 376 { 377 object key = m_loginDownloadKeys.Dequeue(); 378 // 获取下载大小 379 var handleSize = Addressables.GetDownloadSizeAsync(key); 380 yield return handleSize; 381 float sizeMB = handleSize.Result * SizeScale; 382 Addressables.Release(handleSize); 383 384 if (sizeMB == 0) 385 continue; 386 387 // 开始下载 388 AsyncOperationHandle handleDownload = Addressables.DownloadDependenciesAsync(key, false); 389 m_loginDownloadingKeys.Add(key); 390 // 下载中 391 while (!handleDownload.IsDone) 392 { 393 LoginDownloadedMB = m_loginDownloadedMB + handleDownload.PercentComplete * sizeMB; 394 PercentComplete = Mathf.Clamp01(LoginDownloadedMB / LoginNeedDownloadMB); 395 yield return null; 396 } 397 398 // 下载完成 399 m_loginDownloadingKeys.Remove(key); 400 m_loginDownloadedMB += sizeMB; 401 //Debug.Log($"Download finished, key: {key}, status: {handleDownload.Status}, size: {sizeMB}mb"); 402 Addressables.Release(handleDownload); 403 } 404 405 if (m_loginDownloadingKeys.Count > 0) 406 yield break; 407 408 // 所有资源下载完毕 409 m_loginDownloadingKeys = null; 410 PercentComplete = 1; 411 CurStage = Stage.LoginDownloaded; 412 LoginDownloadedMB = LoginNeedDownloadMB; 413 if (onFinished != null) 414 onFinished(); 415 } 416 417 #endregion 418 419 420 #region 静默下载 421 422 void QuietlyDownload() 423 { 424 if (!NeedQuietlyDownload) 425 return; 426 427 CurStage = Stage.QuietlyDownloading; 428 PercentComplete = 0; 429 m_quietlyDownloadedMB = 0; 430 QuietlyDownloadedMB = 0; 431 m_timerQuietlyDownloading = 0; 432 StartCoroutine(QuietlyDownloadAsync()); 433 434 Debug.Log($"Start quietly download, size: {QuietlyNeedDownloadMB} MB"); 435 } 436 437 IEnumerator QuietlyDownloadAsync() 438 { 439 while (m_quietlyDownloadKeys.Count > 0) 440 { 441 while (CurStage == Stage.QuietlyDownloadPause) 442 yield return null; 443 444 object key = m_quietlyDownloadKeys.Dequeue(); 445 // 获取下载大小 446 var handleSize = Addressables.GetDownloadSizeAsync(key); 447 yield return handleSize; 448 float sizeMB = handleSize.Result * SizeScale; 449 Addressables.Release(handleSize); 450 451 if (sizeMB == 0) 452 continue; 453 454 // 开始下载 455 AsyncOperationHandle handleDownload = Addressables.DownloadDependenciesAsync(key, false); 456 // 下载中 457 while (!handleDownload.IsDone) 458 { 459 QuietlyDownloadedMB = m_quietlyDownloadedMB + handleDownload.PercentComplete * sizeMB; 460 PercentComplete = Mathf.Clamp01(QuietlyDownloadedMB / QuietlyNeedDownloadMB); 461 yield return null; 462 } 463 464 // 下载完成 465 m_quietlyDownloadedMB += sizeMB; 466 //Debug.Log($"Download finished, key: {key}, status: {handleDownload.Status}, size: {sizeMB}MB, progress: {QuietlyDownloadedMB}MB / {QuietlyNeedDownloadMB}MB"); 467 Addressables.Release(handleDownload); 468 } 469 470 // 所有资源下载完毕 471 PercentComplete = 1; 472 CurStage = Stage.QuietlyDownloaded; 473 QuietlyDownloadedMB = QuietlyNeedDownloadMB; 474 } 475 476 public void ToggleQuietlyDownload(bool isOpen) 477 { 478 if (isOpen) 479 { 480 if (CurStage == Stage.QuietlyDownloadPause) 481 { 482 CurStage = Stage.QuietlyDownloading; 483 return; 484 } 485 486 if (CurStage == Stage.QuietlyDownloading) 487 return; 488 if (!NeedQuietlyDownload) 489 return; 490 QuietlyDownload(); 491 } 492 else 493 { 494 if (CurStage == Stage.QuietlyDownloading) 495 { 496 CurStage = Stage.QuietlyDownloadPause; 497 Debug.Log("Stop quietly download"); 498 } 499 } 500 } 501 502 // 尝试静默下载 503 public void TryOpenQuietlyDownload() 504 { 505 if (ClientSetting.Singleton.IsOpenQuietlyDownload) 506 ToggleQuietlyDownload(true); 507 } 508 509 510 #endregion 511 512 513 public void ClearCache() 514 { 515 Caching.ClearCache(); 516 GameApp.QuitGame(); 517 } 518 } 519 }
热更导演类:
1 using Modules.UI; 2 3 namespace FashionBeat 4 { 5 public class Director_HotPatching : DirectorBase 6 { 7 DirectorBase m_nextDir; 8 string m_strTips; 9 10 11 public override void Enter() 12 { 13 base.Enter(); 14 15 if (!Config_t_fb_game_config.Singleton.IsOpenHotPatch) 16 { 17 GoLogin(); 18 return; 19 } 20 21 if (AddressableABManager.Singleton.LoginNeedHotPatch) 22 { 23 float size = AddressableABManager.Singleton.LoginNeedDownloadMB; 24 string msg = string.Format(Localization.Get("发现新版本,立即更新\n资源大小:") + "{0:f2}MB", size); 25 26 UI_BeforeLoginWnd.Singleton.Tips = Localization.Get("初始化完成"); 27 UI_BeforeLoginWnd.Singleton.ShowMsgBox(msg, StartDownload); 28 } 29 else 30 { 31 UI_BeforeLoginWnd.Singleton.Tips = Localization.Get("已经是最新版本!"); 32 UI_BeforeLoginWnd.Singleton.HideMsgBox(); 33 GoLogin(); 34 } 35 } 36 37 void StartDownload() 38 { 39 AddressableABManager.Singleton.LoginDownload(OnDownloaded); 40 Analysis.AnalysisManager.TrackCommonEvent(Analysis.AnalysisEventType.BEGIN_DOWNLOAD); 41 42 Director_Login.TryLoadNotice(); 43 } 44 45 void OnDownloaded() 46 { 47 UI_BeforeLoginWnd.Singleton.Tips = Localization.Get("下载完成"); 48 GoLogin(); 49 Analysis.AnalysisManager.TrackCommonEvent(Analysis.AnalysisEventType.DOWNLOAD_SUCCESS); 50 } 51 52 void GoLogin() 53 { 54 m_nextDir = new Director_Login(); 55 } 56 57 public override DirectorBase Update() 58 { 59 if (AddressableABManager.Singleton.CurStage == AddressableABManager.Stage.StartLoginDownload) 60 { 61 UI_BeforeLoginWnd.Singleton.Tips = Localization.Get("正在扫描资源"); 62 UI_BeforeLoginWnd.Singleton.BarValue = 0; 63 } 64 else if (AddressableABManager.Singleton.CurStage == AddressableABManager.Stage.LoginDownloading) 65 { 66 float downloadedSize = AddressableABManager.Singleton.LoginDownloadedMB; 67 float needDownloadedSize = AddressableABManager.Singleton.LoginNeedDownloadMB; 68 float downloadSpeed = AddressableABManager.Singleton.LoginDownloadSpeedMB; 69 float downadedSeconds = AddressableABManager.Singleton.LoginDownloadedSeconds; 70 float leftSeconds = AddressableABManager.Singleton.LoginDownloadLeftSeconds; 71 72 if (m_strTips == null) 73 { 74 m_strTips = Localization.Get("正在下载:"); 75 m_strTips += "{0:f2}MB / {1:f2}MB"; 76 m_strTips += " " + Localization.Get("速率:"); 77 m_strTips += "{2:f2}MB/S"; 78 m_strTips += " " + Localization.Get("下载时长:"); 79 m_strTips += "{3:f0}S"; 80 m_strTips += " " + Localization.Get("预计剩余:"); 81 m_strTips += "{4:f0}S"; 82 } 83 84 UI_BeforeLoginWnd.Singleton.Tips = string.Format(m_strTips, 85 downloadedSize, needDownloadedSize, downloadSpeed, downadedSeconds, leftSeconds); 86 UI_BeforeLoginWnd.Singleton.BarValue = AddressableABManager.Singleton.PercentComplete; 87 } 88 89 return m_nextDir ?? this; 90 } 91 92 public override void Exit() 93 { 94 base.Exit(); 95 m_strTips = null; 96 } 97 98 } 99 }
下载界面:
1 using FashionBeat; 2 using System; 3 using UnityEngine; 4 5 namespace Modules.UI 6 { 7 // [fb] 8 public class UI_BeforeLoginWnd : MonoBehaviour 9 { 10 static UI_BeforeLoginWnd s_singleton; 11 12 13 [SerializeField] 14 UISlider m_slider; 15 [SerializeField] 16 UILabel m_labelTips; 17 [SerializeField] 18 UILabel m_labelProgress; 19 20 [SerializeField] 21 GameObject m_goMsgBox; 22 [SerializeField] 23 UILabel m_labelMsgText; 24 [SerializeField] 25 GameObject m_buttonMsgOK; 26 [SerializeField] 27 Transform m_FB; 28 29 30 public static UI_BeforeLoginWnd Singleton 31 { 32 get 33 { 34 if (!s_singleton) 35 { 36 var asset = Resources.Load("Prefabs/Windows/ui_before_login"); 37 Transform parent = FBUIRoot.Singleton.transform; 38 GameObject obj = (GameObject)Instantiate(asset, parent); 39 obj.transform.localScale = Vector3.one; 40 obj.transform.localPosition = Vector3.zero; 41 s_singleton = obj.GetComponent<UI_BeforeLoginWnd>(); 42 } 43 return s_singleton; 44 } 45 } 46 47 public static bool IsShowing => s_singleton; 48 49 50 51 public float BarValue 52 { 53 set 54 { 55 m_slider.value = value; 56 m_labelProgress.text = value.ToString("p2"); 57 m_slider.gameObject.SetActive(value > 0); 58 m_labelProgress.gameObject.SetActive(value > 0); 59 } 60 } 61 62 public string Tips 63 { 64 set 65 { 66 bool hasValue = !string.IsNullOrEmpty(value); 67 m_labelTips.gameObject.SetActive(hasValue); 68 if (hasValue) 69 m_labelTips.text = value; 70 } 71 } 72 73 74 public static UI_BeforeLoginWnd Create() 75 { 76 return Singleton; 77 } 78 79 public static void Destroy() 80 { 81 if (s_singleton) 82 { 83 Destroy(s_singleton.gameObject); 84 s_singleton = null; 85 } 86 } 87 88 public static void OnlyShowBg() 89 { 90 Singleton.BarValue = 0; 91 Singleton.Tips = null; 92 Singleton.HideMsgBox(); 93 } 94 95 96 void Awake() 97 { 98 s_singleton = this; 99 100 Tips = "Tip: Initializing..."; 101 BarValue = 0; 102 HideMsgBox(); 103 104 InitFBScreenScale(); 105 } 106 107 void OnDestroy() 108 { 109 s_singleton = null; 110 } 111 112 public void ShowMsgBox(string text, Action onClick) 113 { 114 m_goMsgBox.SetActive(true); 115 m_labelMsgText.text = text; 116 UIEventListener.Get(m_buttonMsgOK).onClick = go => 117 { 118 HideMsgBox(); 119 if (onClick != null) 120 onClick(); 121 }; 122 } 123 124 public void HideMsgBox() 125 { 126 m_goMsgBox.SetActive(false); 127 } 128 129 private void InitFBScreenScale() 130 { 131 Vector2 v = FBUIRoot.Singleton.NGUISize; 132 if (v.x / v.y > FBUIRoot.DefaultWidth / FBUIRoot.DefaultHeight) 133 { 134 return; 135 } 136 137 float t = v.y / FBUIRoot.DefaultHeight; 138 m_FB.localScale = new Vector3(t, t, 1); 139 } 140 141 } 142 }