I'd just like to add that changing your own input detection to use the NGUI's event system will be far, far easier. :)
Btw, what I did in Windward is simply attach a UICamera script to the main game camera, and set its event mask to "nothing". I then set the UICamera.fallThrough to my "game event listener" script, and all the events that don't get handled by the UI simply go to that script.
one question:UICamera.lastTouchID tells you the ID of the touch / mouse / controller event that triggered OnClick, OnPress, etc function that you're in.
how do you handle multiple touches (for zoom and rotation) at the same time with ngui interface for on mousedown etc
We ran into this problem ourselves with our game project. Since the input controller for our game was already well established by the time we decided to investigate NGUI, it was not practical to replace our current working input system with NGUI input handling.This solution is really good! I was in the exact same situation where I needed to make minimal impact on an existing project, and this dropped in cleanly. So thank you for contributing this!
In order to solve the problem of input fall-through, I made sure that all NGUI elements are on the UI layer, and I implemented the following in our game's main input controller:
// This grabs the camera attached to the NGUI UI_Root object. Camera nguiCam = GameController.Instance.GameUI.NGUI_Manager.GetComponentInChildren<Camera>(); if( nguiCam != null ) { // pos is the Vector3 representing the screen position of the input Ray inputRay = nguiCam.ScreenPointToRay( pos ); RaycastHit hit; if( Physics.Raycast( inputRay.origin, inputRay.direction, out hit, Mathf.Infinity, LayerMask.NameToLayer( "UI" ) ) ) { // UI was hit, so don't allow this input to fall through to the gameplay input handler } }
GetComponentInChildren is a slow operation, so using it every frame is a bad idea. UICamera.mainCamera gives you the camera instead.Ah yes, that should have been cached. Now I don't have to though, since you already have it available.
Casting a ray into the screen is also unnecessary, assuming "pos" is mouse position. UICamera.hoveredObject already tells you what's under the mouse.
"hover object is always the fall-through game object" that makes no sense to me. Did you mean that in reverse?
Generally when dragging you should disable the collider of whatever you started dragging, or it will be intercepting events.
Also when you can always modify UICamera.currentTouch.pressed game object to be something else, and that something else will receive your OnClick instead.
Btw, what I did in Windward is simply attach a UICamera script to the main game camera, and set its event mask to "nothing". I then set the UICamera.fallThrough to my "game event listener" script, and all the events that don't get handled by the UI simply go to that script.
Btw, what I did in Windward is simply attach a UICamera script to the main game camera, and set its event mask to "nothing". I then set the UICamera.fallThrough to my "game event listener" script, and all the events that don't get handled by the UI simply go to that script.