Advertisement
Guest User

GameCanvas Template

a guest
Apr 23rd, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. /*
  6. http://stackoverflow.com/questions/36809687/ui-canvas-image-with-ui-buttons
  7. */
  8.  
  9. public class GameCanvas : MonoBehaviour
  10. {
  11. public Button pauseButton;
  12. public Button jumpButton;
  13.  
  14. public Canvas pauseCanvas;
  15.  
  16. void OnEnable()
  17. {
  18. //Register Button Events
  19. pauseButton.onClick.AddListener(() => buttonCallBack(pauseButton));
  20. jumpButton.onClick.AddListener(() => buttonCallBack(jumpButton));
  21. }
  22.  
  23. private void buttonCallBack(Button buttonPressed)
  24. {
  25. //Pause Button Pressed
  26. if (buttonPressed == pauseButton)
  27. {
  28. //Pause Game here
  29.  
  30. //Hide Game UI
  31. gameObject.SetActive(false);
  32.  
  33. //Show Pause UI
  34. pauseCanvas.gameObject.SetActive(true);
  35. }
  36.  
  37. //Jump Button Pressed
  38. if (buttonPressed == jumpButton)
  39. {
  40. jump();
  41. }
  42.  
  43. }
  44.  
  45. public void jump()
  46. {
  47. //Put your jump code here
  48. Debug.Log("Jumped");
  49. }
  50.  
  51. void OnDisable()
  52. {
  53. //Un-Register Button Events
  54. pauseButton.onClick.RemoveAllListeners();
  55. jumpButton.onClick.RemoveAllListeners();
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement