예제 #1
0
        public UINode DrawLayer(LayerInfo layer)
        {
            var    newRect = LayerImportUtil.AddSubRectFromResourceDic(layer.rect, layer.resourceDic);
            UINode node    = mechine.CreateRootNode(layer, newRect);

            node.AddComponent <UnityEngine.UI.Text>();
            return(node);
        }
예제 #2
0
        public void SetUIParams(UINode node)
        {
            if (node.layerInfo.resourceDic != null)
            {
                mechine.ChargeInfo(node.transform, node.layerInfo.resourceDic);
            }

            LayerImportUtil.LoadCommonResources(mechine, node);
        }
        public void SetUIParams(UINode node)
        {
            var pic = node.GetComponent <UnityEngine.UI.Image>();

            mechine.ChargeInfo(pic, node.layerInfo.resourceDic);

            var newRect = LayerImportUtil.AddSubRectFromResourceDic(node.layerInfo.rect, node.layerInfo.resourceDic);

            LayerImportUtil.LoadCommonResources(mechine, node, newRect);
        }
예제 #4
0
        /// <summary>
        /// 按资源的Rect设置拉伸的坐标
        /// </summary>
        /// <param name="rectTransform"></param>
        /// <param name="baseRect"></param>
        /// <param name="resourceDic"></param>
        public static void SetRectFromResource(this RectTransform rectTransform, Rect baseRect, ResourceDic resourceDic = null)
        {
            var rect = new Rect(Vector2.zero, baseRect.size);

            if (resourceDic != null)
            {
                LayerImportUtil.UpdateRectFromResourceDic(ref rect, resourceDic);
            }
            rect = ParamAnalysisTool.PsRectToUnityRect(baseRect.size, rect);
            LayerImportUtil.SetRectTransform(rect, rectTransform);
            LayerImportUtil.SetCustomAnchor(baseRect.size, rectTransform);
        }
        public void SetUIParams(UINode node)
        {
            var toggle = node.GetComponent <Toggle>();

            if (node.layerInfo.resourceDic != null)
            {
                mechine.ChargeInfo(toggle, node.layerInfo.resourceDic);
            }

            var update_targetGraphicRect = false;
            var update_graphicRect       = false;

            ///加载子项资源
            if (node.layerInfo.subResourceDic != null)
            {
                using (var enumerator = node.layerInfo.subResourceDic.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        var current = enumerator.Current;
                        if (current.Key == titleKey)
                        {
                            var title = LayerImportUtil.MakeTitleComponenet("Title", toggle.transform);
                            title.rectTransform.SetRectFromResource(node.layerInfo.rect, current.Value);
                            mechine.ChargeInfo(title, current.Value);
                        }
                        else if (current.Key == targetGraphicKey)
                        {
                            toggle.targetGraphic.rectTransform.SetRectFromResource(node.layerInfo.rect, current.Value);
                            update_targetGraphicRect = true;
                            mechine.ChargeInfo(toggle.targetGraphic, current.Value);
                        }
                        else if (current.Key == graphicKey)
                        {
                            toggle.graphic.rectTransform.SetRectFromResource(node.layerInfo.rect, current.Value);
                            update_graphicRect = true;
                            mechine.ChargeInfo(toggle.graphic, current.Value);
                        }
                    }
                }
            }

            if (!update_targetGraphicRect)
            {
                toggle.targetGraphic.rectTransform.SetRectFromResource(node.layerInfo.rect);
            }

            if (!update_graphicRect)
            {
                toggle.graphic.rectTransform.SetRectFromResource(node.layerInfo.rect);
            }
        }
        private void SetAnchorDeepth(UINode node)
        {
            if (node != null && node.childNodes != null)
            {
                for (int i = 0; i < node.childNodes.Count; i++)
                {
                    var childNode = node.childNodes[i];
                    SetAnchorDeepth(childNode);
                }
            }

            LayerImportUtil.SetAnchorByNode(node);
        }
예제 #7
0
        /// <summary>
        /// 按资源的Rect设置拉伸的坐标
        /// </summary>
        /// <param name="rectTransform"></param>
        /// <param name="rootRect"></param>
        /// <param name="baseRect"></param>
        /// <param name="resourceDic"></param>
        public static void SetRectFromResource(this RectTransform rectTransform, Rect rootRect, Rect baseRect, ResourceDic resourceDic = null)
        {
            var rect = new Rect(Vector2.zero, baseRect.size);

            if (resourceDic != null)
            {
                if (LayerImportUtil.UpdateRectFromResourceDic(ref rect, resourceDic))
                {
                    //得到相对于BaseRect的相对值
                    rect = new Rect(rect.x + rootRect.x - baseRect.x, rect.y + rootRect.y - baseRect.y, rect.width, rect.height);
                }
            }
            rect = ParamAnalysisTool.PsRectToUnityRect(baseRect.size, rect);
            LayerImportUtil.SetRectTransform(rect, rectTransform);
            LayerImportUtil.SetCustomAnchor(baseRect.size, rectTransform);
        }
