c# - Why would supposedly different two event methods be called by the same object? (Mouse Click-Drag-Unclick) -


version: unity3d 4.6.2f1

i have grid of tiles (cube gameobjects tile script attached). positioned @ index location [x][y] = (x, y, z=0) world coordinates.

i trying achieve behavior similar minesweepers: when left click released, unless there right click down, tile upon mouse released revealed. below, try coordinates of tile upon mouse left click released.

before code:

  • onmousedown() - onmousedown called when user has pressed mouse button while on guielement or collider.
  • onmouseup() - onmouseup called when user has released mouse button.

based on these definitions, i assumed (mistake?) onmouseup() called object above it, mouse left click released.

on tile script, have following :

public class tile : monobehaviour {     // variables     private vector2 _gridposition = vector2.zero;      public vector2 gridposition     {         { return _gridposition; }         set { _gridposition = value; }     }      // functions     string vec2totext(vector2 v)     {         return string.format("(" + v.x + ", " + v.y + ")");     }      void onmousedown()     {         debug.log("tile:: on_mouse_down:: " + vec2totext(_gridposition));     }      void onmouseup()     {         debug.log("tile:: on_mouse_up:: " + vec2totext(_gridposition));     }      void onmouseenter()     {         debug.log("tile:: mouse_enter: " + vec2totext(_gridposition));     }      void onmouseexit()     {         debug.log("tile:: mouse_exit: " + vec2totext(_gridposition));     } } 

and here output:

tile:: mouse_enter: (15, 15) tile:: mouse_down: (15, 15) tile:: mouse_exit: (15, 15) tile:: mouse_enter: (14, 15) tile:: mouse_up: (15, 15)    %% line tile:: mouse_exit: (14, 15) 

if (14,15) on onmouseup() function, i'd directly find tile reveal apparently can't. workaround goal thought keep static bool _isdragging , use in onmouseenter() in if block assign tile revealed correctly. -- calling 2 functions same object though pressed left click object , released on other object? -- intended? there better workaround situation?

onmousedown emitted left mouse button. have implement right click yourself.

one option check raycast press buttons.

void update() {     if (input.getmousebuttondown(1))     {         var ray = camera.screenpointtoray(input.mouseposition);         raycasthit hitinfo;         if (physics.raycast(ray, out hitinfo))         {             var myobject = hitinfo.collider.getcomponent<myscriptorsomething>();             // here         }     } } 

one other option using onmouseenter check right click. like:

void onmouseover() {     if (input.getmousebuttondown(1))     {         // here     } } 

edit:

the reason onmouseup called tile clicked simple. behaviour safest , more expected way of time. think of mouse events of gameobject "user clicked on me" , "user released button after clicked on me".

so let's onmouseup called object under cursor @ time of mouse button release. think of operation starts on mouse click , going finalized when mouse button released (like drag , drop). painfully figure out onmouseup not called if mouse no longer points clicked object. won't able finalize operation. have catch mouse release event yourself.

in case, onmouseup , onmousedown alone won't cover needs. trying specific application. unfortunately unity's input system not cover kind of interaction default. luckily unity flexible enough implement easily.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -