Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GUI Button > I > Open Inventory

Discussion in 'Scripting' started by jorn818, Mar 24, 2016.

  1. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    so Im making a mobile game and I have an inventory button on the canvas (GUI Texture/sprite with box collider)
    so what I want is if I click on it (or tap on it its for mobile, but thats irrelevant for now since its the same thing)
    the inventory opens, now the asset for the inventory uses the button ''I'' to open and close the inventory
    so what I want.. is if I press the GUIbutton attached to the canvas it will press I, and because of that the inventory will open

    Like this:

    GuiTexture > i > Open Inventory
    so the script simply has to be onmousedownGuiTexture > i

    so i have made a script for my main menu to change a scene, however I wonder if this script could be changed to do what is described above, however how do I do this?
    im not that good of a coder, espacially in the Unity api
    so help would be appreciated :D

    /c#
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PLAYTEST : MonoBehaviour {
    7.         void OnMouseDown() {
    8.             Application.LoadLevel("MiniZLoad");
    9.         }
    10.      
    11.     }
     
  2. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    I'm not understanding why you want the click event to have anything to do with "pressing I." Why not just have the click event of your Button toggle the Inventory panel? Look and see what pressing "I" is doing and just do that with your button event, too. Also, what system are you using? If it's one of mine, I can help you a bit further.

    Also, are you using a GUI texture with a click event instead of a Button object for a reason? Do you mean a legacy GUI Texture? Or like a GUI sprite?
     
  3. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97

    Because I dont want to destroy the asset but at the same time having pc compatibility simply for testing is always nice :D
    its a GUI Sprite form the new system.

    atm this is my script which dont seem to work

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class InputINV : MonoBehaviour {
    7.     void OnMouseDown() {
    8.         Input.GetKey("I");
    9.     }
    10.    
    11. }
     
  4. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    Yeah, that just reads Input. As far as I'm aware there is no built-in way to simulate key presses. You don't have to touch the code from the asset, just in your button controller call the method that toggles the inventory panel from the asset. Also, the new UI has buttons so you don't have to handle the OnMouseDown event at all. And if you did want to write it, you would be looking at EventSystems pointer events.
     
  5. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    but if there isnt any way to simulate key presses, than how do smartphone keyboards (which use mouse/touch input to function) work? what if I used a Event.Keycode or something?
     
  6. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    I'm not entirely sure what you're talking about. Do you mean attaching like a Bluetooth keyboard to a smartphone? The mouse and touch events are handled the same way with the event system.
     
  7. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    I simply want the following: I have a GUIButton on my Canvas, When I click on it I want the ''i'' button to be simulated
    so what I want is for that ''virtual button'' to do the same as my actual ''i'' button on my keyboard
     
  8. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    I understand that, but since there's no way to simulate key presses, you just have to call the relevant methods when the button is clicked.
     
  9. jorn818

    jorn818

    Joined:
    May 12, 2013
    Posts:
    97
    Well thats gonna overcomplicate my idea xD, thanks for your time though, although it might not have seem like it you actually helped me out allot