예제 #8
0
        public void SetUIParams(UINode node)
        {
            var component = node.GetComponent <UnityEngine.UI.Text>();

            component.resizeTextForBestFit = true;//缩放时会看不到?
            if (PreferHelper.defultFont)
            {
                component.font = PreferHelper.defultFont;
            }

            mechine.ChargeInfo(component, node.layerInfo.resourceDic);

            var newRect = LayerImportUtil.AddSubRectFromResourceDic(node.layerInfo.rect, node.layerInfo.resourceDic);

            LayerImportUtil.LoadCommonResources(mechine, node, newRect);
        }
        /// <summary>
        /// 初始化Canvas
        /// </summary>
        /// <returns></returns>
        public static Canvas FindOrCreateCanvas()
        {
            Canvas canvas = null;

            if (Selection.activeTransform != null)
            {
                canvas = Selection.activeTransform.GetComponentInChildren <Canvas>();
            }
            if (canvas == null)
            {
                canvas = GameObject.FindObjectOfType <Canvas>();
            }
            if (canvas == null)
            {
                canvas = LayerImportUtil.CreateCanvas(PreferHelper.uiSize);
            }
            return(canvas);
        }
예제 #10
0
        /// <summary>
        /// 加载Image/Text/Label
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="layerInfo"></param>
        public static void LoadCommonResources(AssemblerStateMechine mechine, UINode node, Rect baseRect)
        {
            Transform parent    = node.transform;
            LayerInfo layerInfo = node.layerInfo;

            if (layerInfo.sub_images != null)
            {
                for (int i = 0; i < layerInfo.sub_images.Count; i++)
                {
                    var current = layerInfo.sub_images[i];
                    var image   = new GameObject("Image", typeof(RectTransform), typeof(Image)).GetComponent <Image>();
                    image.transform.SetParent(parent, false);
                    mechine.ChargeInfo(image, current);
                    image.rectTransform.SetRectFromResource(layerInfo.rect, baseRect, current);
                }
            }

            if (layerInfo.sub_texts != null)
            {
                for (int i = 0; i < layerInfo.sub_texts.Count; i++)
                {
                    var current = layerInfo.sub_texts[i];
                    var title   = LayerImportUtil.MakeTitleComponenet("Title", parent);
                    title.resizeTextForBestFit = true;//缩放时会看不到?
                    mechine.ChargeInfo(title, current);
                    title.rectTransform.SetRectFromResource(layerInfo.rect, baseRect, current);
                }
            }

            if (layerInfo.sub_rawImages != null)
            {
                for (int i = 0; i < layerInfo.sub_rawImages.Count; i++)
                {
                    var current  = layerInfo.sub_rawImages[i];
                    var rawImage = new GameObject("RawImage", typeof(RectTransform), typeof(RawImage)).GetComponent <RawImage>();
                    rawImage.transform.SetParent(parent, false);
                    mechine.ChargeInfo(rawImage, current);
                    rawImage.rectTransform.SetRectFromResource(layerInfo.rect, baseRect, current);
                }
            }
        }
        public void SetUIParams(UINode node)
        {
            var slider          = node.GetComponent <Slider>();
            var backgroundImage = node.GetSubComponent <Image>(runtime_key_background);

            ///加载Slider属性
            if (node.layerInfo.resourceDic != null)
            {
                mechine.ChargeInfo(slider, node.layerInfo.resourceDic);
            }

            var         fillRect        = node.GetSubComponent <RectTransform>(runtime_key_fill);
            ResourceDic fillResourceDic = null;

            ///加载子项资源
            if (node.layerInfo.subResourceDic != null)
            {
                using (var enumerator = node.layerInfo.subResourceDic.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        var current = enumerator.Current;
                        if (current.Key == runtime_key_background)//动态背景
                        {
                            backgroundImage.rectTransform.SetRectFromResource(node.layerInfo.rect, current.Value);
                            mechine.ChargeInfo(backgroundImage, current.Value);
                        }
                        else if (current.Key == runtime_key_fill)
                        {
                            var key = current.Key;
                            mechine.ChargeInfo(slider.fillRect.GetComponent <Image>(), current.Value);

                            fillResourceDic = current.Value;
                        }
                        else if (current.Key == runtime_key_handle)
                        {
                            var handleArea = new GameObject("Handle Slide Area", typeof(RectTransform)).GetComponent <RectTransform>();
                            handleArea.transform.SetParent(slider.transform, false);

                            var handle = new GameObject("Handle", typeof(Image)).GetComponent <RectTransform>();
                            handle.transform.SetParent(handleArea.transform, false);

                            slider.handleRect = handle.GetComponent <RectTransform>();
                            mechine.ChargeInfo(slider.handleRect.GetComponent <Image>(), current.Value);

                            Vector2 handleSize = new Vector2(node.layerInfo.rect.height, node.layerInfo.rect.height);
                            LayerImportUtil.UpdateSizeFromResourceDic(current.Value, ref handleSize);

                            if (slider.direction == Slider.Direction.BottomToTop || slider.direction == Slider.Direction.TopToBottom)
                            {
                                var handleRectX    = (node.layerInfo.rect.width - handleSize.x) * 0.5f;
                                var handleAreaRect = new Rect(handleRectX, handleSize.y * 0.5f, handleSize.x, node.layerInfo.rect.height - handleSize.y);
                                handleAreaRect = ParamAnalysisTool.PsRectToUnityRect(node.layerInfo.rect.size, handleAreaRect);
                                LayerImportUtil.SetRectTransform(handleAreaRect, handleArea);
                                LayerImportUtil.SetCustomAnchor(node.layerInfo.rect.size, handleArea);

                                handle.anchorMin = Vector2.zero;
                                handle.anchorMax = new Vector2(1, 0);
                                handle.sizeDelta = new Vector2(0, handleSize.y);
                            }
                            else
                            {
                                var handleRectY    = (node.layerInfo.rect.height - handleSize.y) * 0.5f;
                                var handleAreaRect = new Rect(handleSize.x * 0.5f, handleRectY, node.layerInfo.rect.width - handleSize.x, handleSize.y);
                                handleAreaRect = ParamAnalysisTool.PsRectToUnityRect(node.layerInfo.rect.size, handleAreaRect);
                                LayerImportUtil.SetRectTransform(handleAreaRect, handleArea);
                                LayerImportUtil.SetCustomAnchor(node.layerInfo.rect.size, handleArea);

                                handle.anchorMin = Vector2.zero;
                                handle.anchorMax = new Vector2(0, 1);
                                handle.sizeDelta = new Vector2(handleSize.x, 0);
                            }
                        }
                        else
                        {
                            mechine.ChargeSubInfo(slider, current.Key, current.Value);
                        }
                    }
                }
            }

            fillRect.SetRectFromResource(node.layerInfo.rect, fillResourceDic);
            slider.fillRect.anchorMin = Vector2.zero;
            slider.fillRect.anchorMax = Vector2.one;
            slider.fillRect.sizeDelta = Vector2.zero;

            LayerImportUtil.LoadCommonResources(mechine, node);
            slider.value = slider.minValue;
        }
        public void SetUIParams(UINode node)
        {
            var buttonTrans = node.transform;

            ///加载子项资源
            if (node.layerInfo.subResourceDic != null)
            {
                using (var enumerator = node.layerInfo.subResourceDic.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        var current = enumerator.Current;
                        if (current.Key == runtime_key_text)//文字按扭
                        {
                            var text = buttonTrans.gameObject.GetComponent <Text>();
                            if (text == null && buttonTrans.gameObject.GetComponent <Graphic>() == null)
                            {
                                text           = buttonTrans.gameObject.AddComponent <Text>();
                                text.alignment = TextAnchor.MiddleCenter;
                                text.font      = PreferHelper.defultFont;
                                var btn = node.MustComponent <Button>();
                                btn.targetGraphic = text;
                                mechine.ChargeInfo(text, current.Value);
                            }
                        }
                        else if (current.Key == runtime_key_image && buttonTrans.gameObject.GetComponent <Graphic>() == null)//图片按扭
                        {
                            var image = buttonTrans.gameObject.GetComponent <Image>();
                            if (image == null)
                            {
                                image = buttonTrans.gameObject.AddComponent <Image>();
                                var btn = node.MustComponent <Button>();
                                btn.targetGraphic = image;
                                mechine.ChargeInfo(image, current.Value);
                            }
                        }
                        else
                        {
                            var key = current.Key;
                            mechine.ChargeSubInfo(buttonTrans, key, current.Value);
                        }
                    }
                }
            }
            var button = node.MustComponent <Button>();

            ///加载Button属性
            if (node.layerInfo.resourceDic != null)
            {
                mechine.ChargeInfo(button, node.layerInfo.resourceDic);
            }

            ///将有图片的Button显示方式设置为SpriteSwap (优先)
            if (button.spriteState.highlightedSprite || button.spriteState.pressedSprite || button.spriteState.disabledSprite)
            {
                button.transition = Selectable.Transition.SpriteSwap;
            }

            ///加载公式资源
            LayerImportUtil.LoadCommonResources(mechine, node);
        }
        private void MakeRelation(UINode rootNode, UINode[] uiNodes)
        {
            if (rootNode == null || uiNodes == null)
            {
                return;
            }
            var pathCatchDic    = new Dictionary <string, string[]>();
            var nodeTemplateDic = new Dictionary <string, UINode>();
            var deepthDic       = new Dictionary <int, List <UINode> >();
            var maxdeepth       = 0;

            ///建立索引
            for (int i = 0; i < uiNodes.Length; i++)
            {
                var node = uiNodes[i];
                nodeTemplateDic.Add(node.path, node);
                var pathArray = LayerImportUtil.PathToArray(node.path);
                pathCatchDic.Add(node.path, pathArray);

                var deepth = pathArray.Length;

                if (deepthDic.ContainsKey(deepth))
                {
                    deepthDic[deepth].Add(node);
                }
                else
                {
                    deepthDic[deepth] = new List <UINode>()
                    {
                        node
                    };
                }

                maxdeepth = maxdeepth > deepth ? maxdeepth : deepth;
            }

            ///关系对应
            for (int deepth = 1; deepth <= maxdeepth; deepth++)
            {
                var nodes = deepthDic[deepth];
                for (int k = 0; k < nodes.Count; k++)
                {
                    var node = nodes[k];
                    var path = pathCatchDic[node.path];

                    var parentPath = LayerImportUtil.ArrayToPath(path, deepth - 1);

                    if (string.IsNullOrEmpty(parentPath))
                    {
                        rootNode.AddChildNode(node);
                    }
                    else if (nodeTemplateDic.ContainsKey(parentPath))
                    {
                        var nodeTemp = nodeTemplateDic[parentPath];
                        nodeTemp.AddChildNode(node);
                    }
                    else
                    {
                        Debug.LogWarning("未找到层级:" + parentPath);
                    }
                }
            }
            //设置父级
            SetUIParentsDeepth(rootNode);
        }
        private UINode[] CompleteEmptyNodes(UINode[] uiNodes)
        {
            var pathCatchDic    = new Dictionary <string, string[]>();
            var nodeTemplateDic = new Dictionary <string, UINode>();
            var neededNodes     = new Dictionary <string, List <UINode> >();
            var emptyNodes      = new List <UINode>();
            var maxdeepth       = 0;

            ///确定深度,记录索引
            for (int i = 0; i < uiNodes.Length; i++)
            {
                var node = uiNodes[i];

                if (string.IsNullOrEmpty(node.path))
                {
                    continue;
                }

                nodeTemplateDic.Add(node.path, node);
                var pathArray = LayerImportUtil.PathToArray(node.path);
                pathCatchDic.Add(node.path, pathArray);

                var deepth = pathArray.Length;
                maxdeepth = maxdeepth > deepth ? maxdeepth : deepth;
            }

            ///查找所有空节点的实体子节点
            for (int i = 0; i < uiNodes.Length; i++)
            {
                var node = uiNodes[i];

                if (string.IsNullOrEmpty(node.path))
                {
                    continue;
                }

                var path = pathCatchDic[node.path];

                for (int j = path.Length; j > 0; j--)
                {
                    var parentPath = LayerImportUtil.ArrayToPath(path, j - 1);
                    if (!string.IsNullOrEmpty(parentPath))
                    {
                        if (!nodeTemplateDic.ContainsKey(parentPath))
                        {
                            if (neededNodes.ContainsKey(parentPath))
                            {
                                if (!neededNodes[parentPath].Contains(node))
                                {
                                    neededNodes[parentPath].Add(node);
                                }
                            }
                            else
                            {
                                neededNodes.Add(parentPath, new List <UINode>()
                                {
                                    node
                                });
                            }
                        }
                    }
                }
            }

            ///创建节点
            using (var enumerator = neededNodes.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    var current = enumerator.Current;
                    var rects   = new Rect[current.Value.Count];
                    for (int i = 0; i < rects.Length; i++)
                    {
                        rects[i] = current.Value[i].layerInfo.rect;
                    }
                    var info = new LayerInfo();
                    info.path = current.Key;
                    info.rect = LayerImportUtil.GetRectContent(rects);
                    var emptyNode = DrawLayer(info);
                    emptyNodes.Add(emptyNode);
                }
            }

            return(emptyNodes.ToArray());
        }