void OnGUI() { m_uiName = EditorGUILayout.TextField("UI Name:", m_uiName); // if (GUILayout.Button("查找UI")) { if (null == m_uiInfos) { return; } UIAssetInfo uiInfo = null; foreach (UIAssetInfo info in m_uiInfos) { if (info.uiID == m_uiName) { uiInfo = info; break; } } if (null == uiInfo) { EditorUtility.DisplayDialog("提示", "查无此UI", "确定"); return; } GameObject uiInstance = GameObject.Instantiate(Resources.Load("UI/Prefabs/" + uiInfo.uiID)) as GameObject; GameObject canvas = UIToolsEditor.GetCanvas(); uiInstance.transform.SetParent(canvas.transform, false); uiInstance.GetComponent <RectTransform>().sizeDelta = uiInfo.sizeDelta; uiInstance.GetComponent <RectTransform>().anchoredPosition3D = uiInfo.anchoredPosition3D; uiInstance.name = uiInfo.uiID; Debug.LogWarning("克隆成功"); } // for (int i = 0; i < m_uiInfos.Length; ++i) { var infoItem = m_uiInfos[i]; EditorGUILayout.BeginHorizontal(); GUILayout.Label("Name=" + infoItem.uiID); if (GUILayout.Button("Load")) { GameObject uiInstance = GameObject.Instantiate(Resources.Load("UI/Prefabs/" + infoItem.uiID)) as GameObject; GameObject canvas = UIToolsEditor.GetCanvas(); uiInstance.transform.SetParent(canvas.transform, false); uiInstance.GetComponent <RectTransform>().sizeDelta = infoItem.sizeDelta; uiInstance.GetComponent <RectTransform>().anchoredPosition3D = infoItem.anchoredPosition3D; uiInstance.name = infoItem.uiID; Debug.LogWarning("克隆成功"); } EditorGUILayout.EndHorizontal(); } }
void OnGUI() { m_uiInstance = EditorGUILayout.ObjectField("UI Instance", m_uiInstance, typeof(BaseView), true) as BaseView; if (GUILayout.Button("加入UI")) { #region 加入UI if (null == m_uiInstance) { Debug.LogError("UI 为Null"); return; } Directory.CreateDirectory(Application.dataPath + "/Resources/UI/UIAssetInfos"); UIAssetInfo[] infos = Resources.LoadAll <UIAssetInfo>("UI/UIAssetInfos"); if (null != infos) { foreach (UIAssetInfo info in infos) { if (info.uiID == m_uiInstance.GetUiID()) { if (EditorUtility.DisplayDialog("提示", "是否更新UI", "OK")) { break; } return; } } } UIAssetInfo infoInstance = ScriptableObject.CreateInstance <UIAssetInfo>(); infoInstance.uiID = m_uiInstance.GetUiID(); RectTransform rectTrans = m_uiInstance.GetComponent <RectTransform>(); infoInstance.anchoredPosition3D = rectTrans.anchoredPosition3D; infoInstance.sizeDelta = rectTrans.sizeDelta; AssetDatabase.CreateAsset(infoInstance, "Assets/Resources/UI/UIAssetInfos" + "/" + infoInstance.uiID + ".asset"); //调用导出回调 var uiScriptType = m_uiInstance.GetType(); MethodInfo[] methodInfos = uiScriptType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (null != methodInfos || methodInfos.Length > 0) { foreach (MethodInfo methodInfo_Item in methodInfos) { if (methodInfo_Item.IsDefined(typeof(ExportRecoverAttribute), false)) { methodInfo_Item.Invoke(m_uiInstance, null); } } } UIPage[] pages = m_uiInstance.GetComponentsInChildren <UIPage>(true); if (null != pages && pages.Length > 0) { foreach (UIPage pageInstance in pages) { var pageScriptType = pageInstance.GetType(); MethodInfo[] methodInfos2 = pageScriptType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (null != methodInfos2 || methodInfos2.Length > 0) { foreach (MethodInfo methodInfo_Item in methodInfos2) { if (methodInfo_Item.IsDefined(typeof(ExportRecoverAttribute), false)) { methodInfo_Item.Invoke(pageInstance, null); } } } pageInstance.gameObject.name = pageScriptType.Name; pageInstance.gameObject.SetActive(false); } } // m_uiInstance.name = m_uiInstance.GetUiID(); m_uiInstance.gameObject.SetActive(false); //保存预设 Directory.CreateDirectory(Application.dataPath + "/Resources/UI/Prefabs"); PrefabUtility.CreatePrefab( UIToolsEditor.prefabPathAtEditor + "/" + m_uiInstance.GetUiID() + ".prefab", m_uiInstance.gameObject, ReplacePrefabOptions.Default); PrefabUtility.DisconnectPrefabInstance(m_uiInstance); AssetDatabase.Refresh(); Debug.LogWarning("加入UI成功"); #endregion } if (GUILayout.Button("移除UI")) { #region 移除UI if (null == m_uiInstance) { Debug.LogError("UI 为Null"); return; } Directory.CreateDirectory(Application.dataPath + "/Resources/UI/UIAssetInfos"); UIAssetInfo[] infos = Resources.LoadAll <UIAssetInfo>("UI/UIAssetInfos"); if (null != infos) { foreach (UIAssetInfo info in infos) { if (info.uiID == m_uiInstance.GetUiID()) { AssetDatabase.DeleteAsset("Assets/Resources/UI/UIAssetInfos" + "/" + info.uiID + ".asset"); AssetDatabase.Refresh(); break; } } } try { AssetDatabase.DeleteAsset(UIToolsEditor.prefabPathAtEditor + "/" + m_uiInstance.GetUiID() + ".prefab"); } catch { }; AssetDatabase.Refresh(); Debug.LogWarning("删除UI成功"); #endregion } }
public UIElement(UIAssetInfo info) { BaseInfo = info; ActiveTime = BaseInfo.defaultActiveTime; }
protected override void Init() { m_uiElements = new Dictionary <string, UIElement>(); m_assets = new Dictionary <string, AssetInformation>(); try { UIAssetInfo[] infos = Resources.LoadAll <UIAssetInfo>("UI/UIAssetInfos"); for (int i = 0; i < infos.Length; ++i) { UIAssetInfo info = infos[i]; if (!m_uiElements.ContainsKey(info.uiID)) { m_uiElements.Add(info.uiID, new UIElement(info)); } } } catch { } m_canvasObject = GameObject.Find("Canvas"); m_eventSystemObject = GameObject.Find("EventSystem"); if (null == m_canvasObject) { #region 自动创建Cavas代码 m_canvasObject = new GameObject("Canvas"); int uiLayer = LayerMask.NameToLayer("UI"); m_canvasObject.layer = uiLayer; Canvas canvasComponent = m_canvasObject.AddComponent <Canvas>(); canvasComponent.renderMode = RenderMode.ScreenSpaceCamera; // Camera uiCamera = new GameObject("UICamera").AddComponent <Camera>(); uiCamera.transform.SetParent(m_canvasObject.transform, false); uiCamera.gameObject.layer = m_canvasObject.layer; canvasComponent.worldCamera = uiCamera; //设置UI相机 uiCamera.transform.Translate(Vector3.back * 100); uiCamera.clearFlags = CameraClearFlags.Depth; uiCamera.cullingMask = 0x01 << uiLayer; uiCamera.orthographic = true; uiCamera.useOcclusionCulling = false; #if Patch_5_6X uiCamera.allowHDR = false; uiCamera.allowMSAA = false; #endif //设置屏幕适配 CanvasScaler scalerComponent = m_canvasObject.AddComponent <CanvasScaler>(); scalerComponent.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scalerComponent.referenceResolution = new Vector2(Screen.width, Screen.height); scalerComponent.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; scalerComponent.matchWidthOrHeight = 1; // m_canvasObject.AddComponent <GraphicRaycaster>(); // if (null == m_eventSystemObject) { m_eventSystemObject = new GameObject("EventSystem"); m_eventSystemObject.AddComponent <EventSystem>(); m_eventSystemObject.AddComponent <StandaloneInputModule>(); } #endregion } GameObject.DontDestroyOnLoad(m_canvasObject); GameObject.DontDestroyOnLoad(m_eventSystemObject); }