public static void CreateLabel(string text, DymicLabelPosition pos, float duration = 1f, int fontSize = 5, bool fadeInAndOut = true, string fontPath = "Fonts/Nulshock_free", Color?color = null) { foreach (var lbl in FindObjectsOfType <DynamicLabel>()) { Destroy(lbl.gameObject); } if (color == null) { color = Color.white; } var obj = new GameObject().AddComponent <DynamicLabel>(); obj.Init(text, pos, duration, fontSize, fadeInAndOut, fontPath, color); }
public void Init(string text, DymicLabelPosition pos, float duration, int fontSize, bool fadeInAndOut, string fontPath, Color?color) { Rect screenRect = AspectUtility.screenRect; this.transform.position = Camera.main.ScreenToWorldPoint(new Vector2(screenRect.width / 2, screenRect.height / 2)); _guiStyle = new GUIStyle(); _guiStyle.fontSize = (int)(screenRect.width / fontSize); _guiStyle.font = UnityEngine.Resources.Load <Font>(fontPath); _guiContent = new GUIContent(text); _guiStyle.normal.textColor = color.Value; var size = _guiStyle.CalcSize(_guiContent); _guiRect = new Rect(); switch (pos) { case DymicLabelPosition.HORIZONTAL_AND_VERTICAL_CENTERED: _guiRect.x = (float)(screenRect.width - size.x) / 2; _guiRect.y = (float)(screenRect.height - size.y) / 2; _guiRect.width = (float)(screenRect.width * size.x); _guiRect.height = (float)(screenRect.height * size.y); break; } this.gameObject.name = string.Format("DynamicLabel {0}", Regex.Replace(text, @"\r\n?|\n", "")); if (duration > 0) { StartCoroutine(this.SelfDestruct(duration)); } if (duration > 0 && fadeInAndOut) { StartCoroutine(this.FadeInAndFadeOut(duration)); } }