/// <summary>
        /// Creates a custom dialog that can be modified or stored before showing.
        /// </summary>
        /// <typeparam name="T">The type of dialog to show, must inherit from <see cref="MaterialDialog"/>.</typeparam>
        /// <param name="dialogPrefabPath">The path to the dialog prefab.</param>
        /// <returns>The instance of the created dialog.</returns>
        public static T CreateCustomDialog <T>(string dialogPrefabPath) where T : MaterialDialog
        {
            T dialog = PrefabManager.InstantiateGameObject(dialogPrefabPath, instance.transform).GetComponent <T>();

            return(dialog);
        }
Example #2
0
        /// <summary>
        /// Instantiates the dropdown object and expands it.
        /// </summary>
        public void Show()
        {
            Canvas rootCanvas = m_BaseTransform.GetRootCanvas();

            rootCanvas.CopySettingsToOtherCanvas(m_DropdownCanvas);
            m_DropdownCanvas.pixelPerfect = true;
            m_DropdownCanvas.sortingOrder = 30000;

            m_DropdownPanel       = PrefabManager.InstantiateGameObject(PrefabManager.ResourcePrefabs.dropdownPanel, m_DropdownCanvas.transform).GetComponent <RectTransform>();
            m_PanelLayer          = m_DropdownPanel.GetChildByName <RectTransform>("PanelLayer");
            m_DropdownCanvasGroup = m_DropdownPanel.GetComponent <CanvasGroup>();
            m_ShadowCanvasGroup   = m_DropdownPanel.GetChildByName <CanvasGroup>("Shadow");

            m_DropdownPanel.GetRootCanvas().scaleFactor = rootCanvas.scaleFactor;

            m_CancelPanel           = m_DropdownPanel.GetChildByName <RectTransform>("Cancel Panel");
            m_CancelPanel.sizeDelta = transform.root.GetComponent <Canvas> ().pixelRect.size * 2;           // CHANGED FROM: scaler.targetCanvas.pixelRect.size * 2;
            DropdownTrigger trigger = m_DropdownPanel.gameObject.GetChildByName <DropdownTrigger>("Cancel Panel");

            trigger.index    = -1;
            trigger.dropdown = this;

            m_DropdownPanel.gameObject.GetChildByName <Image>("ScrollRect").color = m_PanelColor;

            m_ListItemTemplate = new DropdownListItem();

            m_ListItemTemplate.rectTransform = m_DropdownPanel.GetChildByName <RectTransform>("Item");
            m_ListItemTemplate.canvasGroup   = m_ListItemTemplate.rectTransform.GetComponent <CanvasGroup>();
            m_ListItemTemplate.text          = m_ListItemTemplate.rectTransform.GetChildByName <Text>("Text");

            if (m_OptionDataList.imageType == ImageDataType.Sprite)
            {
                m_ListItemTemplate.image = m_ListItemTemplate.rectTransform.GetChildByName <Image>("Icon");
                Destroy(m_ListItemTemplate.rectTransform.GetChildByName <VectorImage>("Icon").gameObject);
            }
            else
            {
                m_ListItemTemplate.image = m_ListItemTemplate.rectTransform.GetChildByName <VectorImage>("Icon");
                Destroy(m_ListItemTemplate.rectTransform.GetChildByName <Image>("Icon").gameObject);
            }

            m_ListItems = new List <DropdownListItem>();

            for (int i = 0; i < m_OptionDataList.options.Count; i++)
            {
                m_ListItems.Add(CreateItem(m_OptionDataList.options[i], i));
            }

            for (int i = 0; i < m_ListItems.Count; i++)
            {
                Selectable selectable = m_ListItems[i].rectTransform.GetComponent <Selectable>();
                Navigation navigation = new Navigation();
                navigation.mode = Navigation.Mode.Explicit;

                if (i > 0)
                {
                    navigation.selectOnUp = m_ListItems[i - 1].rectTransform.GetComponent <Selectable>();
                }
                if (i < m_ListItems.Count - 1)
                {
                    navigation.selectOnDown = m_ListItems[i + 1].rectTransform.GetComponent <Selectable>();
                }

                selectable.navigation = navigation;
            }

            if (m_BaseSelectable != null)
            {
                if (m_ListItems.Count > 0)
                {
                    Navigation navigation = Navigation.defaultNavigation;
                    navigation.selectOnDown     = m_ListItems[0].rectTransform.GetComponent <Selectable>();
                    m_BaseSelectable.navigation = navigation;
                }
            }

            float maxWidth    = CalculateMaxItemWidth();
            float buttonWidth = m_BaseTransform.rect.width;

            m_FullHeight = m_OptionDataList.options.Count * LayoutUtility.GetPreferredHeight(m_ListItemTemplate.rectTransform) + 16;

            m_ExpandedSize = new Vector2(Mathf.Max(maxWidth, buttonWidth), m_FullHeight);

            m_TempMaxHeight = m_MaxHeight;

            if (m_TempMaxHeight == 0)
            {
                m_TempMaxHeight = transform.root.GetComponent <RectTransform>().rect.height - 32;                // CHANGED FROM: MaterialUIScaler.GetParentScaler(m_DropdownPanel).targetCanvas.GetComponent<RectTransform>().rect.height - 32;
            }

            if (m_ExpandedSize.y > m_TempMaxHeight)
            {
                m_ExpandedSize.y = m_TempMaxHeight;
            }
            else
            {
                m_DropdownPanel.GetChildByName <Image>("Handle").gameObject.SetActive(false);
            }

            Destroy(m_ListItemTemplate.rectTransform.gameObject);

            m_DropdownPanel.position = m_BaseTransform.GetPositionRegardlessOfPivot();

            if (m_ExpandStartType == ExpandStartType.ExpandFromBaseTransformWidth)
            {
                if (m_VerticalPivotType == VerticalPivotType.BelowBase)
                {
                    m_DropdownPanel.position = new Vector3(m_DropdownPanel.position.x, m_DropdownPanel.position.y - m_BaseTransform.GetProperSize().y / 2, m_DropdownPanel.position.z);
                }
                else if (m_VerticalPivotType == VerticalPivotType.AboveBase)
                {
                    m_DropdownPanel.position = new Vector3(m_DropdownPanel.position.x, m_DropdownPanel.position.y + m_BaseTransform.GetProperSize().y / 2, m_DropdownPanel.position.z);
                }
            }


            m_ExpandedPosition   = CalculatedPosition();
            m_ExpandedPosition.z = m_BaseTransform.position.z;

            m_DropdownCanvasGroup.alpha = 0f;
            m_ShadowCanvasGroup.alpha   = 0f;

            if (m_ExpandStartType == ExpandStartType.ExpandFromBaseTransformWidth)
            {
                m_DropdownPanel.sizeDelta = new Vector2(m_BaseTransform.rect.size.x, 0f);
            }
            else if (m_ExpandStartType == ExpandStartType.ExpandFromBaseTransformHeight)
            {
                m_DropdownPanel.sizeDelta = new Vector2(0f, m_BaseTransform.rect.size.y);
            }
            else if (m_ExpandStartType == ExpandStartType.ExpandFromBaseTransformSize)
            {
                m_DropdownPanel.sizeDelta = m_BaseTransform.rect.size;
            }
            else
            {
                m_DropdownPanel.sizeDelta = Vector2.zero;
            }

            m_DropdownPanel.gameObject.SetActive(true);

            for (int i = 0; i < m_ListItemAutoTweeners.Count; i++)
            {
                TweenManager.EndTween(m_ListItemAutoTweeners[i]);
            }

            m_AutoTweeners         = new List <int>();
            m_ListItemAutoTweeners = new List <int>();

            m_AutoTweeners.Add(TweenManager.TweenFloat(f => m_DropdownCanvasGroup.alpha = f, m_DropdownCanvasGroup.alpha, 1f, m_AnimationDuration * 0.66f, 0, null, false, Tween.TweenType.Linear));
            m_AutoTweeners.Add(TweenManager.TweenFloat(f => m_ShadowCanvasGroup.alpha   = f, m_ShadowCanvasGroup.alpha, 1f, m_AnimationDuration * 0.66f, 0, null, false, Tween.TweenType.Linear));

            m_AutoTweeners.Add(TweenManager.TweenVector2(vector2 => m_DropdownPanel.sizeDelta = vector2, m_DropdownPanel.sizeDelta, m_ExpandedSize, m_AnimationDuration, m_AnimationDuration / 3, null, false, Tween.TweenType.EaseInOutQuint));

            m_AutoTweeners.Add(TweenManager.TweenVector3(vector3 => m_DropdownPanel.position = vector3, m_DropdownPanel.position, m_ExpandedPosition, m_AnimationDuration, m_AnimationDuration / 3, () =>
            {
                if (m_BaseSelectable != null && m_IsExapanded)
                {
                    m_BaseSelectable.interactable = false;
                }

                Vector2 tempVector2           = m_PanelLayer.anchoredPosition;
                tempVector2.x                 = Mathf.RoundToInt(tempVector2.x);
                tempVector2.y                 = Mathf.RoundToInt(tempVector2.y);
                m_PanelLayer.anchoredPosition = tempVector2;
            }, false, Tween.TweenType.EaseInOutQuint));

            for (int i = 0; i < m_ListItems.Count; i++)
            {
                int         i1          = i;
                CanvasGroup canvasGroup = m_ListItems[i].canvasGroup;
                m_ListItemAutoTweeners.Add(TweenManager.TweenFloat(f => canvasGroup.alpha = f, canvasGroup.alpha, 1f, m_AnimationDuration * 1.66f, (i1 * (m_AnimationDuration / 6) + m_AnimationDuration) - m_ScrollPosOffset / 800, null, false, Tween.TweenType.Linear));
            }

            if (m_FullHeight > m_TempMaxHeight)
            {
                m_DropdownPanel.GetChildByName <ScrollRect>("ScrollRect").gameObject.AddComponent <RectMask2D>();
            }

            m_IsExapanded = true;

            m_TimeShown = Time.unscaledTime;
        }