Search Unity

tooltip to show from array C#

Discussion in 'Scripting' started by Zastrow89, Jul 8, 2012.

  1. Zastrow89

    Zastrow89

    Joined:
    May 1, 2010
    Posts:
    51
    hello there fellow coders! im completely new to the whole unity and coding in general but i want to learn :) im atm building a little inventory, and i got it to put teh items into an array that i display via GUI buttons, my question is how do i make it so when i hover over the item in my inventory, that it read the "tooltip" string i got under my item.

    this is my inventory script so far. thanks alot in advance!

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class inventory : MonoBehaviour {
    5.  public GameObject[] itemSlots = new GameObject[10];
    6.  public int i = 0;
    7.  
    8.  // Use this for initialization
    9.  void Start () {
    10.  
    11.  }
    12.  
    13.  // Update is called once per frame
    14.  void Update () {
    15.  
    16.  }
    17.  
    18.  void OnGUI () {
    19.  GUILayout.BeginArea(new Rect(Screen.width-144, Screen.height-180, 200, 200));
    20.  Rect r = new Rect(0,0,35,35);
    21.     for (i = 0; i < itemSlots.Length; i++){
    22.  if (GUI.Button(r,itemSlots[i].GetComponentInChildren<item>().icon)){
    23.  
    24.  }
    25.  r.x += 36;
    26.    if(r.x > 108) {
    27.       r.x = 0;
    28.       r.y += 36;
    29.    }
    30.  }
    31.  GUILayout.EndArea();
    32. }
    33. }
    my item script if someone needs it is here:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class item : MonoBehaviour {
    8.  
    9.     public bool isEquipped = false;
    10.      public int itemId;
    11.      public int buyPrice;
    12.      public int sellPrice;
    13.      public GameObject itemVisual;
    14.      public Texture2D icon;
    15.      public string tooltipText;
    16.  
    17.      // Use this for initialization
    18.      void Start () {
    19.  
    20.      }
    21.  
    22.      // Update is called once per frame
    23.      void Update () {
    24.  
    25.  
    26.      }
    27.      public void isEquippedCheck (){
    28.  
    29.      if (isEquipped == true){
    30.      itemVisual.renderer.enabled = true;
    31.      }
    32.      else itemVisual.renderer.enabled = false;
    33.      }
    34.  
    35.  
    36.  
    37.     }
    38.  
    39.  
     
  2. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Welcome to Unity, and glad to hear you're using C#.

    If I were you I'd stay away from Unity's GUI and invest in a proper 3rd party GUI like NGUI from the Asset Store.
    It will make your life much easier.
     
  3. Zastrow89

    Zastrow89

    Joined:
    May 1, 2010
    Posts:
    51
    i already got that Ngui, but imo it cant give me what i want :/ i might just be stupid... but i cant get anything out of that :/ the build in inventory in Ngui is so hard coded and "this is the only way to do it" as it can be :/ kind a sucks... i know unity's gui eats a motherload of drawcalls, but how can i do this with Ngui? how can i do exactly what i did here just with Ngui? because i am stumped!
     
  4. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    You don't have to do the inventory how aren does it in his example.
     
  5. Zastrow89

    Zastrow89

    Joined:
    May 1, 2010
    Posts:
    51
    i know ^^ but i still have to start somewhere... and i have no idea where to start with Ngui, its like an engine inside an engine, and i am just stumped :/
    anyone knows what i can use to get me startet making it with Ngui?
     
  6. Boss

    Boss

    Joined:
    Jun 27, 2012
    Posts:
    133
    Just look for OnGUI tutorials its simple, also goto assets section of foum and find my ultimate package, its coming soon. Has inventory in it, and GUI.