void Awake() { this.mainCamera = Camera.main; this.mouseState = MouseStateEnum.DEFAULT; this.mouseIsHeldDownOneFrame = false; this.mouseIsReleasedOneFrame = false; }
void Update() { // this Update() must run before anything else. this.oldMousePos = this.mousePos; this.mousePos = GetMousePos(); // changes CLICKED to HELD and UNCLICKED to DEFAULT after a new frame begins switch (this.mouseState) { case MouseStateEnum.CLICKED: this.mouseState = MouseStateEnum.HELD; break; case MouseStateEnum.UNCLICKED: this.dragOffset = Vector3.zero; this.oldDragOffset = Vector3.zero; this.clickedPos = Vector3.zero; this.mouseState = MouseStateEnum.DEFAULT; break; } // detect mouse button down and up to set CLICKED and UNCLICKED if (Input.GetMouseButtonDown(0)) { this.mouseState = MouseStateEnum.CLICKED; } else if (Input.GetMouseButtonUp(0)) { this.mouseState = MouseStateEnum.UNCLICKED; } switch (this.mouseState) { case MouseStateEnum.DEFAULT: break; case MouseStateEnum.CLICKED: // runs once for one frame before mouseState changes to HELD print("GameManger - clicked: " + this.mousePos); this.clickedPos = this.mousePos; OnClickDown(); break; case MouseStateEnum.HELD: this.oldDragOffset = this.dragOffset; this.dragOffset = this.mousePos - this.clickedPos; OnHoldUpdate(); break; case MouseStateEnum.UNCLICKED: // runs once for one frame before mouseState changes to DEFAULT print("GameManger - unclicked: " + this.mousePos + " offset: " + this.dragOffset); OnUnclick(); break; } }
void Update() { this.isCursorOverUI = EventSystem.current.IsPointerOverGameObject(); if (this.mouseIsHeldDownOneFrame) { this.mouseIsHeldDownOneFrame = false; this.mouseState = MouseStateEnum.HELD; } if (this.mouseIsReleasedOneFrame) { this.mouseIsReleasedOneFrame = false; this.mouseState = MouseStateEnum.DEFAULT; } // this Update() must run before anything else. this.oldMousePos = this.mousePos; this.oldMousePosV2 = this.mousePosV2; this.mousePos = GetMousePos(); this.mousePosV2 = Util.V3ToV2I(this.mousePos); switch (this.mouseState) { case MouseStateEnum.DEFAULT: break; case MouseStateEnum.CLICKED: // runs once for one frame before mouseState changes to HELD this.clickedPos = this.mousePos; this.clickedPosV2 = Util.V3ToV2I(this.clickedPos); // OnClickDown(); this.mouseIsHeldDownOneFrame = true; break; case MouseStateEnum.HELD: // runs PER FRAME while mouse is held down this.oldDragOffset = this.dragOffset; this.dragOffset = this.mousePos - this.clickedPos; this.dragOffsetV2 = this.mousePosV2 - this.clickedPosV2; break; case MouseStateEnum.RELEASED: // runs once for one frame before mouseState changes to DEFAULT this.dragOffset = Vector3.zero; this.oldDragOffset = Vector3.zero; this.clickedPos = Vector3.zero; this.mouseIsReleasedOneFrame = true; break; default: throw new ArgumentOutOfRangeException(); } }