2014-11-22 90 views
2

快速問題我在這裏得到了這個腳本來顯示你收集的物品的名稱。這些名稱是公開的,所以我可以在項目列表中更改它們。我想要做的是走進一個項目並將鼠標懸停在項目上,以便它可以在屏幕中間顯示項目的名稱。我不知道我是否應該使用觸發器,或者GUILayout或其他。請幫助和謝謝。下面是腳本:修訂OnMouseOver在Unity中

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

安裝下面的腳本到每個項目。鼠標懸停該項目時,它將顯示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); 
     } 
    } 
} 
+0

我想爲此添加任何觸發事件嗎? – Ghostdre 2014-11-22 19:02:25

+0

我認爲你的問題是你無法在鼠標上顯示GUI元素。用我的腳本示例,沒有任何額外的觸發器或任何需要的東西。您可以輕鬆修改它以滿足您的需求。由於我沒有這個場景,所以很難編寫完整的代碼。 – maZZZu 2014-11-22 19:16:49

+0

哦,我看到我試圖用我的方式修改它,但我認爲我失敗了,哈哈。但我想知道是否可以將字符串消息替換爲字符串名稱。字符串名稱已經建立了名稱,但它在我的類Raycasting中。我應該把光線投射的字符串名稱叫做HoverGui類嗎? – Ghostdre 2014-11-22 19:25:11