Advertisement
Berhoh

Brimstone

Oct 11th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. //Clickable button for Unity
  2.  
  3. void Update()
  4.     {
  5.         StaticUpdate();
  6.  
  7.         if (Enabled)
  8.         {
  9.             var mouseInRect = false;
  10.             //CHECK IF MOUSE IS IN RECTANGLE
  11.             if (_cam != null)
  12.             {
  13.                 _position = _cam.WorldToViewportPoint(_rectTransform.position);
  14.                 _position = new Vector2(_position.x * _resolution.x, _position.y * _resolution.y);
  15.  
  16.                 Vector2 mouse = Input.mousePosition;
  17.  
  18.                 var hw = _size.x * 0.28f;
  19.                 var hh = _size.y * 0.28f;
  20.  
  21.                 mouseInRect = (mouse.x > _position.x - hw && mouse.x < _position.x + hw &&
  22.                                mouse.y > _position.y - hh && mouse.y < _position.y + hh);
  23.             }
  24.             else
  25.             {
  26.                 if (_cam == null)
  27.                     _cam = GameObject.Find("Camera").GetComponent<Camera>();
  28.                 if (_cam == null)
  29.                     _cam = GameObject.Find("Main Camera").GetComponent<Camera>();
  30.             }
  31.  
  32.             //CHECK IF HOVERING
  33.             if (_image != null)
  34.                 _image.color = (mouseInRect || _highlight) ? ColorSelected : ColorUnselected;
  35.  
  36.             //IF MOUSE CLICKED IN RECTANGLE
  37.             if (mouseInRect && Input.GetKeyDown(KeyCode.Mouse0))
  38.                 InvokeEvents();
  39.         }
  40.         else
  41.         {
  42.             //Set color if disabled
  43.             if (_image == null) return;
  44.             _image.color = ColorDisabled;
  45.         }
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement