Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Teonnyn on April 18, 2017, 01:50:21 PM

Title: NGUI - Improving touch?
Post by: Teonnyn on April 18, 2017, 01:50:21 PM
Under NGUI 3, what would be the best way to improve touch detection? I'm using the fallthrough and NGUI OnPress/OnClick in my main program to detect game clicks in a mobile game.

  1. UICamera.fallThrough = this.gameObject;


  1.    
  2. public void OnClick()
  3.     {
  4.         bool mouseClick = true;
  5.         DetectMouseClick(mouseClick);
  6.     }
  7.  
  8.     public void OnPress(bool isPressed)
  9.     {
  10.         DetectMouseClick(isPressed);
  11.     }
  12.  
  13.     public void MoveHitObject(RaycastHit hit)
  14.     {
  15.         hitObject.gameObject.transform.position = hit.transform.position;
  16.         hitObject.IsActive = true;
  17.     }
  18.  
  19.     public void DetectMouseClick(bool Pressed)
  20.     {
  21.         if (!enableGameClickTimer && !isPaused)
  22.         {
  23.             RaycastHit hit;
  24.             Ray ray = gameCamera.ScreenPointToRay(UICamera.lastEventPosition);
  25.  
  26.             if (Physics.Raycast(ray, out hit))
  27.             {
  28.                 MoveHitObject(hit);
  29.  
  30.                 switch (hit.transform.tag)
  31.                 {
  32.                     case "Ground":
  33.                     case "Brick":
  34.                         PurchaseBrick();
  35.                         break;
  36.                 }
  37.  
  38.             } else
  39.             {
  40.                 PurchaseBrick();
  41.             }
  42.  
  43.            
  44.             enableGameClickTimer = true;
  45.         }
  46.  
  47.     }

When either is detected, a ray is cast into the game world and an object moved to encounter other objects that the touch might have intercepted. However, my testers are reporting that
this only works roughly half the time. Touch seems to be spotty, but the game relies on it. What can I do to improve the odds that an object is detected properly?

Title: Re: NGUI - Improving touch?
Post by: ArenMook on April 22, 2017, 11:24:11 AM
You can subscribe directly to the event delegates: UICamera.onPress, UICamera.onClick, etc. Those will be called regardless of what the action will be called on, letting you react to multiple objects without having to attach anything to them.