private static GameObject CreateToastGameObject(Transform parent, ToastAlignment toastAlignment) { GameObject toastGO = new GameObject(); toastGO.name = TOASTS_NAME; toastGO.transform.parent = parent; RectTransform rectTransform = toastGO.AddComponent <RectTransform>(); rectTransform.localPosition = new Vector3(0, 0, 0); //Content size fitter ContentSizeFitter contentSizeFitter = toastGO.AddComponent <ContentSizeFitter>(); contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; ////Horizontal layout VerticalLayoutGroup verticalLayoutGroup = toastGO.AddComponent <VerticalLayoutGroup>(); verticalLayoutGroup.childControlWidth = true; verticalLayoutGroup.childControlHeight = true; verticalLayoutGroup.childForceExpandWidth = false; verticalLayoutGroup.childForceExpandHeight = false; verticalLayoutGroup.padding = new RectOffset(VERTICAL_LAYOUT_PADDING, VERTICAL_LAYOUT_PADDING, VERTICAL_LAYOUT_PADDING, VERTICAL_LAYOUT_PADDING); Image image = toastGO.AddComponent <Image>(); Color bgColor = ToastSettings.Instance.toastBackgroundColor; bgColor.a = 0; image.color = bgColor; image.raycastTarget = false; return(toastGO); }
public static void CreateToast(string displayText, ToastDuration toastDuration, ToastAlignment toastAlignment) { if (string.IsNullOrEmpty(displayText)) { Debug.Log("Display Text is Null/Empty."); } else { Transform canvas = CreateCanvas().transform; GameObject parentGO = CreateToastGameObject(canvas, toastAlignment); Toast toast = parentGO.AddComponent <Toast>(); toast.DisplayToast(displayText, toastDuration, toastAlignment); } }