/// <summary> /// 异步初始化 /// </summary> /// <returns></returns> public IEnumerator InitializeAsync() { // 下载面板 string location = "UIPanel/PatchWindow"; _handle = ResourceManager.Instance.LoadAssetAsync <GameObject>(location); yield return(_handle); if (_handle.AssetObject == null) { throw new Exception("PatchWindow load failed."); } _uiRoot = _handle.InstantiateObject; _manifest = _uiRoot.GetComponent <UIManifest>(); _slider = _manifest.GetUIComponent <Slider>("PatchWindow/UIWindow/Slider"); _tips = _manifest.GetUIComponent <Text>("PatchWindow/UIWindow/Slider/txt_tips"); _tips.text = "正在准备游戏世界......"; _messageBoxObj = _manifest.GetUIElement("PatchWindow/UIWindow/MessgeBox").gameObject; _messageBoxObj.SetActive(false); _eventGroup.AddListener <PatchEventMessageDefine.PatchStatesChange>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.FoundForceInstallAPP>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.FoundUpdateFiles>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.DownloadFilesProgress>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.GameVersionRequestFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebPatchManifestDownloadFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebFileDownloadFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebFileCheckFailed>(OnHandleEvent); }
private void Handle_Completed(AssetOperationHandle obj) { if (obj.AssetObject == null) { return; } _uiRoot = obj.InstantiateObject; _manifest = _uiRoot.GetComponent <UIManifest>(); _slider = _manifest.GetUIComponent <Slider>("PatchWindow/UIWindow/Slider"); _tips = _manifest.GetUIComponent <Text>("PatchWindow/UIWindow/Slider/txt_tips"); _messageBoxObj = _manifest.GetUIElement("PatchWindow/UIWindow/MessgeBox").gameObject; _messageBoxObj.SetActive(false); _eventGroup.AddListener <PatchEventMessageDefine.PatchStatesChange>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.FoundForceInstallAPP>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.FoundUpdateFiles>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.DownloadFilesProgress>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.GameVersionRequestFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebPatchManifestDownloadFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebFileDownloadFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebFileCheckFailed>(OnHandleEvent); SendOperationEvent(EPatchOperation.BeginingRequestGameVersion); }
private void OnUILoginLoad(Asset asset, EAssetResult result) { if (result != EAssetResult.OK) return; _uiLogin.GameObj.transform.SetParent(_uiDesktop, false); // 获取UIManifest组件 UIManifest manifest = _uiLogin.GameObj.GetComponent<UIManifest>(); // 获取UISprite组件 _loginSprite = manifest.GetComponent("UILogin/BtnLogin", "UISprite") as UISprite; // 通过配表数据设置文本 var hero1 = CfgHero.Instance.GetCfgTab(1001); var text1 = manifest.GetComponent("UILogin/Text1", "Text") as Text; text1.text = hero1.Name; Debug.Log($"热更新表格数据:{hero1.Name}"); // 通过配表数据设置文本 var hero2 = CfgHero.Instance.GetCfgTab(1002); var text2 = manifest.GetComponent("UILogin/Text2", "Text") as Text; text2.text = hero2.Name; Debug.Log($"热更新表格数据:{hero2.Name}"); // 监听按钮点击事件 Button btnLogin = manifest.GetComponent("UILogin/BtnLogin", "Button") as Button; btnLogin.onClick.AddListener(OnClickLogin); }
/// <summary> /// 刷新面板清单 /// </summary> public static void Refresh(UIManifest manifest) { if (UIPanelSettingData.CheckValid()) { CacheUIElement(manifest); UpdateUIComponent(manifest); } }
private void Handle_Completed(AssetOperationHandle obj) { if (_handle.AssetObject == null) { return; } Go = _handle.InstantiateObject; // 设置父类 GameObject uiDesktop = UIManager.Instance.UIDesktop; Go.transform.SetParent(uiDesktop.transform, false); // 获取组件 _manifest = Go.GetComponent <UIManifest>(); if (_manifest == null) { GameLogger.Error($"Not found {nameof(UIManifest)} in window {WindowType}"); return; } // 获取组件 _canvas = Go.GetComponent <Canvas>(); if (_canvas == null) { GameLogger.Error($"Not found {nameof(Canvas)} in window {WindowType}"); return; } _canvas.overrideSorting = true; // 获取组件 _raycaster = Go.GetComponent <GraphicRaycaster>(); if (_raycaster == null) { GameLogger.Error($"Not found {nameof(GraphicRaycaster)} in window {WindowType}"); return; } // 获取组件 _childCanvas = Go.GetComponentsInChildren <Canvas>(true); _childRaycaster = Go.GetComponentsInChildren <GraphicRaycaster>(true); // 虚函数 if (IsPrepare == false) { IsPrepare = true; OnCreate(); } // 最后设置是否激活 Go.SetActive(IsOpen); // 通知UI管理器 _userCallback?.Invoke(this); }
static void OnPrefabSaving(GameObject go) { PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage(); if (stage != null) { UIManifest manifest = go.GetComponent <UIManifest>(); if (manifest != null) { manifest.Refresh(); } } }
static void StartInitializeOnLoadMethod() { // 监听Inspector的Apply事件 PrefabUtility.prefabInstanceUpdated = delegate(GameObject go) { UIManifest manifest = go.GetComponent <UIManifest>(); if (manifest != null) { manifest.Refresh(); } }; #if UNITY_2018_4_OR_NEWER // 监听新的Prefab系统 PrefabStage.prefabSaving += OnPrefabSaving; #endif }
static void StartInitializeOnLoadMethod() { #if UNITY_2018_4_OR_NEWER // 监听新的Prefab系统 UnityEditor.Experimental.SceneManagement.PrefabStage.prefabSaving += OnPrefabSaving; #else // 监听Inspector的Apply事件 PrefabUtility.prefabInstanceUpdated = delegate(GameObject go) { UIManifest manifest = go.GetComponent <UIManifest>(); if (manifest != null) { UIPanelModifier.Refresh(manifest); } }; #endif }
/// <summary> /// 缓存所有UI元素 /// </summary> private static void CacheUIElement(UIManifest manifest) { Transform root = manifest.transform; // 清空旧数据 manifest.ElementPath.Clear(); manifest.ElementTrans.Clear(); Transform[] allTrans = root.GetComponentsInChildren <Transform>(true); for (int i = 0; i < allTrans.Length; i++) { Transform trans = allTrans[i]; string path = GetFullPath(root, trans); AddElementToList(manifest, path, trans); } Debug.Log($"Cache panel {root.name} total {allTrans.Length} elements"); }
/// <summary> /// 添加一个UI元素到列表 /// </summary> private static void AddElementToList(UIManifest manifest, string path, Transform trans) { if (string.IsNullOrEmpty(path) || trans == null) { throw new System.NullReferenceException(); } // 如果有重复路径的元素 for (int i = 0; i < manifest.ElementPath.Count; i++) { if (manifest.ElementPath[i] == path) { Debug.LogError($"发现重复路径的元素 : {path}"); return; } } manifest.ElementPath.Add(path); manifest.ElementTrans.Add(trans); }
/// <summary> /// 初始化 /// </summary> public void Initialize() { var prefab = Resources.Load <GameObject>("PatchWindow"); _uiRoot = GameObject.Instantiate(prefab); _manifest = _uiRoot.GetComponent <UIManifest>(); _slider = _manifest.GetUIComponent <Slider>("PatchWindow/UIWindow/Slider"); _tips = _manifest.GetUIComponent <Text>("PatchWindow/UIWindow/Slider/txt_tips"); _tips.text = "正在准备游戏世界......"; _messageBoxYesObj = _manifest.GetUIElement("PatchWindow/UIWindow/MessgeBox").gameObject; _messageBoxYesObj.SetActive(false); _eventGroup.AddListener <PatchEventMessageDefine.PatchStatesChange>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.FoundNewApp>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.FoundUpdateFiles>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.DownloadProgressUpdate>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.GameVersionRequestFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.GameVersionParseFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.PatchManifestRequestFailed>(OnHandleEvent); _eventGroup.AddListener <PatchEventMessageDefine.WebFileDownloadFailed>(OnHandleEvent); }
/// <summary> /// 资源准备完毕 /// </summary> protected override void OnAssetLoad(GameObject go) { // 获取组件 _manifest = go.GetComponent <UIManifest>(); if (_manifest == null) { throw new Exception($"Not found {nameof(UIManifest)} in panel {WindowName}"); } // 获取组件 _canvas = go.GetComponent <Canvas>(); if (_canvas == null) { throw new Exception($"Not found {nameof(Canvas)} in panel {WindowName}"); } _canvas.overrideSorting = true; _canvas.sortingOrder = 0; // 获取组件 _raycaster = go.GetComponent <GraphicRaycaster>(); _childCanvas = go.GetComponentsInChildren <Canvas>(true); _childRaycaster = go.GetComponentsInChildren <GraphicRaycaster>(true); }
/// <summary> /// 更新组件 /// </summary> private static void UpdateUIComponent(UIManifest manifest) { Transform root = manifest.transform; // Clear cache manifest.CacheAtlasTags.Clear(); // 获取依赖的图集名称 Image[] allImage = root.GetComponentsInChildren <Image>(true); for (int i = 0; i < allImage.Length; i++) { Image img = allImage[i]; // Clear UISprite uiSprite = img.GetComponent <UISprite>(); if (uiSprite != null) { uiSprite.Atlas = null; } // 如果图片为空 if (img.sprite == null) { continue; } // 文件路径 string assetPath = UnityEditor.AssetDatabase.GetAssetPath(img.sprite); // 如果是系统内置资源 if (assetPath.Contains("_builtin_")) { continue; } // 如果是图集资源 string spriteDirectory = UIPanelSettingData.Setting.UISpriteDirectory; if (assetPath.Contains(spriteDirectory)) { if (uiSprite == null) { uiSprite = img.gameObject.AddComponent <UISprite>(); } string atlasAssetPath = GetAtlasPath(assetPath); SpriteAtlas spriteAtlas = UnityEditor.AssetDatabase.LoadAssetAtPath <SpriteAtlas>(atlasAssetPath); if (spriteAtlas == null) { throw new System.Exception($"Not found SpriteAtlas : {atlasAssetPath}"); } else { uiSprite.Atlas = spriteAtlas; string atlasName = Path.GetFileNameWithoutExtension(atlasAssetPath); if (manifest.CacheAtlasTags.Contains(atlasName) == false) { manifest.CacheAtlasTags.Add(atlasName); } } } } }