コード例 #1
0
 static public int PostDoLayout(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         Ballance2.UI.BallanceUI.UILayout self = (Ballance2.UI.BallanceUI.UILayout)checkSelf(l);
         self.PostDoLayout();
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
コード例 #2
0
        private UILayout BuildLayoutByTemplateInternal(string name, XmlNode templateXml,
                                                       Dictionary <string, GameHandler> handlers,
                                                       UILayout parent, UILayout root, string[] initialProps)
        {
            //实例化UI预制体
            string     prefabName = templateXml.Name;
            GameObject prefab     = UIManager.FindRegisterElementPrefab(prefabName);

            if (prefab == null)
            {
                GameLogger.Log(TAG, "BuildLayoutByTemplate failed, not found prefab {0}", prefabName);
                GameErrorManager.LastError = GameError.PrefabNotFound;
                return(null);
            }

            //获取预制体上的脚本
            UILayout ilayout = prefab.GetComponent <UILayout>();

            if (ilayout == null) //该方法必须实例化UI容器
            {
                GameLogger.Error(TAG, "BuildLayoutByTemplate with prefab {0} failed, root must be a container", prefabName);
                GameErrorManager.LastError = GameError.MustBeContainer;
                return(null);
            }

            GameObject newCon = GameCloneUtils.CloneNewObjectWithParent(prefab,
                                                                        parent == null ? UIManager.UIRoot.transform : parent.RectTransform);

            ilayout = newCon.GetComponent <UILayout>();
            ilayout.DoCallStart();
            ilayout.LayoutLock();
            ilayout.RectTransform = newCon.GetComponent <RectTransform>();
            UIAnchorPosUtils.SetUIPivot(ilayout.RectTransform, UIPivot.TopCenter);
            ilayout.RectTransform.anchoredPosition = Vector2.zero;

            if (root == null)
            {
                root = ilayout;
                root.IsRootLayout = true;
            }

            GameObject newEle    = null;
            UIElement  uIElement = null;

            //子元素
            for (int i = 0, c = templateXml.ChildNodes.Count; i < c; i++)
            {
                string eleName = "";
                //xml 属性读取
                XmlNode eleNode = templateXml.ChildNodes[i];
                foreach (XmlAttribute a in eleNode.Attributes)
                {
                    if (a.Name.ToLower() == "name")
                    {
                        eleName = a.Value;
                    }
                }

                //预制体
                prefab = UIManager.FindRegisterElementPrefab(eleNode.Name);
                if (prefab == null)
                {
                    GameLogger.Error(TAG, "BuildLayoutByTemplate failed, not found prefab {0}", prefabName);
                    continue;
                }
                if (prefab.GetComponent <UILayout>() != null)                                                                    //这是UI容器
                {
                    UILayout newLayout = BuildLayoutByTemplateInternal(eleName, eleNode, handlers, ilayout, root, initialProps); //递归构建

                    uIElement = newLayout;
                    uIElement.rootContainer = root;
                    uIElement.Name          = eleName;
                }
                else
                {
                    //构建子元素
                    newEle = GameCloneUtils.CloneNewObjectWithParent(prefab, ilayout.RectTransform, eleName);

                    uIElement = newEle.GetComponent <UIElement>();
                    uIElement.RectTransform = newEle.GetComponent <RectTransform>();
                    uIElement.DoCallStart();
                    uIElement.Init(eleNode);
                    uIElement.rootContainer = root;
                    uIElement.Name          = eleName;
                }

                //初始化
                InitiazeChildElement(uIElement, handlers, initialProps);

                //添加元素
                ilayout.AddElement(uIElement, false);
            }

            ilayout.Init(templateXml); //容器的XML读取
            InitiazeChildElement(ilayout, handlers, initialProps);
            ilayout.LayoutUnLock();
            ilayout.PostDoLayout();

            if (root == ilayout)
            {
                ilayout.DoLayout();
            }

            return(ilayout);
        }