Author Topic: UICamera Touch  (Read 10717 times)

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
UICamera Touch
« on: February 04, 2013, 09:59:16 PM »
Does anyone have a helpful example script that talks to some of the UICamera touch events?  Not just for Buttons, but for sprites too.  I see with Buttons you can add UIButtonMessage, but it would be nice to get something a little more open that allows me to check any touch on the screen using a for() statement.

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: UICamera Touch
« Reply #1 on: February 05, 2013, 02:59:13 AM »
I dont really know if i understood your question, but i'll try it: Every Sprite used in NGUI with a collider is able to send events to your camera. The method OnClick() gets called, if you click with your mouse or touch the button / sprite on your mobile device. Events available: http://www.tasharen.com/?page_id=160

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UICamera Touch
« Reply #2 on: February 05, 2013, 07:42:52 AM »
I think what I need is some examples of how to use it without writing a C# void OnClick(){} script.  Is there something I can access in javascript, if so is there an example.  It would be awesome if the code had an example use besides the void OnClick().  I haven't had a chance to learn C# yet, but I do understand some of it from my C days back on text based games (MUDs).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UICamera Touch
« Reply #3 on: February 05, 2013, 09:59:44 AM »
  1. UIEventListener.Get(targetButtonGameObject).onClick = YourFunction;

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UICamera Touch
« Reply #4 on: February 05, 2013, 12:23:06 PM »
The only way I can figure out how to get this to work is to assign my target button.  I want it to auto-assign any button or sprite that might be pushed.  The below code works only if I assign the one Button in my scene to the targetButton.

  1. #pragma strict
  2. var targetButton:GameObject;
  3.  
  4. function Update () {
  5.  
  6.         UIEventListener.Get(targetButton).onClick=ItemClicked;
  7. }
  8.  
  9. function ItemClicked(btnObj : GameObject)
  10. {
  11.         var btnName : String = btnObj.name;
  12.        
  13.                 Debug.Log(btnName+" was pressed");
  14.        
  15. }

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UICamera Touch
« Reply #5 on: February 05, 2013, 01:17:02 PM »
Okay, so I went ahead and created an Array instead.  Now I just need to add more listener types to catch all possibilities. 

FYI - I had to add the UIImage Button Script to my Sprite/Image and set the Pressed/Normal/Hover.

  1. #pragma strict
  2. //var targetButton:GameObject;
  3. var targetButtons:GameObject[];
  4.  
  5. function Update () {
  6.         for(var me :GameObject in targetButtons){
  7.                 //UIEventListener.Get(targetButton).onClick=ItemClicked;
  8.                 UIEventListener.Get(me).onClick=ItemClicked;
  9.                 UIEventListener.Get(me).onPress=ItemClicked;
  10.         }
  11.        
  12. }
  13.  
  14. function ItemClicked(btnObj : GameObject)
  15. {
  16.         var btnName : String = btnObj.name;
  17.        
  18.                 Debug.Log(btnName+" was pressed");
  19.        
  20. }

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UICamera Touch
« Reply #6 on: February 11, 2013, 12:00:33 PM »
Here's my attempt at putting this same code in C#

  1. sing UnityEngine;
  2. using System.Collections;
  3.  
  4. public class UICameraTestingC : MonoBehaviour {
  5.        
  6.         public GameObject[] targetButtons;
  7.        
  8.         // Update is called once per frame
  9.         void Update () {
  10.                 foreach(GameObject me in targetButtons){
  11.                         //UIEventListener.Get(targetButton).onClick=ItemClicked;
  12.                         UIEventListener.Get(me).onClick=ItemClicked;
  13.                 }
  14.         }
  15.         public void ItemClicked(GameObject btnObj)
  16.         {
  17.        
  18.                         Debug.Log(btnObj.name+" was pressed");
  19.         }
  20. }

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: UICamera Touch
« Reply #7 on: February 11, 2013, 12:30:15 PM »
Here's a picture of what the array looks like in Unity.