Exemple #1
0
        public virtual bool Raycast(Vector2 sp, Camera eventCamera)
        {
            if (!isActiveAndEnabled)
            {
                return(false);
            }

            var t          = transform;
            var components = ListPool <Component> .Get();

            bool ignoreParentGroups = false;
            bool continueTraversal  = true;

            while (t != null)
            {
                t.GetComponents(components);
                for (var i = 0; i < components.Count; i++)
                {
                    var canvas = components[i] as Canvas;
                    if (canvas != null && canvas.overrideSorting)
                    {
                        continueTraversal = false;
                    }

                    var filter = components[i] as ICanvasRaycastFilter;

                    if (filter == null)
                    {
                        continue;
                    }

                    var raycastValid = true;

                    var group = components[i] as CanvasGroup;
                    if (group != null)
                    {
                        if (ignoreParentGroups == false && group.ignoreParentGroups)
                        {
                            ignoreParentGroups = true;
                            raycastValid       = filter.IsRaycastLocationValid(sp, eventCamera);
                        }
                        else if (!ignoreParentGroups)
                        {
                            raycastValid = filter.IsRaycastLocationValid(sp, eventCamera);
                        }
                    }
                    else
                    {
                        raycastValid = filter.IsRaycastLocationValid(sp, eventCamera);
                    }

                    if (!raycastValid)
                    {
                        ListPool <Component> .Release(components);

                        return(false);
                    }
                }
                t = continueTraversal ? t.parent : null;
            }
            ListPool <Component> .Release(components);

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Show the dropdown.
        ///
        /// Plan for dropdown scrolling to ensure dropdown is contained within screen.
        ///
        /// We assume the Canvas is the screen that the dropdown must be kept inside.
        /// This is always valid for screen space canvas modes.
        /// For world space canvases we don't know how it's used, but it could be e.g. for an in-game monitor.
        /// We consider it a fair constraint that the canvas must be big enough to contain dropdowns.
        /// </summary>
        public void Show()
        {
            if (!IsActive() || !IsInteractable() || m_Dropdown != null)
            {
                return;
            }

            // Get root Canvas.
            var list = ListPool <Canvas> .Get();

            gameObject.GetComponentsInParent(false, list);
            if (list.Count == 0)
            {
                return;
            }

            // case 1064466 rootCanvas should be last element returned by GetComponentsInParent()
            var    listCount  = list.Count;
            Canvas rootCanvas = list[listCount - 1];

            for (int i = 0; i < listCount; i++)
            {
                if (list[i].isRootCanvas || list[i].overrideSorting)
                {
                    rootCanvas = list[i];
                    break;
                }
            }

            ListPool <Canvas> .Release(list);

            if (!validTemplate)
            {
                SetupTemplate(rootCanvas);
                if (!validTemplate)
                {
                    return;
                }
            }

            m_Template.gameObject.SetActive(true);


            // Instantiate the drop-down template
            m_Dropdown      = CreateDropdownList(m_Template.gameObject);
            m_Dropdown.name = "Dropdown List";
            m_Dropdown.SetActive(true);

            // Make drop-down RectTransform have same values as original.
            RectTransform dropdownRectTransform = m_Dropdown.transform as RectTransform;

            dropdownRectTransform.SetParent(m_Template.transform.parent, false);

            // Instantiate the drop-down list items

            // Find the dropdown item and disable it.
            DropdownItem itemTemplate = m_Dropdown.GetComponentInChildren <DropdownItem>();

            GameObject    content = itemTemplate.rectTransform.parent.gameObject;
            RectTransform contentRectTransform = content.transform as RectTransform;

            itemTemplate.rectTransform.gameObject.SetActive(true);

            // Get the rects of the dropdown and item
            Rect dropdownContentRect = contentRectTransform.rect;
            Rect itemTemplateRect    = itemTemplate.rectTransform.rect;

            // Calculate the visual offset between the item's edges and the background's edges
            Vector2 offsetMin = itemTemplateRect.min - dropdownContentRect.min + (Vector2)itemTemplate.rectTransform.localPosition;
            Vector2 offsetMax = itemTemplateRect.max - dropdownContentRect.max + (Vector2)itemTemplate.rectTransform.localPosition;
            Vector2 itemSize  = itemTemplateRect.size;

            m_Items.Clear();

            Toggle prev         = null;
            var    optionsCount = options.Count;

            for (int i = 0; i < optionsCount; ++i)
            {
                OptionData   data = options[i];
                DropdownItem item = AddItem(data, value == i, itemTemplate, m_Items);
                if (item == null)
                {
                    continue;
                }

                // Automatically set up a toggle state change listener
                item.toggle.isOn = value == i;
                item.toggle.onValueChanged.AddListener(x => OnSelectItem(item.toggle));

                // Select current option
                if (item.toggle.isOn)
                {
                    item.toggle.Select();
                }

                // Automatically set up explicit navigation
                if (prev != null)
                {
                    Navigation prevNav   = prev.navigation;
                    Navigation toggleNav = item.toggle.navigation;
                    prevNav.mode   = Navigation.Mode.Explicit;
                    toggleNav.mode = Navigation.Mode.Explicit;

                    prevNav.selectOnDown   = item.toggle;
                    prevNav.selectOnRight  = item.toggle;
                    toggleNav.selectOnLeft = prev;
                    toggleNav.selectOnUp   = prev;

                    prev.navigation        = prevNav;
                    item.toggle.navigation = toggleNav;
                }
                prev = item.toggle;
            }

            // Reposition all items now that all of them have been added
            Vector2 sizeDelta = contentRectTransform.sizeDelta;

            sizeDelta.y = itemSize.y * m_Items.Count + offsetMin.y - offsetMax.y;
            contentRectTransform.sizeDelta = sizeDelta;

            float extraSpace = dropdownRectTransform.rect.height - contentRectTransform.rect.height;

            if (extraSpace > 0)
            {
                dropdownRectTransform.sizeDelta = new Vector2(dropdownRectTransform.sizeDelta.x, dropdownRectTransform.sizeDelta.y - extraSpace);
            }

            // Invert anchoring and position if dropdown is partially or fully outside of canvas rect.
            // Typically this will have the effect of placing the dropdown above the button instead of below,
            // but it works as inversion regardless of initial setup.
            Vector3[] corners = new Vector3[4];
            dropdownRectTransform.GetWorldCorners(corners);

            RectTransform rootCanvasRectTransform = rootCanvas.transform as RectTransform;
            Rect          rootCanvasRect          = rootCanvasRectTransform.rect;

            for (int axis = 0; axis < 2; axis++)
            {
                bool outside = false;
                for (int i = 0; i < 4; i++)
                {
                    Vector3 corner = rootCanvasRectTransform.InverseTransformPoint(corners[i]);
                    if ((corner[axis] < rootCanvasRect.min[axis] && !Mathf.Approximately(corner[axis], rootCanvasRect.min[axis])) ||
                        (corner[axis] > rootCanvasRect.max[axis] && !Mathf.Approximately(corner[axis], rootCanvasRect.max[axis])))
                    {
                        outside = true;
                        break;
                    }
                }
                if (outside)
                {
                    RectTransformUtility.FlipLayoutOnAxis(dropdownRectTransform, axis, false, false);
                }
            }

            var itemsCount = m_Items.Count;

            for (int i = 0; i < itemsCount; i++)
            {
                RectTransform itemRect = m_Items[i].rectTransform;
                itemRect.anchorMin        = new Vector2(itemRect.anchorMin.x, 0);
                itemRect.anchorMax        = new Vector2(itemRect.anchorMax.x, 0);
                itemRect.anchoredPosition = new Vector2(itemRect.anchoredPosition.x, offsetMin.y + itemSize.y * (itemsCount - 1 - i) + itemSize.y * itemRect.pivot.y);
                itemRect.sizeDelta        = new Vector2(itemRect.sizeDelta.x, itemSize.y);
            }

            // Fade in the popup
            AlphaFadeList(m_AlphaFadeSpeed, 0f, 1f);

            // Make drop-down template and item template inactive
            m_Template.gameObject.SetActive(false);
            itemTemplate.gameObject.SetActive(false);

            m_Blocker = CreateBlocker(rootCanvas);
        }
Exemple #3
0
        // Show the dropdown.
        //
        // Plan for dropdown scrolling to ensure dropdown is contained within screen.
        //
        // We assume the Canvas is the screen that the dropdown must be kept inside.
        // This is always valid for screen space canvas modes.
        // For world space canvases we don't know how it's used, but it could be e.g. for an in-game monitor.
        // We consider it a fair constraint that the canvas must be big enough to contains dropdowns.
        public void Show()
        {
            if (!IsActive() || !IsInteractable() || m_Dropdown != null)
            {
                return;
            }

            //初始状态时validTemplate为false来触发对于列表模板的初始化设置
            if (!validTemplate)
            {
                //模板初始化方法:检测并设置模板,初始化模板绑定相关组件并调整模板UI层级,若没有通过检查则模板标记为不可用状态。
                SetupTemplate();
                //若检测不通过则无法正常显示下拉列表
                if (!validTemplate)
                {
                    return;
                }
            }

            // Get root Canvas.
            var list = ListPool <Canvas> .Get();

            gameObject.GetComponentsInParent(false, list);
            if (list.Count == 0)
            {
                return;
            }
            //获取父级路径下最近的canvas
            Canvas rootCanvas = list[0];

            ListPool <Canvas> .Release(list);

            //显示模板准备复制列表
            m_Template.gameObject.SetActive(true);

            // Instantiate the drop-down template
            //复制列表模板
            m_Dropdown = CreateDropdownList(m_Template.gameObject);
            //进行改名
            m_Dropdown.name = "Dropdown List";
            m_Dropdown.SetActive(true);

            // Make drop-down RectTransform have same values as original.
            // 设置新的列表模板的父级
            RectTransform dropdownRectTransform = m_Dropdown.transform as RectTransform;

            dropdownRectTransform.SetParent(m_Template.transform.parent, false);

            // Instantiate the drop-down list items
            // 创建列表Item
            // Find the dropdown item and disable it.
            DropdownItem itemTemplate = m_Dropdown.GetComponentInChildren <DropdownItem>();

            GameObject    content = itemTemplate.rectTransform.parent.gameObject;
            RectTransform contentRectTransform = content.transform as RectTransform;

            itemTemplate.rectTransform.gameObject.SetActive(true);

            // Get the rects of the dropdown and item
            Rect dropdownContentRect = contentRectTransform.rect;
            Rect itemTemplateRect    = itemTemplate.rectTransform.rect;

            // Calculate the visual offset between the item's edges and the background's edges
            //计算Item与背景边界的偏移量
            Vector2 offsetMin = itemTemplateRect.min - dropdownContentRect.min + (Vector2)itemTemplate.rectTransform.localPosition;
            Vector2 offsetMax = itemTemplateRect.max - dropdownContentRect.max + (Vector2)itemTemplate.rectTransform.localPosition;
            Vector2 itemSize  = itemTemplateRect.size;

            //清空DropdownItem List 准备开始选项Itme的创建
            m_Items.Clear();

            Toggle prev = null;

            for (int i = 0; i < options.Count; ++i)
            {
                OptionData data = options[i];
                //创建Item
                DropdownItem item = AddItem(data, value == i, itemTemplate, m_Items);
                if (item == null)
                {
                    continue;
                }

                // Automatically set up a toggle state change listener
                // 设置toggle初始状态以及注册事件监听
                item.toggle.isOn = value == i;
                item.toggle.onValueChanged.AddListener(x => OnSelectItem(item.toggle));

                // Select current option
                //标记当前选项
                if (item.toggle.isOn)
                {
                    item.toggle.Select();
                }

                // Automatically set up explicit navigation
                // 设置Item的导航
                if (prev != null)
                {
                    Navigation prevNav   = prev.navigation;
                    Navigation toggleNav = item.toggle.navigation;
                    prevNav.mode   = Navigation.Mode.Explicit;
                    toggleNav.mode = Navigation.Mode.Explicit;

                    prevNav.selectOnDown   = item.toggle;
                    prevNav.selectOnRight  = item.toggle;
                    toggleNav.selectOnLeft = prev;
                    toggleNav.selectOnUp   = prev;

                    prev.navigation        = prevNav;
                    item.toggle.navigation = toggleNav;
                }
                prev = item.toggle;
            }

            // Reposition all items now that all of them have been added
            // 计算内容区域的高度
            Vector2 sizeDelta = contentRectTransform.sizeDelta;

            sizeDelta.y = itemSize.y * m_Items.Count + offsetMin.y - offsetMax.y;
            contentRectTransform.sizeDelta = sizeDelta;

            //计算是否有额外空区域(当内容区域小于列表本身的区域时调整列表大小)
            float extraSpace = dropdownRectTransform.rect.height - contentRectTransform.rect.height;

            if (extraSpace > 0)
            {
                dropdownRectTransform.sizeDelta = new Vector2(dropdownRectTransform.sizeDelta.x, dropdownRectTransform.sizeDelta.y - extraSpace);
            }

            // Invert anchoring and position if dropdown is partially or fully outside of canvas rect.
            // Typically this will have the effect of placing the dropdown above the button instead of below,
            // but it works as inversion regardless of initial setup.
            // 当列表处于canvas外部时,将其按坐标轴进行翻转
            Vector3[] corners = new Vector3[4];
            dropdownRectTransform.GetWorldCorners(corners);

            RectTransform rootCanvasRectTransform = rootCanvas.transform as RectTransform;
            Rect          rootCanvasRect          = rootCanvasRectTransform.rect;

            for (int axis = 0; axis < 2; axis++)
            {
                bool outside = false;
                for (int i = 0; i < 4; i++)
                {
                    Vector3 corner = rootCanvasRectTransform.InverseTransformPoint(corners[i]);
                    if (corner[axis] < rootCanvasRect.min[axis] || corner[axis] > rootCanvasRect.max[axis])
                    {
                        outside = true;
                        break;
                    }
                }
                if (outside)
                {
                    RectTransformUtility.FlipLayoutOnAxis(dropdownRectTransform, axis, false, false);
                }
            }

            for (int i = 0; i < m_Items.Count; i++)
            {
                RectTransform itemRect = m_Items[i].rectTransform;
                itemRect.anchorMin        = new Vector2(itemRect.anchorMin.x, 0);
                itemRect.anchorMax        = new Vector2(itemRect.anchorMax.x, 0);
                itemRect.anchoredPosition = new Vector2(itemRect.anchoredPosition.x, offsetMin.y + itemSize.y * (m_Items.Count - 1 - i) + itemSize.y * itemRect.pivot.y);
                itemRect.sizeDelta        = new Vector2(itemRect.sizeDelta.x, itemSize.y);
            }

            // Fade in the popup
            // 下拉列表渐出效果
            AlphaFadeList(0.15f, 0f, 1f);

            // Make drop-down template and item template inactive
            // 隐藏模板
            m_Template.gameObject.SetActive(false);
            itemTemplate.gameObject.SetActive(false);
            // 创建拦截模板,用于监听点击事件来隐藏下拉列表,层级会低于下拉列表(2999)
            m_Blocker = CreateBlocker(rootCanvas);
        }
Exemple #4
0
        public virtual bool Raycast(Vector2 sp, Camera eventCamera)
        {
            bool result;

            if (!base.isActiveAndEnabled)
            {
                result = false;
            }
            else
            {
                Transform        transform = base.transform;
                List <Component> list      = ListPool <Component> .Get();

                bool flag  = false;
                bool flag2 = true;
                while (transform != null)
                {
                    transform.GetComponents <Component>(list);
                    for (int i = 0; i < list.Count; i++)
                    {
                        Canvas canvas = list[i] as Canvas;
                        if (canvas != null && canvas.overrideSorting)
                        {
                            flag2 = false;
                        }
                        ICanvasRaycastFilter canvasRaycastFilter = list[i] as ICanvasRaycastFilter;
                        if (canvasRaycastFilter != null)
                        {
                            bool        flag3       = true;
                            CanvasGroup canvasGroup = list[i] as CanvasGroup;
                            if (canvasGroup != null)
                            {
                                if (!flag && canvasGroup.ignoreParentGroups)
                                {
                                    flag  = true;
                                    flag3 = canvasRaycastFilter.IsRaycastLocationValid(sp, eventCamera);
                                }
                                else if (!flag)
                                {
                                    flag3 = canvasRaycastFilter.IsRaycastLocationValid(sp, eventCamera);
                                }
                            }
                            else
                            {
                                flag3 = canvasRaycastFilter.IsRaycastLocationValid(sp, eventCamera);
                            }
                            if (!flag3)
                            {
                                ListPool <Component> .Release(list);

                                result = false;
                                return(result);
                            }
                        }
                    }
                    transform = ((!flag2) ? null : transform.parent);
                }
                ListPool <Component> .Release(list);

                result = true;
            }
            return(result);
        }
Exemple #5
0
        private void OnCombineMesh()
        {
            RichText[]  texts     = GetComponentsInChildren <RichText>(false);
            RichImage[] images    = GetComponentsInChildren <RichImage>(false);
            var         meshCount = texts.Length + images.Length;

            if (meshCount == 0)
            {
                if (m_mesh)
                {
                    m_mesh.Clear();
                }
                if (m_meshRender)
                {
                    m_meshRender.enabled = false;
                }
                return;
            }

            if (m_meshRender == null)
            {
                m_meshRender           = gameObject.GetOrAddComponent <MeshRenderer>();
                m_meshRender.hideFlags = MeshHideflags;

                m_meshFilter           = gameObject.GetOrAddComponent <MeshFilter>();
                m_meshFilter.hideFlags = MeshHideflags;

                m_mesh = new Mesh();
                m_mesh.MarkDynamic();
                m_mesh.hideFlags = MeshHideflags;
            }

            Material material = null;

            var meshes = ListPool <MeshOrder> .Get();

            var worldToLocalMatrix = this.transform.worldToLocalMatrix;

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

                if (!image.IsActive())
                {
                    continue;
                }

                var mesh = image.Mesh();
                if (mesh == null)
                {
                    continue;
                }

                var meshOrder = new MeshOrder();
                meshOrder.mesh   = mesh;
                meshOrder.matrix = worldToLocalMatrix * image.transform.localToWorldMatrix;
                meshOrder.z      = image.transform.localPosition.z;

                meshes.Add(meshOrder);

                if (material == null)
                {
                    material = image.material;
                }
            }
            for (int j = 0; j < texts.Length; ++j)
            {
                var text = texts[j];

                if (!text.IsActive())
                {
                    continue;
                }

                var mesh = text.Mesh();
                if (mesh == null)
                {
                    continue;
                }

                var meshOrder = new MeshOrder();
                meshOrder.mesh   = mesh;
                meshOrder.matrix = worldToLocalMatrix * text.transform.localToWorldMatrix;
                meshOrder.z      = text.transform.localPosition.z;

                meshes.Add(meshOrder);

                if (material == null)
                {
                    material = text.material;
                }
            }

            if (meshes.Count == 0)
            {
                if (m_meshRender)
                {
                    m_meshRender.enabled = false;
                }
                return;
            }
            m_meshRender.enabled = true;

            meshes.Sort((lhs, rhs) => rhs.z.CompareTo(lhs.z));

            CombineInstance[] combine = new CombineInstance[meshes.Count];
            for (int i = 0; i < meshes.Count; ++i)
            {
                combine[i].mesh      = meshes[i].mesh;
                combine[i].transform = meshes[i].matrix;
            }

            ListPool <MeshOrder> .Release(meshes);

            m_mesh.CombineMeshes(combine, true);
            m_meshFilter.sharedMesh     = m_mesh;
            m_meshRender.sharedMaterial = material;
        }
