Ghostdre
Ghostdre

Reputation: 69

OnMouseOver In Unity

quick question I got this script here to show the name of an Item you collect. These names are made public so I can change them within the item list. What I want to do Is to walk into an item and hover my mouse over the item so it can display the name of the item in the middle of the screen. I don't know if I should use a trigger, or a GUILayout or however. Please Help and thanks. Here is the Script: UPDATED

public class RayCasting : MonoBehaviour
{
public float pickupDistance;
public List<Item> items;
//public List<Keybutton> buttons;

#region Unity
void Start ()
{
    Screen.lockCursor = false;

    for (int i = 0; i < items.Count; i++) {
        Item temp = items[i];
        int randomIndex = Random.Range(i, items.Count);
        items[i] = items[randomIndex];
        items[randomIndex] = temp;
    }
}
void Update ()
{
            RaycastHit hit;
            Ray ray = new Ray (transform.position, transform.forward);
            if (Physics.Raycast (ray, out hit, pickupDistance)) {
                    foreach (Item item in items) {

                            if (Input.GetMouseButtonDown (0)) {
                                    if (item.gameObject.Equals (hit.collider.gameObject)) {
                                            numItemsCollected++;
                                            item.Collect ();
                                            break;
                                    }

                            }
                    }
            }
    }

void OnGUI()
{

    GUI.backgroundColor = Color.blue;
    GUI.Box(new Rect(120,390,170,250),"Text Message");
    GUILayout.BeginArea(new Rect(132,432,100,170));
    {
        GUILayout.BeginVertical();
        {
    if (numItemsCollected < items.Count)
    {
        foreach (Item item in items)

                    GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "" + item.password: " ", item.name ));

    }
    else
    {
    foreach (Item item in items)

                    GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "" + item.password: " ", item.name ));          
                //GUILayout.Label("Take code to KeyPad");
    }
        }
        GUILayout.EndVertical();
}
    GUILayout.EndArea();

    //Enter Code to unlock doors.

                    if (GUI.Button (new Rect (250, 830, 100, 50), "Enter Code"))
                    if (numItemsCollected > items.Count) {              
                            Debug.Log ("Entering Code");
                }
}

#endregion

#region Private
private int numItemsCollected;
#endregion
}

[System.Serializable]
public class Item
{
public string name;
//public GUIText textObject;
public GameObject gameObject;
public float guiDelay = 0.1f;

private float lastHoverTime = -99.0f;
public int password;

public bool Collected { get; private set; }



public void Collect()
{
    Collected = true;
    //gameObject.SetActive(false);
}

public void passwordNumber()
{
    password = 0;
    Collected = true;
    gameObject.SetActive(false);
}

void OnMouseEnter ()
{
    lastHoverTime = Time.timeSinceLevelLoad;
}

void OnGUI(){
            if (lastHoverTime + guiDelay > Time.timeSinceLevelLoad) {
                    GUI.Box (new Rect (300, 300, 170, 250), name);
            }
    }
}

Upvotes: 2

Views: 5149

Answers (1)

maZZZu
maZZZu

Reputation: 3625

Attach following script to every item. It will show QUI.Box when mouse hovers the item. Just change the size of the QUI.Box and set the message to suitable values.

using UnityEngine;
using System.Collections;

public class HoverGUI : MonoBehaviour {

    public string message = "Foo Bar";
    public float guiDelay = 0.1f;

    private float lastHoverTime = -99.0f;
    void OnMouseOver() {
        lastHoverTime = Time.timeSinceLevelLoad;
    }

    void OnGUI(){
        if(lastHoverTime + guiDelay > Time.timeSinceLevelLoad){
            GUI.Box(new Rect(0,0,170,250),message);
        }
    }
}

Upvotes: 1

Related Questions