/// <summary> /// UI GameObject销毁倒计时 /// </summary> public IEnumerator Destroy() { float timeCount = 0; while (true) { yield return(null); if (UIState.Closed == UiInstance.State) { timeCount += Time.deltaTime; if (timeCount >= ActiveTime) { //销毁UiInstance UiInstance.Destroy(); UiInstance = null; ActiveTime = BaseInfo.defaultActiveTime; UiAsset.RefCount--; if (UiAsset.RefCount <= 0) { //开启ui asset销毁倒计时 UIRes.Instance.StartCoroutine(UiAsset.Destroy()); UiAsset = null; } yield break; } } else { yield break; } } }
/// <summary> /// 单例释放 /// </summary> /// <param name="instance"></param> public static void OnDestroyInManager(this UiInstance instance) { if (InstDic.ContainsKey(instance.GetType())) { InstDic.Remove(instance.GetType()); } }
/// <summary> /// 显示界面 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="notify"></param> public static void Show <T>(Action <T> notify = null) where T : UiInstance { var uiInfo = GetUiInfo <T>(); if (uiInfo == null) { Debug.LogError("UI未注册,请在XUI_LIST.cs中注册:" + typeof(T).FullName); return; } //单例UI if (uiInfo.LoadType == UiLoadType.Single) { ShowInstanceUi <T>(uiInfo, false, notify); } //非单例 else { UiInstance.LoadUi(uiInfo, uiInfo.ViewPath, typeof(T), inst => { inst.UiInfo = uiInfo; ShowUiView(inst, uiInfo.Layer); if (notify != null) { notify.Invoke(inst as T); } inst.AfterOnShow(); }); } }
private static void ShowInstanceUi <T>(UiInfo uiInfo, bool fromBack, Action <T> notify = null, Type classType = null) where T : UiInstance { Type typeKey = classType ?? typeof(T); //存在缓存 if (InstDic.ContainsKey(typeKey)) { UiInstance inst = InstDic[typeKey]; AfterLoadInstance(inst, uiInfo, fromBack, notify); } else { //已经在加载中 bool bLoading; if (InstLoadState.TryGetValue(uiInfo.ViewPath, out bLoading) && bLoading) { //do noting } else { //加载 InstLoadState[uiInfo.ViewPath] = true; UiInstance.LoadUi(uiInfo, uiInfo.ViewPath, typeKey, inst => { InstLoadState[uiInfo.ViewPath] = false; InstDic[typeKey] = inst; AfterLoadInstance(inst, uiInfo, fromBack, notify); }); } } }
public static void ShowUIFromBack(Type type, Action <UiInstance> notify) { var uiInfo = GetUiInfo(type); if (uiInfo == null) { Debug.LogError("UI未注册,请在XUI_LIST.cs中注册:" + type.FullName); return; } //单例UI if (uiInfo.LoadType == UiLoadType.Single) { ShowInstanceUi(uiInfo, true, notify); } //非单例 else { UiInstance.LoadUi(uiInfo, uiInfo.ViewPath, type, inst => { inst.UiInfo = uiInfo; ShowUiView(inst, uiInfo.Layer); if (notify != null) { notify.Invoke(inst); } inst.AfterOnShow(true); }); } }
private static void AfterLoadInstance <T>(UiInstance inst, UiInfo uiInfo, bool fromBack, Action <T> notify) where T : UiInstance { inst.UiInfo = uiInfo; ShowUiView(inst, uiInfo.Layer); inst.AfterOnShow(fromBack); if (notify != null) { notify.Invoke(inst as T); } }
private static T CacheShowInstanceUi <T>(UiInfo uiInfo, bool fromBack, Type classType = null) where T : UiInstance { Type typeKey = classType ?? typeof(T); //存在缓存 if (InstDic.ContainsKey(typeKey)) { UiInstance inst = InstDic[typeKey]; if (inst != null) { AfterLoadInstanceNoCallBack <T>(inst, fromBack, uiInfo); return(inst as T); } else { Debug.LogError("存在缓存情况 CacheShowInstanceUi inst is null, :" + uiInfo.ViewPath); } } else { //已经在加载中 bool bLoading; if (InstLoadState.TryGetValue(uiInfo.ViewPath, out bLoading) && bLoading) { //do noting } else { //加载 InstLoadState[uiInfo.ViewPath] = true; var inst = UiInstance.ShowImmediately(uiInfo.ViewPath, typeKey); if (inst != null) { InstLoadState[uiInfo.ViewPath] = false; InstDic[typeKey] = inst; AfterLoadInstanceNoCallBack <T>(inst, fromBack, uiInfo); return(inst as T); } else { Debug.LogError("新加载情况 CacheShowInstanceUi inst is null, :" + uiInfo.ViewPath); } } } return(null); }
private static void ShowUiView(UiInstance view, UiLayer layerName) { if (view == null) { Debug.LogError("ShowUiView View Is null, layer:" + layerName); return; } string layer = Enum.GetName(typeof(UiLayer), layerName); var parent = _uiBind.CanvasParentList[layerName]; if (parent == null) { Debug.LogError("ShowUIView 层级错误,使用了存在不存在的层:" + layer); return; } view.transform.SetParent(parent, false); view.transform.SetAsLastSibling(); view.gameObject.SetActiveSafe(true); }
public static void LoadUi(UiInfo uiInfo, string resource, Type typeUi, Action <UiInstance> notify) { ResourceMgr.Load(resource, obj => { ResourceMgr.InstantiateX(obj, uiObject => { uiObject.SetActiveSafe(true); var script = GameObjectTools.AddComponent(uiObject, typeUi); UiInstance ui = script as UiInstance; if (ui) { ui.UiInfo = uiInfo; ui.OnLoadFinish(); notify(ui); } else { Debug.LogError(string.Format("Load UI fail ui == null resource{0}", resource)); } }); }); }
public static UiInstance ShowImmediately(string resource, Type typeUi) { var obj = ResourceMgr.LoadImmediately(resource); var uiObject = ResourceMgr.Instantiate(obj); if (uiObject) { uiObject.SetActiveSafe(true); var script = GameObjectTools.AddComponent(uiObject, typeUi); UiInstance ui = script as UiInstance; if (ui) { ui.OnLoadFinish(); return(ui); } Debug.LogError(string.Format("Load UI fail ui == null resource{0}", resource)); } else { Debug.LogError(string.Format("Load UI fail gameObject == null resource{0}", resource)); } return(null); }
/// <summary> /// 关闭窗口 /// </summary> /// <param name="instance"></param> /// <param name="notify"></param> public static void Close(this UiInstance instance, Action notify = null) { if (instance == null) { return; } instance.OnClose(inst => { if (notify != null) { notify.Invoke(); } //如果是单例UI,则隐藏 if (inst.UiInfo.LoadType == UiLoadType.Single) { inst.gameObject.SetActiveSafe(false); } else { Object.Destroy(inst.gameObject); } }); }
/// <summary> /// 显示界面 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="notify"></param> public static T ImmediatelyShow <T>(bool fromBack = false) where T : UiInstance { var uiInfo = GetUiInfo <T>(); if (uiInfo == null) { Debug.LogError("UI未注册,请在XUI_LIST.cs中注册:" + typeof(T).FullName); return(null); } //单例UI if (uiInfo.LoadType == UiLoadType.Single) { return(CacheShowInstanceUi <T>(uiInfo, fromBack)); } //非单例 var inst = UiInstance.ShowImmediately(uiInfo.ViewPath, typeof(T)); inst.UiInfo = uiInfo; ShowUiView(inst, uiInfo.Layer); inst.AfterOnShow(fromBack); return(inst as T); }
public void Execute(string command) { if (command.StartsWith("rotate ")) { string[] split = command.Split(' '); Vector3D axis = new Vector3D(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3])); Quaternion q = new Quaternion(axis, double.Parse(split[4])); var tmpl = UiStates.Selection.SelectedTemplate; if (!tmpl.IsNull) { UiInstance.Rotate(tmpl, q, tmpl.Center); } else { Console.WriteLine("No template selected."); } } else if (command.StartsWith("translate ")) { string[] split = command.Split(' '); Vector3D trans = double.Parse(split[4]) * new Vector3D(double.Parse(split[1]), double.Parse(split[2]), double.Parse(split[3])); var tmpl = UiStates.Selection.SelectedTemplate; if (!tmpl.IsNull) { UiInstance.Translate(tmpl, trans); } else { Console.WriteLine("No template selected."); } } else if (command.StartsWith("scad example")) { UiInstance.makeExampleScadTemplate(); } else if (command == "reflect on") { OverlapPreventionHack.SetAlwaysReflect(true); } else if (command == "reflect off") { OverlapPreventionHack.SetAlwaysReflect(false); } else if (command == "test label selection") { JointLayout layout = new JointLayout(); layout.RobotShape = new List <Point> { new Point(0, 10), new Point(10, 10), new Point(10, 0), new Point(0, 0) }; layout.Joints = new List <Point> { new Point(0, 10), new Point(10, 10), new Point(10, 0), new Point(0, 0) }; List <JointLabeling> labelings = new List <JointLabeling> { new JointLabeling { JointLabels = new List <int> { 1, 3, 2, 4 } }, new JointLabeling { JointLabels = new List <int> { 1, 2, 2, 4 } }, new JointLabeling { JointLabels = new List <int> { 1, 1, 2, 4 } }, new JointLabeling { JointLabels = new List <int> { 1, 1, 3, 4 } } }; //MainWindow.configurationChooser.Choose(UiInstance, layout, labelings, 1, 40); } else if (command.StartsWith("add constraint ")) { // Example: // add constraint 2(#0) + -3(#1) + (#3) = 0 // Means that // Suppose the selected template has parameters defined as: // param #0 = width, param #1 = height, param #2 = depth, param #3 = radius // Then this means 2 * width - 3 * height + radius = 0 Regex regex = new Regex("^(.*)([=<>])(.+)$"); string exprWithoutSpaces = command.Substring("add constraint ".Length).Replace(" ", ""); Match match = regex.Match(exprWithoutSpaces); if (match.Success) { string expr = match.Groups[1].Value; string oper = match.Groups[2].Value; string constant = match.Groups[3].Value; string[] terms = expr.Split('+'); Regex termRegex = new Regex(@"^(.*)[(]#(\d+)[)]$"); try { Tuple <double, int>[] parsedTerms = terms.Select(term => { Match termMatch = termRegex.Match(term); if (termMatch.Success) { string coeffStr = termMatch.Groups[1].Value; string indexStr = termMatch.Groups[2].Value; double coeff = coeffStr == "" ? 1 : coeffStr == "-" ? -1 : double.Parse(coeffStr); int index = int.Parse(indexStr); return(Tuple.Create(coeff, index)); } else { throw new ArgumentException("Invalid term: " + term); } }).ToArray(); bool isEquality = oper == "="; double constantTerm = double.Parse(constant); if (oper == ">") { parsedTerms = parsedTerms.Select(t => Tuple.Create(-t.Item1, t.Item2)).ToArray(); constantTerm = -constantTerm; } TemplateRef selectedTemplate = UiStates.Selection.SelectedTemplate; if (selectedTemplate.IsNull) { MessageBox.Show("Must select a template first."); return; } selectedTemplate.AddConstraint(parsedTerms, constantTerm, isEquality); } catch (ArgumentException ex) { MessageBox.Show(ex.Message); return; } catch (FormatException ex) { MessageBox.Show(ex.Message); return; } } else { MessageBox.Show("Invalid command"); } } }
private static void AfterLoadInstanceNoCallBack <T>(UiInstance inst, bool fromBack, UiInfo uiInfo) where T : UiInstance { inst.UiInfo = uiInfo; ShowUiView(inst, uiInfo.Layer); inst.AfterOnShow(fromBack); }