Created
October 20, 2020 05:13
-
-
Save Frank-Buss/f72d8429e259563e4b67d79a03fc37a3 to your computer and use it in GitHub Desktop.
creates a toolbar with two buttons with a C# script, without prefabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.EventSystems; | |
using UnityEngine.UI; | |
// creates a toolbar with two buttons with a C# script, without prefabs | |
// extension method to set all values of a RectOffset to the same value | |
public static class Extensions | |
{ | |
public static void SetAll(this RectOffset ro, int value) | |
{ | |
ro.left += value; | |
ro.right += value; | |
ro.top += value; | |
ro.bottom += value; | |
} | |
} | |
// arranges buttons in a toolbar at the top left of the screen | |
public class Toolbar | |
{ | |
// initialize anchor and pivot, so that position 0,0 is top left of the parent | |
private void InitTopLeftAnchor(RectTransform rt) | |
{ | |
rt.pivot = new Vector2(0, 1); | |
rt.anchorMin = new Vector2(0, 1); | |
rt.anchorMax = new Vector2(0, 1); | |
rt.localPosition = new Vector3(0, 0, 0); | |
} | |
// create a text with fixed font size and the DejaVuSans font | |
private void CreateText(GameObject parent, string text) | |
{ | |
GameObject g2 = new GameObject(text); | |
g2.transform.parent = parent.transform; | |
Text t = g2.AddComponent<Text>(); | |
t.alignment = TextAnchor.MiddleCenter; | |
t.horizontalOverflow = HorizontalWrapMode.Overflow; | |
t.verticalOverflow = VerticalWrapMode.Overflow; | |
InitTopLeftAnchor(g2.GetComponent<RectTransform>()); | |
// note: GetBuiltinResource is undocumented, better use your own font | |
Font font = Resources.GetBuiltinResource<Font>("Arial.ttf"); | |
//Font font = Resources.Load<Font>("DejaVuSans"); | |
t.font = font; | |
t.fontSize = 20; | |
t.text = text; | |
t.enabled = true; | |
t.color = Color.black; | |
} | |
// create a button with a text | |
private void CreateButton(GameObject parent, string text, UnityAction action) | |
{ | |
GameObject button = new GameObject("Button"); | |
button.transform.parent = parent.transform; | |
button.AddComponent<RectTransform>(); | |
InitTopLeftAnchor(button.GetComponent<RectTransform>()); | |
var layout = button.AddComponent<HorizontalLayoutGroup>(); | |
layout.padding.SetAll(5); | |
layout.childControlWidth = true; | |
layout.childControlHeight = true; | |
Image image = button.AddComponent<Image>(); | |
image.color = Color.green; | |
button.AddComponent<Button>(); | |
button.GetComponent<Button>().onClick.AddListener(action); | |
CreateText(button, text); | |
} | |
// create a panel with a horizontal layout | |
private GameObject CreatePanel(GameObject parent) | |
{ | |
GameObject panel = new GameObject("Panel"); | |
panel.transform.parent = parent.transform; | |
panel.AddComponent<RectTransform>(); | |
InitTopLeftAnchor(panel.GetComponent<RectTransform>()); | |
var layout = panel.AddComponent<HorizontalLayoutGroup>(); | |
layout.padding.SetAll(5); | |
layout.spacing = 3; | |
layout.childControlWidth = true; | |
layout.childControlHeight = true; | |
layout.childForceExpandHeight = true; | |
var fitter = panel.AddComponent<ContentSizeFitter>(); | |
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize; | |
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize; | |
Image image = panel.AddComponent<Image>(); | |
image.color = Color.black; | |
return panel; | |
} | |
// create the toolbar with the specified global scale | |
public Toolbar(float scale) | |
{ | |
// create a canvas with the usual components, as if you create it from the editor | |
GameObject g = new GameObject("Canvas"); | |
if (UnityEngine.Object.FindObjectOfType<EventSystem>() == null) | |
{ | |
// create a new event system, if it doesn't exist already | |
new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule)); | |
} | |
Canvas canvas = g.AddComponent<Canvas>(); | |
canvas.renderMode = RenderMode.ScreenSpaceOverlay; | |
CanvasScaler cs = g.AddComponent<CanvasScaler>(); | |
cs.scaleFactor = 10.0f; | |
cs.dynamicPixelsPerUnit = 10f; | |
cs.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize; | |
cs.scaleFactor = scale; | |
GraphicRaycaster gr = g.AddComponent<GraphicRaycaster>(); | |
RectTransform rt = g.GetComponent<RectTransform>(); | |
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 3.0f); | |
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 3.0f); | |
// create a panel with the canvas as parent | |
GameObject p = CreatePanel(g); | |
// add buttons to the panel | |
CreateButton(p, "Test 1", () => { Debug.Log("button 1 pressed"); }); | |
CreateButton(p, "Test 2", () => { Debug.Log("button 2 pressed"); }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment