OnMouseOver在Unity中

时间:2014-11-22 03:51:43

标签: c# triggers unity3d onmouseover

快速提问我这里有这个脚本来显示你收集的物品的名称。这些名称是公开的,因此我可以在项目列表中更改它们。我想做的是走进一个项目并将鼠标悬停在项目上,这样它就可以在屏幕中间显示项目的名称。我不知道我是应该使用触发器,还是使用GUILayout。请帮助和谢谢。这是脚本:更新

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);
            }
    }
}

1 个答案:

答案 0 :(得分:1)

将以下脚本附加到每个项目。鼠标悬停项目时会显示QUI.Box。只需更改QUI.Box的大小,并将message设置为合适的值。

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);
        }
    }
}