Author Topic: Best way for raycast only UI  (Read 8459 times)

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Best way for raycast only UI
« on: September 07, 2014, 09:27:06 PM »
My issue is raycast when i touch UI this make raycast twice for UI and screen but i only wanna make UI touch. May someone show me how to solve this ?

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: Best way for raycast only UI
« Reply #1 on: September 07, 2014, 10:36:07 PM »
I used UICamera.hoverObject != null and work fine but only PopupList with dropdown list this not work when i touch item in dropdown list. So when i press an UI and drag it, i wanna stop raycast in gameplay scene too but i cant.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: Best way for raycast only UI
« Reply #2 on: September 08, 2014, 03:44:51 AM »
Another issue is mobile plaform which not has hover state, my first solution totally fail :3

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best way for raycast only UI
« Reply #3 on: September 08, 2014, 08:52:33 PM »
I don't quite understand you here... raycast twice for UI and screen? What does "make UI touch" mean?

UICamera is what controls which cameras receive events. If you remove this script from a camera, objects that are drawn by that camera won't be receiving events. If you only have UICamera on the camera that draws your UI, then only UI elements will receive events.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: Best way for raycast only UI
« Reply #4 on: September 09, 2014, 11:47:10 AM »
Sorry for my bad English, I have 1 button and character 3d model behind it. Character will move if i touch on screen and it make me crazy when i touch button ngui and character move too. But i solved my problems by this code in UICamera

  1. Ray ray = UICamera.currentCamera.ScreenPointToRay(m_vTargetPos);
  2.             RaycastHit currentTouchorMouse;
  3.             Physics.Raycast(ray, out currentTouchorMouse, 1000);
  4.  
  5.                 if (currentTouchorMouse.collider.gameObject.layer == LayerMask.NameToLayer("UI"))
  6.                 {
  7.                     return;
  8.                 }

Thank you for reply me :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Best way for raycast only UI
« Reply #5 on: September 10, 2014, 12:54:14 AM »
Don't modify the UICamera.

Change the Event Mask on the UICamera instead.

Also make sure that your Main Camera doesn't draw your UI layer, and that your UI camera doesn't draw your 3D layers.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: Best way for raycast only UI
« Reply #6 on: September 10, 2014, 09:39:20 AM »
Ya I just copy a part code of UICamera in Raycast method and use it for dectected what object selected. May u think Raycast method in UICamera should be return a gameobject ?