private void CreateComponent(UIelement elem) { Canvas canvas = (Canvas)FindObjectOfType(typeof(Canvas)); // If the canvas does not exist, create one. No duplicates. if (canvas == null) { GameObject newCanvas = new GameObject("Canvas"); Canvas c = newCanvas.AddComponent <Canvas>(); canvas = c; c.name = canvasName; c.renderMode = RenderMode.ScreenSpaceOverlay; CanvasScaler scaler = newCanvas.AddComponent <CanvasScaler>(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = new Vector2(1920, 1080); newCanvas.AddComponent <GraphicRaycaster>(); } switch (elem) { case UIelement.Text: GameObject newText = new GameObject("Text"); newText.transform.SetParent(canvas.transform, false); newText.AddComponent <CanvasRenderer>(); Text t = newText.AddComponent <Text>(); t.name = txtName; t.raycastTarget = false; t.color = color; t.fontSize = txtSize; t.text = txt; t.alignment = TextAnchor.MiddleCenter; t.rectTransform.anchoredPosition = pos; t.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); t.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); switch (txtStyle) { case FontStyle.Normal: t.fontStyle = UnityEngine.FontStyle.Normal; break; case FontStyle.Bold: t.fontStyle = UnityEngine.FontStyle.Bold; break; case FontStyle.Italic: t.fontStyle = UnityEngine.FontStyle.Italic; break; case FontStyle.BoldAndItalic: t.fontStyle = UnityEngine.FontStyle.BoldAndItalic; break; } break; case UIelement.Button: GameObject newButton = new GameObject("Button"); newButton.transform.SetParent(canvas.transform, false); newButton.AddComponent <RectTransform>(); newButton.AddComponent <CanvasRenderer>(); Image bImage = newButton.AddComponent <Image>(); Button b = newButton.AddComponent <Button>(); var cb = newButton.GetComponent <Button>().colors; cb.normalColor = color; cb.highlightedColor = hColor; cb.pressedColor = pColor; newButton.GetComponent <Button>().colors = cb; b.targetGraphic = b.image; b.name = btnName; bImage.sprite = btnSprite; bImage.type = Image.Type.Sliced; RectTransform bTransform = newButton.GetComponent <RectTransform>(); bTransform.anchoredPosition = pos; bTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); bTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); GameObject newButtonText = new GameObject("Text"); newButtonText.transform.SetParent(newButton.transform, false); newButtonText.AddComponent <CanvasRenderer>(); Text tBtn = newButtonText.AddComponent <Text>(); tBtn.color = txtColor; tBtn.fontSize = txtSize; tBtn.text = txt; tBtn.alignment = TextAnchor.MiddleCenter; tBtn.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); tBtn.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); break; case UIelement.Image: GameObject newImage = new GameObject("Image"); newImage.transform.SetParent(canvas.transform, false); newImage.AddComponent <CanvasRenderer>(); Image i = newImage.AddComponent <Image>(); i.raycastTarget = false; i.name = imgName; i.color = color; i.sprite = sprite; i.rectTransform.anchoredPosition = pos; i.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); i.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); break; case UIelement.Background: GameObject newBackground = new GameObject("Panel"); newBackground.transform.SetParent(canvas.transform, false); newBackground.transform.SetSiblingIndex(0); newBackground.AddComponent <CanvasRenderer>(); Image bg = newBackground.AddComponent <Image>(); bg.raycastTarget = false; bg.name = bgName; bg.color = color; bg.sprite = bgSprite; bg.type = Image.Type.Sliced; bg.rectTransform.anchoredPosition = pos; bg.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, canvas.GetComponent <RectTransform>().rect.width); bg.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, canvas.GetComponent <RectTransform>().rect.height); break; } }
private void OnGUI() { GUILayout.Label("Unity UI Creator Toolbox", EditorStyles.boldLabel); GUILayout.Label("Select the UI element you wish to add:"); elem = (UIelement)EditorGUILayout.EnumPopup(elem); GUILayout.Label("Style Properties", EditorStyles.miniBoldLabel); switch (elem) { case UIelement.Text: txtName = EditorGUILayout.TextField("Name: ", txtName); GUILayout.Label("Text:"); txt = EditorGUILayout.TextArea(txt, GUILayout.Height(50)); txtFont = EditorGUILayout.ObjectField("Font: ", txtFont, typeof(Font), true) as Font; txtSize = EditorGUILayout.IntField("Font Size: ", txtSize); txtStyle = (FontStyle)EditorGUILayout.EnumPopup("Font Style: ", txtStyle); break; case UIelement.Button: btnName = EditorGUILayout.TextField("Name: ", btnName); btnSprite = EditorGUILayout.ObjectField("Source Image: ", btnSprite, typeof(Sprite), true, GUILayout.Height(100)) as Sprite; EditorGUILayout.Space(); txt = EditorGUILayout.TextField("Button Text: ", txt); txtFont = EditorGUILayout.ObjectField("Font: ", txtFont, typeof(Font), true) as Font; txtSize = EditorGUILayout.IntField("Font Size: ", txtSize); txtStyle = (FontStyle)EditorGUILayout.EnumPopup("Font Style: ", txtStyle); txtColor = EditorGUILayout.ColorField("Text Color: ", txtColor); txtColor.a = 255; hColor = EditorGUILayout.ColorField("Highlighted Color: ", hColor); hColor.a = 255; pColor = EditorGUILayout.ColorField("Pressed Color: ", pColor); pColor.a = 255; break; case UIelement.Image: imgName = EditorGUILayout.TextField("Name: ", imgName); sprite = EditorGUILayout.ObjectField("Source Image: ", sprite, typeof(Sprite), true, GUILayout.Height(100)) as Sprite; EditorGUILayout.Space(); break; case UIelement.Background: bgName = EditorGUILayout.TextField("Name: ", bgName); GUILayout.Label("*Background will cover the entire canvas.", EditorStyles.miniLabel); EditorGUILayout.Space(); bgSprite = EditorGUILayout.ObjectField("Source Image: ", bgSprite, typeof(Sprite), true, GUILayout.Height(100)) as Sprite; EditorGUILayout.Space(); break; } color = EditorGUILayout.ColorField("Color: ", color); color.a = 255; GUILayout.Label("Transform Properties", EditorStyles.miniBoldLabel); EditorGUILayout.BeginHorizontal(); EditorGUIUtility.labelWidth = 50; width = EditorGUILayout.IntField("Width: ", width); EditorGUIUtility.labelWidth = 50; height = EditorGUILayout.IntField("Height: ", height); EditorGUILayout.EndHorizontal(); pos = EditorGUILayout.Vector2Field("Position: ", pos); EditorGUILayout.Space(); if (GUILayout.Button("Create UI element", GUILayout.Height(30))) { CreateComponent(elem); } }
private void NewsTab(int tab) { // Header OpLabel labelID = new OpLabel(new Vector2(100f, 560), new Vector2(400f, 40f), $"News Feed".ToUpper(), FLabelAlignment.Center, true); Tabs[tab].AddItems(labelID); OpLabel labelDsc = new OpLabel(new Vector2(100f, 540), new Vector2(400f, 20f), $"Latest news for CRS", FLabelAlignment.Center, false); Tabs[tab].AddItems(labelDsc); List <UIelement> news = new List <UIelement>(); if (File.Exists(Custom.RootFolderDirectory() + "customNewsLog.txt")) { DateTime current = DateTime.UtcNow.Date; CustomWorldMod.Log($"Reading news feed, current time [{current.ToString("dd/MM/yyyy")}]"); string[] lines = File.ReadAllLines(Custom.RootFolderDirectory() + "customNewsLog.txt"); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains(News.IGNORE) || lines[i].Equals(string.Empty)) { continue; } bool bigText = false; string lastUpdate = string.Empty; TimeSpan diff; if (lines[i].Contains(News.DATE)) { if (!updatedNews) { try { DateTime newsDate = DateTime.ParseExact(lines[i].Replace(News.DATE, ""), "dd/MM/yyyy", null); diff = current - newsDate; lastUpdate = newsDate.ToShortDateString(); CustomWorldMod.Log($"News date [{lastUpdate}], difference [{diff.TotalDays}]"); if (Math.Abs(diff.TotalDays) < 7) { updatedNews = true; } } catch (Exception e) { CustomWorldMod.Log($"Error reading the date time in news feed [{lines[i].Replace(News.DATE, "")}] - [{e}]", true); } } continue; } if (lines[i].Contains(News.BIGTEXT)) { bigText = true; lines[i] = lines[i].Replace(News.BIGTEXT, ""); } if (bigText) { news.Add(new OpLabel(default(Vector2), default(Vector2), lines[i], FLabelAlignment.Center, true)); } else { news.Add(new OpLabelLong(default(Vector2), default(Vector2), lines[i], true, FLabelAlignment.Left)); } } //How Many Options int numberOfNews = news.Count; if (numberOfNews < 1) { OpLabel label2 = new OpLabel(new Vector2(100f, 350), new Vector2(400f, 20f), "No news found.", FLabelAlignment.Center, false); Tabs[tab].AddItems(label2); return; } int spacing = 25; Vector2 rectSize = new Vector2(500 - spacing, 30); OpScrollBox mainScroll = new OpScrollBox(new Vector2(25, 25), new Vector2(550, 500), (int)(spacing + ((rectSize.y + spacing) * numberOfNews))); Vector2 rectPos = new Vector2(spacing / 2, mainScroll.contentSize - rectSize.y - spacing); Vector2 labelSize = new Vector2(rectSize.x - spacing, rectSize.y - 2 * spacing); Tabs[tab].AddItems(mainScroll); for (int i = 0; i < numberOfNews; i++) { UIelement label = news[i]; label.pos = rectPos + new Vector2(spacing, spacing); label.size = labelSize; mainScroll.AddItems(label); rectPos.y -= rectSize.y + spacing; } } }