Пример #1
0
    public MyInputField(string name, Sprite sprite, Rect rect)
        : base(name)
    {
        Element = new MyPanel(sprite, rect).Element;
        var inputScript = Element.AddComponent<InputField>();
        var text = new MyText("");
        var placeholder = new MyText("Enter Text...");
        text.SetAnchor(0, 0, 1, 1);
        placeholder.SetAnchor(0, 0, 1, 1);
        text.Element.GetComponent<Text>().alignment = TextAnchor.MiddleLeft;
        placeholder.Element.GetComponent<Text>().alignment = TextAnchor.MiddleLeft;
        placeholder.Element.GetComponent<Text>().fontStyle = FontStyle.Italic;
        placeholder.Element.GetComponent<Text>().color = Color.gray;

        var rectT = text.Element.GetComponent<RectTransform>();
        rectT.offsetMin = new Vector2(10, 5);
        rectT.offsetMax = new Vector2(-10, -5);

        rectT = placeholder.Element.GetComponent<RectTransform>();
        rectT.offsetMin = new Vector2(10, 5);
        rectT.offsetMax = new Vector2(-10, -5);

        AddElement(text);
        AddElement(placeholder);
        inputScript.placeholder = placeholder.Element.GetComponent<Text>();
        inputScript.textComponent = text.Element.GetComponent<Text>();
    }
Пример #2
0
    public MyButton(UnityEngine.Events.UnityAction eventListner, Sprite sprite, Rect rect, string text = "Button")
        : base("Button")
    {
        var trans = Element.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;

        Element.AddComponent<CanvasRenderer>();

        var image = Element.AddComponent<Image>();
        image.sprite = sprite;
        image.type = Image.Type.Sliced;

        var title = new MyText(text);
        var titleRect = title.Element.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 = Element.AddComponent<Button>();
        button.interactable = true;
        button.onClick.AddListener(eventListner);
    }