GUI изнутри

Научился сам? Помоги начинающему.

GUI изнутри

Сообщение gnoblin 23 июл 2009, 19:56

Интересная тема про GUI и Events.

http://forum.unity3d.com/viewtopic.php?p=88783#88783

using UnityEngine;
Код: Выделить всё
public class HoverButtonScript : MonoBehaviour
{
    static int HoverButtonHash = "HoverButton".GetHashCode();
    private Rect HoverButtonRect = new Rect(100, 100, 100, 100);
    void OnGUI()
    {
        switch (HoverButton(HoverButtonRect, new GUIContent("HoverButton"), "button"))
        {
            case EventType.mouseDown:
                Debug.Log("MouseDown");
                break;
            case EventType.mouseUp:
                Debug.Log("MouseUp");
                break;
            case EventType.mouseDrag:
                HoverButtonRect.x = Input.mousePosition.x - (HoverButtonRect.width / 2);
                HoverButtonRect.y = (Screen.height - Input.mousePosition.y) - (HoverButtonRect.height / 2);
                break;
            case EventType.mouseMove:
                GUI.Button(new Rect(HoverButtonRect.x + 100, HoverButtonRect.y, 50, 50), "Mouse\nis Over");
                break;
        }
    }
    public static EventType HoverButton(Rect position, GUIContent content, GUIStyle style)
    {
        int controlID = GUIUtility.GetControlID(HoverButtonHash, FocusType.Native);
        switch (Event.current.GetTypeForControl(controlID))
        {
            case EventType.mouseDown:
                if (position.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = controlID;
                    Event.current.Use();
                    return EventType.mouseDown;
                }
                break;
            case EventType.mouseUp:
                if (GUIUtility.hotControl != controlID)
                    return EventType.ignore;
                GUIUtility.hotControl = 0;
                Event.current.Use();
                if (position.Contains(Event.current.mousePosition))
                    return EventType.mouseUp;
                else
                    return EventType.ignore;
            case EventType.mouseDrag:
                if (GUIUtility.hotControl == controlID)
                {
                    Event.current.Use();
                    return EventType.mouseDrag;
                }
                else
                    return EventType.ignore;
            case EventType.repaint:
                style.Draw(position, content, controlID);
                if (position.Contains(Event.current.mousePosition))
                    return EventType.mouseMove;
                else
                    return EventType.repaint;
        }
        if (position.Contains(Event.current.mousePosition))
            return EventType.mouseMove;
        else
            return EventType.ignore;
    }
}
skypeid: madkust
Мои крайние проекты:
Убойный Хоккей
Cube Day Z (альфа)
Аватара пользователя
gnoblin
Адепт
 
Сообщения: 4633
Зарегистрирован: 08 окт 2008, 17:23
Откуда: Минск, Беларусь
Skype: madkust
  • Сайт

Re: GUI изнутри

Сообщение gnoblin 24 июл 2009, 10:47

Более простой скрипт из похожей темы на оф. форуме:

Код: Выделить всё
using UnityEngine;
using System.Collections;
/// <summary>
/// кнопка
/// </summary>
public class ForumButtonScript : MonoBehaviour
{
    static int buttonHash = "Button".GetHashCode();
    public static bool Button(Rect position, GUIContent content, GUIStyle style)
    {
        int id = GUIUtility.GetControlID(buttonHash, FocusType.Native);
        switch (Event.current.GetTypeForControl(id))
        {
            case EventType.MouseDown:
                // If the mouse is inside the button, we say that we're the hot control
                if (position.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = id;
                    Event.current.Use();
                }
                return false;
            case EventType.MouseUp:
                if (GUIUtility.hotControl == id)
                {
                    GUIUtility.hotControl = 0;

                    // If we got the mousedown, the mouseup is ours as well
                    // (no matter if the click was in the button or not)
                    Event.current.Use();

                    // But we only return true if the button was actually clicked
                    return position.Contains(Event.current.mousePosition);
                }
                return false;
            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                    Event.current.Use();
                break;
            case EventType.Repaint:
                style.Draw(position, content, id);
                return false;
        }
        return false;
    }

    void OnGUI()
    {
        Button(new Rect(100,100,100,100), new GUIContent("hello"), "button");
    }
}
skypeid: madkust
Мои крайние проекты:
Убойный Хоккей
Cube Day Z (альфа)
Аватара пользователя
gnoblin
Адепт
 
Сообщения: 4633
Зарегистрирован: 08 окт 2008, 17:23
Откуда: Минск, Беларусь
Skype: madkust
  • Сайт

Re: GUI изнутри

Сообщение gnoblin 24 июл 2009, 10:48

И еще один про TextArea:

Код: Выделить всё
using UnityEngine;
public class TextFieldScript : MonoBehaviour
{
   
    public int curKeyboardControl;
    private GUIContent myText1 = new GUIContent("Test Field");
    private GUIContent myText2 = new GUIContent("Test Field1");
    private GUIContent myText3 = new GUIContent("Test Field2");
    private bool DoRemoveFocus = false;
    void OnGUI()
    {
        if (DoRemoveFocus)
        {
            GUIUtility.keyboardControl = 0;
            DoRemoveFocus = false;
        }
        curKeyboardControl = GUIUtility.keyboardControl;
        GUI.Label(new Rect(310, 10, 100, 100), "Current\nKeyCtrl: " + curKeyboardControl.ToString(), "box");
        if (GUI.Button(new Rect(410, 10, 100, 100), "Remove Focus")) DoRemoveFocus = true;
        int TextField1ID = GUIUtility.GetControlID(FocusType.Keyboard);
        GUI.DoTextField(new Rect(10, 10, 100, 100), TextField1ID, myText1, true, 200, "TextArea");
        int TextField2ID = GUIUtility.GetControlID(FocusType.Keyboard);
        GUI.DoTextField(new Rect(110, 10, 100, 100), TextField2ID, myText2, true, 200, "TextArea");
        int TextField3ID = GUIUtility.GetControlID(FocusType.Keyboard);
        GUI.DoTextField(new Rect(210, 10, 100, 100), TextField3ID, myText3, true, 200, "TextArea");
    }
}
skypeid: madkust
Мои крайние проекты:
Убойный Хоккей
Cube Day Z (альфа)
Аватара пользователя
gnoblin
Адепт
 
Сообщения: 4633
Зарегистрирован: 08 окт 2008, 17:23
Откуда: Минск, Беларусь
Skype: madkust
  • Сайт

Re: GUI изнутри

Сообщение Hansen 05 окт 2009, 12:23

Спасибо! Актуально
Hansen
UNец
 
Сообщения: 11
Зарегистрирован: 30 июл 2009, 08:30


Вернуться в Уроки

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 6