Esempio n. 1
0
        /// <summary>
        /// Scrolls immediately to a given object in the scrollview.
        ///
        /// <b>Important:</b>
        /// Unity UI's scrolling component and UI setup is complex and full of variations. Therefore this
        /// function may not always give the expected results. Simply try it. If it does not work, you may copy the
        /// implementation of this function into your own code and customize as needed.
        /// </summary>
        /// <param name="scrollRect">ScrollView that should perform the scrolling.</param>
        /// <param name="focusObject">Object to scroll to. Must be a child of scrollRect's content object.</param>
        /// <param name="keepX">If true, scrolling does not apply to X.</param>
        /// <param name="keepY">If true, scrolling does not apply to Y.</param>
        /// <seealso cref="https://stackoverflow.com/questions/30766020/how-to-scroll-to-a-specific-element-in-scrollrect-with-unity-ui"/>
        public static void scrollJumpTo(UnityEngine.UI.ScrollRect scrollRect, GameObject focusObject, bool keepX = false, bool keepY = false)
        {
            RectTransform contentPanel = scrollRect.content;
            RectTransform target       = focusObject.GetComponent <RectTransform>();

            Canvas.ForceUpdateCanvases();

            Vector2 result =
                (Vector2)scrollRect.transform.InverseTransformPoint(contentPanel.position)        //current scrollpos
                - (
                    (Vector2)scrollRect.transform.InverseTransformPoint((Vector2)target.position) //... pos of object
                    );

            result.x += (scrollRect.GetComponent <RectTransform>().rect.width / 2);  //try to scroll object into center
            result.y -= (scrollRect.GetComponent <RectTransform>().rect.height / 2); //try to scroll object into center
                                                                                     //Debug.Log("scrollRect.rect="+(scrollRect.GetComponent<RectTransform>().rect)+", result="+result);

            result.x = Mathf.Clamp(result.x, 0, contentPanel.rect.width);
            result.y = Mathf.Clamp(result.y, -contentPanel.rect.height, 0);

            if (keepX)
            {
                result.x = contentPanel.anchoredPosition.x;
            }
            if (keepY)
            {
                result.y = contentPanel.anchoredPosition.y;
            }

            //scrollRect.inertia = false;
            contentPanel.anchoredPosition = result;
            scrollRect.StopMovement(); //Important: Stop any flow that may come from inertia, because it will falsify the programmatic scrolling results here (as we want a hard but reliable jump to a fixed position)
            //Debug.Log("-> result=" + result +", c.ap="+ contentPanel.anchoredPosition+"c.rs="+contentPanel.rect.height);
        }
