Search Unity

[solved] what is the difference between label and button

Discussion in 'Immediate Mode GUI (IMGUI)' started by wisly, Jan 22, 2010.

  1. wisly

    wisly

    Joined:
    Jan 18, 2010
    Posts:
    12
    I want to design an gui editor.
    the following is my code(SimpleLabel.cs):


    using UnityEngine;
    using System.Collections;

    [ExecuteInEditMode()]
    public class SimpleLabel : MonoBehaviour
    {
    public Rect location = new Rect(0, 0, 100, 100);
    private bool _startDrag = false;
    public string textLabel;
    public GUIStyle textStyle = new GUIStyle();
    public string labelId;



    void OnGUI()
    {
    //refresh the main camera.
    Camera.main.transform.Translate(new Vector3(0, 0, 0));

    //GUILayout.Label(GUI.depth.ToString());
    if (Event.current.mousePosition.x >= location.xMin
    Event.current.mousePosition.x <= location.xMax
    Event.current.mousePosition.y >= location.yMin
    Event.current.mousePosition.y <= location.yMax
    Event.current.type == EventType.mouseDown)
    {
    //Debug.Log("Mouse Down");
    _startDrag = true;
    }


    if (_startDrag Event.current.type == EventType.mouseDrag)
    {
    {
    if (Event.current.button == 0)
    {
    location.x += Event.current.delta.x;
    location.y += Event.current.delta.y;
    }
    else if (Event.current.button == 1)
    {
    location.width += Event.current.delta.x;
    location.height += Event.current.delta.y;
    }
    }
    }


    if (_startDrag Event.current.type == EventType.mouseUp)
    {
    _startDrag = false;
    }
    GUI.Box(location, textLabel);
    }
    }


    I also created another file called SimpleButton with similar code(change the Box into Label).

    then I add two button and two label to the main camera.

    Now the problem is I can change each button position easily,even they are at the same position at first.but I cann't do that on the label. it seems there is a layer on each button, but not on label(box).
    Can anyone help me?
    Thanks.
     
  2. wisly

    wisly

    Joined:
    Jan 18, 2010
    Posts:
    12
    I'm sorry, I wrote a wrong word.
    another file should change the Box(Label) into Button.
     
  3. wisly

    wisly

    Joined:
    Jan 18, 2010
    Posts:
    12
    OK,Now I 've found the problem.
    Every time after using Event.Current,
    I should add a code Event.current.Used() method,
    so that other GUI elements can ignore it.

    but I'm still not understand why the button element can ignore the event automatically.

    Thanks.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    because you didn't tell it to.
    you need to inform the ui that the focus is "locked" basically (I think its a GUIUtility field / function when I remember read) on the object with id XY. Then it won't react to it on other objects
     
  5. wisly

    wisly

    Joined:
    Jan 18, 2010
    Posts:
    12
    Thanks for reply,dreamora.
    I think you must mean the GUIUtility.hotControl.
    Can I use this to tell the ui a Label has caught the focus and prevent other gui components(including label) from achieving the the event again?