123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- /// <summary>
- /// Fire input controller is responsible for detecting clicks through NGUI.
- /// This resides on a gameObject with a box collider inside the UI hierarchy.
- /// The collider should be at the back of the UI hierarchy. This will allow
- /// pickups and buttons to have higher priority than firing.
- /// </summary>
- //[RequireComponent(typeof(BoxCollider))]
- public class FireInputController : MonoBehaviour
- {
- bool _isTakingInput = true;
- bool _hasMouseBeenPressed;
- private PointerEventData _data;
- public Canvas Canvas;
- static CanvasScaler _canvasScaler;
- public static Vector3 UnscaleEventDelta(Vector3 vec)
- {
- Vector2 referenceResolution = _canvasScaler.referenceResolution;
- Vector2 currentResolution = new Vector2(Screen.width, Screen.height);
- float widthRatio = currentResolution.x / referenceResolution.x;
- float heightRatio = currentResolution.y / referenceResolution.y;
- float ratio = Mathf.Lerp(widthRatio, heightRatio, _canvasScaler.matchWidthOrHeight);
- return vec / ratio;
- }
- void Awake()
- {
- _canvasScaler = Canvas.GetComponent<CanvasScaler>();
- NotificationCenter.AddListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.AddListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.AddListener(OnStartReplay, NotificationType.StartReplay);
- NotificationCenter.AddListener(OnEndReplay, NotificationType.EndReplay);
- }
- void OnDestroy()
- {
- NotificationCenter.RemoveListener(OnGameStart, NotificationType.GameStart);
- NotificationCenter.RemoveListener(OnStartSpawningEnemies, NotificationType.StartSpawningEnemies);
- NotificationCenter.RemoveListener(OnStartReplay, NotificationType.StartReplay);
- NotificationCenter.RemoveListener(OnEndReplay, NotificationType.EndReplay);
- }
- public void OnPress(bool isPressed)
- {
- if (isPressed &&
- _isTakingInput)
- {
- #if LEVEL_EDITOR
- //ignore clicks for the level editor controls
- if ((Input.mousePosition.x < 250 && Input.mousePosition.y > Screen.height - 200) ||
- (Input.mousePosition.y < 50)) //for the scroll bar
- {
- return;
- }
- #endif
- //process it in FixedUpdate for recording ticks purposes and playing back 100% reliably
- _hasMouseBeenPressed = true;
- }
- }
- public void OnBeginDrag(PointerEventData data)
- {
- _data = data;
- Debug.Log("OnBeginDrag: " + data.position);
- }
- public void CustomFixedUpdate()
- {
- if (_hasMouseBeenPressed/* && _data!=null*/)
- {
- var vec = UnscaleEventDelta(Input.mousePosition);
- NotificationCenter.Post(NotificationType.Fire, new Vector2(vec.x, vec.y));
- _hasMouseBeenPressed = false;
- }
- }
- void OnGameStart(Notification note)
- {
- _isTakingInput = false;
- }
- void OnStartSpawningEnemies(Notification note)
- {
- _isTakingInput = true;
- }
- void OnStartReplay(Notification note)
- {
- _isTakingInput = false;
- }
- void OnEndReplay(Notification note)
- {
- _isTakingInput = true;
- }
- }
|