コード例 #1
0
ファイル: UINotification.cs プロジェクト: AlessioBona/LangPaK
        /// <summary>
        /// Shows the notification considering the NotificationData value.
        /// </summary>
        public void ShowNotification(NotificationData ndata, UICanvas targetCanvas)
        {
            foreach (Transform t in transform) //in case we have a child that is not on the proper layer, we set it here so it shows up in the target camera
            {
                t.gameObject.layer = gameObject.layer;
                Canvas c = t.gameObject.GetComponent <Canvas>();
                if (c != null)
                {
                    c.sortingLayerName = targetCanvas.Canvas.sortingLayerName;
                    c.overrideSorting  = true;
                    c.sortingOrder    += 10000;
                }
                Renderer r = t.gameObject.GetComponent <Renderer>();
                if (r != null)
                {
                    r.sortingLayerName = targetCanvas.Canvas.sortingLayerName;
                    r.sortingOrder    += 10000;
                }
            }

            if (notificationContainer != null)
            {
                notificationContainer.startHidden = true;
            }
            if (overlay != null)
            {
                overlay.startHidden = true;
            }

            len = specialElements != null ? specialElements.Length : 0;
            if (len > 0)
            {
                for (int i = 0; i < len; i++)
                {
                    if (specialElements[i] == null)
                    {
                        continue;
                    }
                    specialElements[i].startHidden = true;
                }
            }

            data = ndata;                           //we save this data to use it if we need to unregister from the Notification Queue

            if (icon != null && ndata.icon != null) //if this notification has an icon slot and the show notification passed a new icon, we update it
            {
                icon.sprite = ndata.icon;
            }

            if (ndata.buttonTexts == null || ndata.buttonTexts.Length == 0)
            {
                closeOnClick = true;    //if there are no button texts we let the user close this notification just by ckicking it
            }
            if (ndata.buttonNames == null || ndata.buttonNames.Length == 0)
            {
                closeOnClick = true;                 //if there are no button names we let the user close this notification just by ckicking it
            }
            if (closeButton != null && closeOnClick) //if we linked the closeOnClickButton, we configure it to close the notification window on click
            {
                UIButton b            = closeButton.GetComponent <UIButton>();
                float    onClickDelay = 0f;
                if (b != null)                                   //we check if we have an UIButton attached (we do this so that if the button has an onClick animation, we show it and after that we hide the notification)
                {
                    onClickDelay = b.onClickPunch.TotalDuration; //this creates a more pleasent user experience (UX) by letthing the OnClick animation play before hiding the notification
                }

                closeButton.onClick.AddListener(() =>
                {
                    StartCoroutine(HideNotification(onClickDelay));
                    StartCoroutine(DestroyAfterTime(GetOutAnimationsTimeAndDelay() + onClickDelay));
                });
            }

            if (ndata.lifetime > 0)  //We look for the lifetime (if it's -1 we do not auto hide the notification. We wait for the player to hit a button.
            {
                StartCoroutine(HideNotification(GetInAnimationsTimeAndDelay() + ndata.lifetime));
                StartCoroutine(DestroyAfterTime(GetInAnimationsTimeAndDelay() + ndata.lifetime + GetOutAnimationsTimeAndDelay()));
            }

#if dUI_TextMeshPro
            if (title != null)
            {
                if (title.GetComponent <TMPro.TextMeshProUGUI>() != null)
                {
                    title.GetComponent <TMPro.TextMeshProUGUI>().text = ndata.title;
                }
                else if (title.GetComponent <Text>() != null)
                {
                    title.GetComponent <Text>().text = ndata.title;
                }
            }
            if (message != null)
            {
                if (message.GetComponent <TMPro.TextMeshProUGUI>() != null)
                {
                    message.GetComponent <TMPro.TextMeshProUGUI>().text = ndata.message;
                }
                else if (message.GetComponent <Text>() != null)
                {
                    message.GetComponent <Text>().text = ndata.message;
                }
            }
#else
            if (title != null && title.GetComponent <Text>() != null)
            {
                title.GetComponent <Text>().text = ndata.title;
            }
            if (message != null && message.GetComponent <Text>() != null)
            {
                message.GetComponent <Text>().text = ndata.message;
            }
#endif

            if (buttons != null && ndata.buttonNames != null) //If this notification prefab has buttons and buttonNames is not null (those are the buttonNames we are listening for) we start adding the buttons
            {
                for (int i = 0; i < buttons.Length; i++)
                {
                    var index = i;
                    buttons[i].GetComponent <Button>().onClick.AddListener(() =>
                    {
                        if (ndata.buttonCallback != null && index < ndata.buttonCallback.Length && ndata.buttonCallback[index] != null)
                        {
                            ndata.buttonCallback[index].Invoke();
                        }
                        StartCoroutine(HideNotification(0.2f));
                        StartCoroutine(DestroyAfterTime(GetOutAnimationsTimeAndDelay() + 0.2f));             //We destroy this notification after the Out animation finished
                    });

                    if (ndata.buttonNames.Length > i && string.IsNullOrEmpty(ndata.buttonNames[i]) == false) //If we have a buttonName we make the button active and set the buttonName to the UIButton compoenent
                    {
                        buttons[i].gameObject.SetActive(true);
                        buttons[i].buttonName = ndata.buttonNames[i]; //We set the buttonName

                        if (ndata.buttonTexts != null)                //We might not have a text for the button (it might be an image or an icon) so we check if we wanted a text on it
                        {
                            if (ndata.buttonTexts.Length > i && !string.IsNullOrEmpty(ndata.buttonTexts[i]))
                            {
#if dUI_TextMeshPro
                                if (buttons[i].GetComponentInChildren <TMPro.TextMeshProUGUI>() != null)
                                {
                                    buttons[i].GetComponentInChildren <TMPro.TextMeshProUGUI>().text = ndata.buttonTexts[i];
                                }
#else
                                if (buttons[i].GetComponentInChildren <Text>() != null)
                                {
                                    buttons[i].GetComponentInChildren <Text>().text = ndata.buttonTexts[i];
                                }
#endif
                            }
                        }
                    }
                    else
                    {
                        buttons[i].gameObject.SetActive(false); //if we still have unused buttons on this notification prefab, we hide them
                    }
                }
            }
            StartCoroutine(ShowNotification());
        }