Exemple #6
0
        public void Show()
        {
            if (this.IsActive() && this.IsInteractable() && !(this.m_Dropdown != null))
            {
                if (!this.validTemplate)
                {
                    this.SetupTemplate();
                    if (!this.validTemplate)
                    {
                        return;
                    }
                }
                List <Canvas> list = ListPool <Canvas> .Get();

                base.gameObject.GetComponentsInParent <Canvas>(false, list);
                if (list.Count != 0)
                {
                    Canvas canvas = list[0];
                    ListPool <Canvas> .Release(list);

                    this.m_Template.gameObject.SetActive(true);
                    this.m_Dropdown      = this.CreateDropdownList(this.m_Template.gameObject);
                    this.m_Dropdown.name = "Dropdown List";
                    this.m_Dropdown.SetActive(true);
                    RectTransform rectTransform = this.m_Dropdown.transform as RectTransform;
                    rectTransform.SetParent(this.m_Template.transform.parent, false);
                    Dropdown.DropdownItem componentInChildren = this.m_Dropdown.GetComponentInChildren <Dropdown.DropdownItem>();
                    GameObject            gameObject          = componentInChildren.rectTransform.parent.gameObject;
                    RectTransform         rectTransform2      = gameObject.transform as RectTransform;
                    componentInChildren.rectTransform.gameObject.SetActive(true);
                    Rect    rect    = rectTransform2.rect;
                    Rect    rect2   = componentInChildren.rectTransform.rect;
                    Vector2 vector  = rect2.min - rect.min + componentInChildren.rectTransform.localPosition;
                    Vector2 vector2 = rect2.max - rect.max + componentInChildren.rectTransform.localPosition;
                    Vector2 size    = rect2.size;
                    this.m_Items.Clear();
                    Toggle toggle = null;
                    for (int i = 0; i < this.options.Count; i++)
                    {
                        Dropdown.OptionData   data = this.options[i];
                        Dropdown.DropdownItem item = this.AddItem(data, this.value == i, componentInChildren, this.m_Items);
                        if (!(item == null))
                        {
                            item.toggle.isOn = (this.value == i);
                            item.toggle.onValueChanged.AddListener(delegate(bool x)
                            {
                                this.OnSelectItem(item.toggle);
                            });
                            if (item.toggle.isOn)
                            {
                                item.toggle.Select();
                            }
                            if (toggle != null)
                            {
                                Navigation navigation  = toggle.navigation;
                                Navigation navigation2 = item.toggle.navigation;
                                navigation.mode          = Navigation.Mode.Explicit;
                                navigation2.mode         = Navigation.Mode.Explicit;
                                navigation.selectOnDown  = item.toggle;
                                navigation.selectOnRight = item.toggle;
                                navigation2.selectOnLeft = toggle;
                                navigation2.selectOnUp   = toggle;
                                toggle.navigation        = navigation;
                                item.toggle.navigation   = navigation2;
                            }
                            toggle = item.toggle;
                        }
                    }
                    Vector2 sizeDelta = rectTransform2.sizeDelta;
                    sizeDelta.y = size.y * (float)this.m_Items.Count + vector.y - vector2.y;
                    rectTransform2.sizeDelta = sizeDelta;
                    float num = rectTransform.rect.height - rectTransform2.rect.height;
                    if (num > 0f)
                    {
                        rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, rectTransform.sizeDelta.y - num);
                    }
                    Vector3[] array = new Vector3[4];
                    rectTransform.GetWorldCorners(array);
                    RectTransform rectTransform3 = canvas.transform as RectTransform;
                    Rect          rect3          = rectTransform3.rect;
                    for (int j = 0; j < 2; j++)
                    {
                        bool flag = false;
                        for (int k = 0; k < 4; k++)
                        {
                            Vector3 vector3 = rectTransform3.InverseTransformPoint(array[k]);
                            if (vector3[j] < rect3.min[j] || vector3[j] > rect3.max[j])
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag)
                        {
                            RectTransformUtility.FlipLayoutOnAxis(rectTransform, j, false, false);
                        }
                    }
                    for (int l = 0; l < this.m_Items.Count; l++)
                    {
                        RectTransform rectTransform4 = this.m_Items[l].rectTransform;
                        rectTransform4.anchorMin        = new Vector2(rectTransform4.anchorMin.x, 0f);
                        rectTransform4.anchorMax        = new Vector2(rectTransform4.anchorMax.x, 0f);
                        rectTransform4.anchoredPosition = new Vector2(rectTransform4.anchoredPosition.x, vector.y + size.y * (float)(this.m_Items.Count - 1 - l) + size.y * rectTransform4.pivot.y);
                        rectTransform4.sizeDelta        = new Vector2(rectTransform4.sizeDelta.x, size.y);
                    }
                    this.AlphaFadeList(0.15f, 0f, 1f);
                    this.m_Template.gameObject.SetActive(false);
                    componentInChildren.gameObject.SetActive(false);
                    this.m_Blocker = this.CreateBlocker(canvas);
                }
            }
        }