public Button(UnityEngine.Events.UnityAction eventListner, Sprite sprite, Rect rect, string text = "Button") : base("Button") { var trans = UiObject.AddComponent <RectTransform>(); SetSize(trans, rect.size); SetSize(trans, new Vector2(120, 32)); trans.anchoredPosition3D = new Vector3(0, 0, 0); trans.anchoredPosition = new Vector2(0, 0); trans.localScale = new Vector3(1.0f, 1.0f, 1.0f); trans.localPosition = rect.position; UiObject.AddComponent <CanvasRenderer>(); var image = UiObject.AddComponent <UnityEngine.UI.Image>(); image.sprite = sprite; image.type = UnityEngine.UI.Image.Type.Sliced; var title = new Text(text); var titleRect = title.UiObject.GetComponent <RectTransform>(); title.SetAnchor(new Vector2(0, 0), new Vector2(1, 1)); titleRect.sizeDelta.Set(0, 0); titleRect.offsetMin = new Vector2(0, 0); titleRect.offsetMax = new Vector2(0, 0); titleRect.localScale = new Vector3(1, 1, 1); AddElement(title); var button = UiObject.AddComponent <UnityEngine.UI.Button>(); button.interactable = true; button.onClick.AddListener(eventListner); }
public InputField(string name, Sprite sprite, Rect rect) : base(name) { UiObject = new Panel(sprite, rect).UiObject; var inputScript = UiObject.AddComponent <UnityEngine.UI.InputField>(); var text = new Text(""); var placeholder = new Text("Enter Text..."); text.SetAnchor(0, 0, 1, 1); placeholder.SetAnchor(0, 0, 1, 1); text.UiObject.GetComponent <UnityEngine.UI.Text>().alignment = TextAnchor.MiddleLeft; placeholder.UiObject.GetComponent <UnityEngine.UI.Text>().alignment = TextAnchor.MiddleLeft; placeholder.UiObject.GetComponent <UnityEngine.UI.Text>().fontStyle = FontStyle.Italic; placeholder.UiObject.GetComponent <UnityEngine.UI.Text>().color = Color.gray; var rectT = text.UiObject.GetComponent <RectTransform>(); rectT.offsetMin = new Vector2(10, 5); rectT.offsetMax = new Vector2(-10, -5); rectT = placeholder.UiObject.GetComponent <RectTransform>(); rectT.offsetMin = new Vector2(10, 5); rectT.offsetMax = new Vector2(-10, -5); AddElement(text); AddElement(placeholder); inputScript.placeholder = placeholder.UiObject.GetComponent <UnityEngine.UI.Text>(); inputScript.textComponent = text.UiObject.GetComponent <UnityEngine.UI.Text>(); }