using UnityEngine;
using UnityEngine.UI;
public class Notification : MonoBehaviour
{
static GameObject notifyText, canvasObject, panelObject;
public static Notification instance = null;
public static Notification Instance
{
get
{
if(instance == null)
{
CreateText();
CreateCanvas();
CreatePanel();
instance = canvasObject.AddComponent<Notification>();
}
return instance;
}
}
static void CreateText()
{
notifyText = new GameObject("NotificationCanvas");
notifyText.AddComponent<Disappearance>();
notifyText.AddComponent<TMPro.TextMeshProUGUI>().fontSize = 50;
notifyText.GetComponent<RectTransform>().sizeDelta = new Vector2(1000f, 60f);
}
static void CreateCanvas()
{
canvasObject = new GameObject("NotificationCanvas");
Canvas canvas = canvasObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvasObject.AddComponent<GraphicRaycaster>();
CanvasScaler canvasScaler = canvasObject.AddComponent<CanvasScaler>();
canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
canvasScaler.referenceResolution = new Vector2(1920, 1080);
DontDestroyOnLoad(canvasObject);
}
static void CreatePanel()
{
if (canvasObject == null) return;
panelObject = new GameObject("Panel");
RectTransform panelRectTransform = panelObject.AddComponent<RectTransform>();
panelRectTransform.SetParent(canvasObject.transform, false);
panelRectTransform.sizeDelta = new Vector2(1000f, 0f);
panelRectTransform.position = new Vector3(Screen.width / 2, Screen.height / 5 * 4, 0);
UnityEngine.UI.Image panelImage = panelObject.AddComponent<UnityEngine.UI.Image>();
panelImage.color = new Color(0, 0, 0, 0);
VerticalLayoutGroup verticalLayoutGroup = panelObject.AddComponent<VerticalLayoutGroup>();
verticalLayoutGroup.spacing = 5f;
ContentSizeFitter contentSizeFitter = panelObject.AddComponent<ContentSizeFitter>();
contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
}
public string notification
{
set
{
if (value != "" && value != null)
{
Notify(value);
}
}
}
void Notify(string notice)
{
Instantiate(notifyText, panelObject.transform).GetComponent<TMPro.TextMeshProUGUI>().text = notice;
}
}
using UnityEngine;
public class Disappearance : MonoBehaviour
{
void Start()
{
Invoke("Disappear", 10f);
}
void Disappear()
{
Destroy(gameObject);
}
}
사용 예시
Notification.Instance.notification = "All files have already been downloaded.";
'유니티(Unity)' 카테고리의 다른 글
그래픽스 퍼포먼스 최적화 전후 (0) | 2024.04.08 |
---|---|
Unity 3D 렌더링 최적화 및 fps 높이기 (0) | 2024.04.04 |
라이트맵 런타임 Import 주의할 점 (0) | 2024.03.26 |
Unity Android 빌드 시 라이트맵 어두움 (0) | 2024.03.16 |
Collection was modified; enumeration operation may not execute. 에러. 리스트 요소 제거 시 주의 할점 (0) | 2024.02.17 |