Esempio n. 2
0
        public void DrawLayer(Layer layer, GameObject parent)
        {
            //UnityEngine.UI.ScrollRect temp = AssetDatabase.LoadAssetAtPath(PSDImporterConst.PREFAB_PATH_SCROLLVIEW, typeof(UnityEngine.UI.ScrollRect)) as UnityEngine.UI.ScrollRect;
            UnityEngine.UI.ScrollRect scrollRect = PSDImportUtility.LoadAndInstant <UnityEngine.UI.ScrollRect>(PSDImporterConst.ASSET_PATH_SCROLLVIEW, layer.name, parent);
            //scrollRect.transform.SetParent(parent.transform, false); //parent = parent.transform;


            RectTransform rectTransform = scrollRect.GetComponent <RectTransform>();

            rectTransform.sizeDelta        = new Vector2(layer.size.width, layer.size.height);
            rectTransform.anchoredPosition = new Vector2(layer.position.x, layer.position.y);

            if (layer.layers != null)
            {
                string type = layer.arguments[0].ToUpper();
                switch (type)
                {
                case "V":
                    //scrollRect.vertical = true;
                    //scrollRect.horizontal = false;
                    if (layer.arguments.Length > 4)
                    {
                        BuildGridScrollView(scrollRect, layer, parent, true);
                        return;
                    }
                    else
                    {
                        BuildVerticalScrollView(scrollRect, layer);
                    }
                    break;

                case "H":
                    //scrollRect.vertical = false;
                    //scrollRect.horizontal = true;
                    if (layer.arguments.Length > 4)
                    {
                        BuildGridScrollView(scrollRect, layer, parent, false);
                        return;
                    }
                    else
                    {
                        BuildHorizonScrollView(scrollRect, layer);
                    }
                    break;

                default:
                    break;
                }

                ctrl.DrawLayers(layer.layers, scrollRect.content.gameObject);
            }
        }
        public UINode DrawLayer(Layer layer, UINode parent)
        {
            UINode node = PSDImportUtility.InstantiateItem(PSDImporterConst.PREFAB_PATH_SCROLLVIEW, layer.name, parent);

            UnityEngine.UI.ScrollRect scrollRect = node.InitComponent <UnityEngine.UI.ScrollRect>();

            UINode childNode = PSDImportUtility.InstantiateItem(PSDImporterConst.PREFAB_PATH_IMAGE, "Viewport", node);

            scrollRect.viewport = childNode.InitComponent <RectTransform>();
            Color color;

            if (ColorUtility.TryParseHtmlString("#FFFFFF01", out color))
            {
                childNode.InitComponent <UnityEngine.UI.Image>().color = color;
                Debug.Log(color);
            }
            childNode.InitComponent <Mask>();
            childNode.anchoType = UINode.AnchoType.XStretch | UINode.AnchoType.YStretch;

            bool havebg = false;

            for (int i = 0; i < layer.images.Length; i++)
            {
                Image image = layer.images[i];

                if (image.name.ToLower().StartsWith("b_"))
                {
                    havebg = true;
                    UnityEngine.UI.Image graph = node.InitComponent <UnityEngine.UI.Image>();

                    PSDImportUtility.SetPictureOrLoadColor(image, graph);

                    PSDImportUtility.SetRectTransform(image, scrollRect.GetComponent <RectTransform>());
                }
                else
                {
                    ctrl.DrawImage(image, node);
                }
            }

            if (!havebg)
            {
                PSDImportUtility.SetRectTransform(layer, scrollRect.GetComponent <RectTransform>(), parent.InitComponent <RectTransform>());
            }

            PSDImportUtility.SetRectTransform(layer, childNode.InitComponent <RectTransform>(), scrollRect.GetComponent <RectTransform>());

            if (layer.arguments != null)
            {
                string type = layer.arguments[0].ToUpper();
                switch (type)
                {
                case "V":
                    scrollRect.vertical   = true;
                    scrollRect.horizontal = false;
                    break;

                case "H":
                    scrollRect.vertical   = false;
                    scrollRect.horizontal = true;
                    break;

                case "VH":
                case "HV":
                    scrollRect.vertical   = true;
                    scrollRect.horizontal = true;
                    break;

                default:
                    break;
                }
            }

            if (layer.layers != null)
            {
                for (int i = 0; i < layer.layers.Length; i++)
                {
                    Layer  child          = layer.layers[i];
                    string childLowerName = child.name;
                    UINode c_Node         = ctrl.DrawLayer(child, childNode);

                    if (childLowerName.StartsWith("c_"))
                    {
                        scrollRect.content = c_Node.InitComponent <RectTransform>();
                    }
                    else if (childLowerName.StartsWith("vb_"))
                    {
                        scrollRect.verticalScrollbar           = c_Node.InitComponent <Scrollbar>();
                        scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
                    }
                    else if (childLowerName.StartsWith("hb_"))
                    {
                        scrollRect.horizontalScrollbar           = c_Node.InitComponent <Scrollbar>();
                        scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
                    }
                }
            }
            return(node);
        }
        public UGUINode DrawLayer(GroupNode layer, UGUINode parent)
        {
            UGUINode node = PSDImportUtility.InstantiateItem(GroupType.SCROLLVIEW, layer.Name, parent);

            UnityEngine.UI.ScrollRect scrollRect = node.InitComponent <UnityEngine.UI.ScrollRect>();

            UGUINode childNode = PSDImportUtility.InstantiateItem(GroupType.IMAGE, "Viewport", node);

            scrollRect.viewport = childNode.InitComponent <RectTransform>();
            Color color;

            if (ColorUtility.TryParseHtmlString("#FFFFFF01", out color))
            {
                childNode.InitComponent <UnityEngine.UI.Image>().color = color;
            }
            childNode.InitComponent <Mask>();
            childNode.anchoType = AnchoType.XStretch | AnchoType.YStretch;

            bool havebg = false;

            for (int i = 0; i < layer.images.Count; i++)
            {
                ImgNode image = layer.images[i];

                if (image.Name.ToLower().StartsWith("b_"))
                {
                    havebg = true;
                    UnityEngine.UI.Image graph = node.InitComponent <UnityEngine.UI.Image>();

                    PSDImportUtility.SetPictureOrLoadColor(image, graph);

                    PSDImportUtility.SetRectTransform(image, scrollRect.GetComponent <RectTransform>());
                }
                else
                {
                    ctrl.DrawImage(image, node);
                }
            }

            if (!havebg)
            {
                PSDImportUtility.SetRectTransform(layer, scrollRect.GetComponent <RectTransform>());
            }

            PSDImportUtility.SetRectTransform(layer, childNode.InitComponent <RectTransform>());

            switch (layer.direction)
            {
            case Direction.Horizontal:
                scrollRect.vertical   = true;
                scrollRect.horizontal = false;
                break;

            case Direction.Vertical:
                scrollRect.vertical   = false;
                scrollRect.horizontal = true;
                break;

            case Direction.Horizontal | Direction.Vertical:
                scrollRect.vertical   = true;
                scrollRect.horizontal = true;
                break;

            default:
                break;
            }


            if (layer.groups != null)
            {
                for (int i = 0; i < layer.groups.Count; i++)
                {
                    GroupNode child          = layer.groups[i];
                    string    childLowerName = child.Name;
                    UGUINode  c_Node         = ctrl.DrawLayer(child, childNode);

                    if (childLowerName.StartsWith("c_"))
                    {
                        scrollRect.content = c_Node.InitComponent <RectTransform>();
                    }
                    else if (childLowerName.StartsWith("vb_"))
                    {
                        scrollRect.verticalScrollbar           = c_Node.InitComponent <Scrollbar>();
                        scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
                    }
                    else if (childLowerName.StartsWith("hb_"))
                    {
                        scrollRect.horizontalScrollbar           = c_Node.InitComponent <Scrollbar>();
                        scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
                    }
                }
            }
            return(node);
        }
Esempio n. 5
0
 private void Awake()
 {
     //ListItems = new List<GameObject>();
     ScrollRectTransform = ScrollRect.GetComponent <RectTransform>();
 }