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

Inventory system based on mouse coordinates

Discussion in 'Scripting' started by Kreater, Sep 23, 2014.

  1. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Hello @ all,

    first my english is not the best so please be patient :)

    I want to create a inventory system based on a coordinate system and i need some help.
    I know how to check if the mouse is on a specific point at the screen, but how do I highlight this "invicible" rectangle if im hover it with the mouse? I want somethin easy, like a border around for example the coordinates 0,0,50,50.

    I hope theres someone how can help me :)

    With best regards:
    Kreater
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
  3. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Hello MrPriest,

    thanks for the link, but i don't mean the MouseOver event, i mean a method to draw lines or a square.

    Example:

    Code (JavaScript):
    1.  
    2. function Update()
    3. {
    4.     var rect = Rect (0, 0, 50, 50);                                   //Position to check
    5.     if (rect.Contains(Input.mousePosition))               //If mouse in specified rect
    6.     {
    7.         DrawRect(rect);
    8.     }
    9. }
    10.  
    11. function DrawRect(rect : Rect)
    12. {
    13.      //Draw lines around the coordinates from rect
    14. }
    15.  
     
  4. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Isn't it an inventory square?
    If it's like a grid with many cells, have each cell have "OnMouseOver" - which inturn causes there cell to change it's color (highlight effect).
    Otherwise, create a prefab that is just a square, that is instanced over that place (or gets rendered on and off as needed).

    It's hard to understand what you mean...
    How would you explain this feature to someone who would program that inventory system? (basically)
     
  5. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Okay.

    I do not create the inventory with the GUI elements, i want to do it only with the screen coordinates.
    Thats mean that i want to check each sqaure seperatly (because i have read [or written i dont know which is correct] a post some month ago who much people prefered to use coordinates and not GUI elements because to much GUIs will kill the FPS (i dont know if its correct but i think).

    On GUI Elements its check automatically where the mouse position is and its hightlighting a button if im hover it. But if i check seperately "If the mouse position in rect 0,0,50,50" there is nothing that i can see, so there no graphical feedback that im in this rect (only a debug log if i put one in there :D ) and i want that around these rect a line is drawn (that means i want to hightlight the border from the rect).

    I hope you now understand what i mean (i dont know how i could describe it with my terrible english :D )
     
    Last edited: Sep 23, 2014
  6. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Don't worry about your English, it's pretty good, so no need to apologize.
    Yeah, I understand your idea, I'll look at it.

    Oh, and the OnMouseOver works on anything with a collider, it does not have to be a GUI object.
     
  7. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Hehe ok thanks :)
     
  8. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Is it for debug purposes, or ingame?

    Code (CSharp):
    1.  
    2. void DrawRect(int x, int y, int length, int height)
    3. {
    4.   //Draw lines around the coordinates from rect
    5.     Vector3 rectTopLeft = new Vector3(x, y + height, 0);
    6.     Vector3 rectTopRight = new Vector3((x + length, y + height, 0);
    7.     Vector3 rectBotLeft = new Vector3(x, y, 0);
    8.     Vector3 rectBotRight = new Vector3(x + length, y, 0);
    9.    
    10.     Debug.DrawLine(rectTopLeft, rectTopRight, Color.red);
    11.     Debug.DrawLine(rectTopRight, rectBotRight, Color.red);
    12.     Debug.DrawLine(rectBotRight, rectBotLeft, Color.red);
    13.     Debug.DrawLine(rectBotLeft, rectTopLeft, Color.red);
    14. }
    15. }
    Will that do? I've never done that stuff myself, and do not have Unity with me (at work) so I can't test it.

    Sorry about it being in C#, I do not use JS, but it should be simple to convert.
     
    Last edited: Sep 23, 2014
  9. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Hey,

    it is for ingame but that one doesn't worked for me :/

    I have another idea but i have a little Problem. I say "If mouse in rect(0,0,50,50) then draw a GUI.Box on the coodinates from this rect". The Problem is, the coordinate systems has not the same coordinates as the GUI (0,0,50,50 is at the screen coordinates the left lower corner and in the GUI the top left so can't draw a box on the same spot without messing the coordinates :/
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, the coordinate systems for GUI stuff is a pain. If, instead of Input.MousePosition, you use Event.current.mousePosition, then it will already be in GUI coordinates. (You can only do this inside OnGUI, of course.)

    Or, if you need to convert to GUI coordinates, you may be able to use GUIUtility.ScreenToGUIPoint. (There is also a GUIToScreenPoint for going the other way.)

    Hope this helps,
    - Joe
     
  11. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Well, that's why MouseOver is convenient.
    camera.main.ScreenToWorldPoint (Input.mousePosition)
    Will turn your mouse position to a world coordinate (Vector3)
    Of course, that is assuming that you are using the main camera, if your (The player's) POV is from a different camera, use that one instead.
     
  12. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Hello @ all,

    i tried to convert the coordinates and show a texture, and actual i does what i want :)

    Code (CSharp):
    1.  
    2.  
    3. var mouseInside = false;  
    4. var rect : Rect;              
    5. var hightlightedSlot : Texture2D;  
    6.  
    7. function Update ()
    8. {      
    9.     rect = Rect (0, 0, 50, 50);
    10.     if (rect.Contains(Input.mousePosition))
    11.     {
    12.         mouseInside = true;
    13.     }
    14.     else
    15.     {
    16.         mouseInside = false;
    17.     }
    18. }
    19.  
    20. function OnGUI()
    21. {
    22.     if(mouseInside == true)
    23.     {
    24.         GUI.DrawTexture(Rect (rect.left,Screen.height - rect.height,rect.height,rect.width), hightlightedSlot);
    25.     }
    26.     else
    27.     {
    28.  
    29.     }  
    30. }
    31.  
    Thanks for the help :)
     
  13. Kreater

    Kreater

    Joined:
    Jan 2, 2014
    Posts:
    10
    Ok, no it don't work ^^

    I create a inventory like the on from "The Forest" thats much easiert :)
    Thanks for the help anyway :)