Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Clickable button for Unity
- void Update()
- {
- StaticUpdate();
- if (Enabled)
- {
- var mouseInRect = false;
- //CHECK IF MOUSE IS IN RECTANGLE
- if (_cam != null)
- {
- _position = _cam.WorldToViewportPoint(_rectTransform.position);
- _position = new Vector2(_position.x * _resolution.x, _position.y * _resolution.y);
- Vector2 mouse = Input.mousePosition;
- var hw = _size.x * 0.28f;
- var hh = _size.y * 0.28f;
- mouseInRect = (mouse.x > _position.x - hw && mouse.x < _position.x + hw &&
- mouse.y > _position.y - hh && mouse.y < _position.y + hh);
- }
- else
- {
- if (_cam == null)
- _cam = GameObject.Find("Camera").GetComponent<Camera>();
- if (_cam == null)
- _cam = GameObject.Find("Main Camera").GetComponent<Camera>();
- }
- //CHECK IF HOVERING
- if (_image != null)
- _image.color = (mouseInRect || _highlight) ? ColorSelected : ColorUnselected;
- //IF MOUSE CLICKED IN RECTANGLE
- if (mouseInRect && Input.GetKeyDown(KeyCode.Mouse0))
- InvokeEvents();
- }
- else
- {
- //Set color if disabled
- if (_image == null) return;
- _image.color = ColorDisabled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement