Search Unity

Button press

Discussion in 'Project Tiny' started by vincismurf, Dec 10, 2018.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I was able to add a sprite with the button and mouse interaction components but how do I know when a button has been pressed?
     
  2. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
  3. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    Actually I was meaning a button in the project, here is the solution

    Code (JavaScript):
    1.  
    2. // A function in a system/behavior
    3. {...
    4.  
    5. // Find an entity Called "ForwardButton" -  It needs Sprite2dRenderer/Button/Mouse Interaction components on it
    6. let fowardButton = this.world.getEntityByName("FowardButton");
    7.  
    8. //set up listener for the button
    9. ut.UIControls.UIControlsService.addOnClickCallback(this.world,fowardButton, this.OnForwardPressed);
    10.  
    11. ...}
    12.  
    13. // Another Method in the same system/behavior for callback
    14. OnForwardPressed()
    15. {
    16.     console.log("pressed");
    17.  }
    18.  
    To Test, Press play and Inspect the web page, select Console and everytime you press the word "pressed" will be in the console
     
    ER_Dolleman likes this.
  4. WellC

    WellC

    Joined:
    Mar 2, 2015
    Posts:
    48
    This is a project tiny ui button example.
    hope useful to you.
     

    Attached Files:

  5. tbriley

    tbriley

    Joined:
    Sep 10, 2013
    Posts:
    115
    @vincismurf @WellC are these working for you in release builds? it errors out for me.
     
  6. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
  7. tbriley

    tbriley

    Joined:
    Sep 10, 2013
    Posts:
    115
    awesome, thanks!
     
unityunity