Hello Forum,
i came up with this issue while having problems detecting if my mouse is on an UI-Element or in the 3D World. The mistake was, that i had a custom raycast-function going to detect what object in the 3D world the mouse is touching and ngui also does this, for his UI Elements. So i read a lot about it in this Forum and in the documentation and found the advise to use NGUI´s raycast, instead of custom ones, but up to this point i´m not getting how to achieve this.
What i think i got right (pls correct me if something is wrong):
- Having an UICamera on any ingame Camera means NGUI-Raycasts against the set layermaks.
- If those raycasts hit something, colliders or rigidbodys, you get access to the NGUI-Events like OnClick, OnHover etc in those gameobjects.
- If nothing on the set layermask is hit, you can forward the eventsystem to a custom eventhandler on any gameobject using UICamera.fallThrough = this.gameObject; You then have access to these Events in this GO.
For serperating UI-hits from 3D World hits, ArenMook suggested in an other thread the following setup (if i got this right, pls also correct):
- UI-Camera has for sure UICamera-Script, Event-Type set to 2D/3D UI, EventMask set to UI only
- Main-Camera has also UICamera-Script, Event-Type set to 3D-World, EventMask set to nothing
Is it possible that this setup is outdated, cause the current version gives a fallThrough in any case and the UICamera is not needed on the Main Camera anymore? The thread is from 2012:
http://www.tasharen.com/forum/index.php?topic=138.0Assuming that i got it more or less right up to this point i´m trying to use those fallThrough Events for detecting which gameObject in the 3D world was hit, without using additional raycasting. I have the UICamera-Script only on my UI-Camera , set to UI-only, rest of the hits fall through my EventHandler, though i also tried the setup with UICamera on MainCamera but it seems to make no diffrence.
My problems:
I need information what 3D-world, non UI GameObject was hit by the fallThrough event, like i would check it wth a raycast. All functions i tried so far didn´t provide this information or did i missed something?
- OnHover-Event seems to only trigger, when the fallThrough gets triggered, means when i switch from UI to non UI Element. It seems not to trigger constantly when i´m hovering over any 3DWorld colliders.
- UICamera.lastHit doesn´t give me the current hit Object
public void OnClick()
{
Vector3 obj = UICamera.lastHit.transform.position;
Debug.Log(obj);
}
It seems to refere to the 2D UI-Layer, cause all positions i get are like (a, b, 0)
I also have started a workaround using the fallThrough system only to check if we are on UI or not and then do some custom raycasting to check what i´m hitting in the 3DWorld, but if i got all the other informations right, this is not the way things should be done here.
thx in advance
Marcus