Author Topic: Scripting UI Event Triggers  (Read 7673 times)

arumiat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Scripting UI Event Triggers
« on: July 26, 2014, 01:39:20 PM »
I am both a Unity beginner and nGUI extreme beginner so bear with me please ;)

  • (1) I would like to create one button that toggles the meshrenderer on & off for a number of meshes in my scene
  • (2) I would like to create a button that plays an animation.
  • I'd like for them to work with both onClick and onPress (desktop and tablet)

So I've created my buttons in nGUI using labels and added a box collider to them each, enabling 'Is Trigger'.

I've added a UIEvent Trigger script to them both as well (as I read button scripts only cater for onClick).

Re (1)
Any chance you can give me a hand C# scripting this? Using OnGUI, and with the object called T13_L in the Inspector I would do something like...
  1. public bool T13_L = false;
  2.  
  3. void Update ()
  4.         if T13L == true;
  5.         {
  6.                 MeshRenderer T13L GameObject.Find ("T13_L") <GetComponent.MeshRenderer()>
  7.                 T13L.renderer.enabled = !T13L.renderer.enabled
  8.         }

Now I need to make my own public class right?
So something like

  1.  
  2. public void toggleMeshesLSide ()
  3. {
  4. MeshRenderer T13L GameObject.Find ("T13_L") <GetComponent.MeshRenderer()>
  5.                 T13L.renderer.enabled = !T13L.renderer.enabled
  6. }
  7.  
  8. Drag this into the onClick slot and onPress slot on the UIEvent Trigger and should be good to go? Didn't work for me earlier...
  9.  


----------------------------------------------------------------------
Re (2)
My animation clip is called "X_impulse". The animator is called "X" as this is the game object it's attached to obviously.

  1. public  void playAnimation()
  2.     {
  3.         animation.Play();
  4.     }
  5.  

Now I can attach this to the gameobject in question, then drag the gameobject to the UI Event trigger slots for onClick and onPress? Didn't work for me earlier either.


Or could I have a script referencing multiple animations using

  1.  public void X_impulse()
  2.     {
  3.         animation.Play("X impulse");
  4.     }
  5.  
  6.     void X_impulse2()
  7.      {
  8.         animation.Play("X impulse 2");
  9.     }
  10.  
  11.  

And then use the dropdown menu to select the correct animation?

I realise there's probably all sorts of mistakes in there, apologies.

All help appreciated,
A


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scripting UI Event Triggers
« Reply #1 on: July 27, 2014, 03:21:34 AM »
You're complicating this too much. First, you don't even need a button here, and OnClick works on both desktop and mobile platforms. Only OnHover doesn't work on mobiles.

To set up a basic OnClick event handler, on any object -- not just on the button -- all you need to do is have the "void OnClick () {}" function present in a script attached to an object with a collider. Assuming the camera that sees this collider has a UICamera script attached, OnClick will be triggered when you click/tap on the object.

The UIButton component lets you choose any public non-static function just by dragging the object into the Notify field then choosing a function to call. This functionality is one layer above the "OnClick" function -- and is indeed the button script itself has the OnClick() function of its own under the hood that it then uses to call your remote target function.

I would strongly advise you to watch NGUI's tutorial videos. They explain all the basics thoroughly, including how events work and how to trigger your own functions.

Also note the UIPlayAnimation script. You can use it to trigger an animation.

arumiat

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Scripting UI Event Triggers
« Reply #2 on: July 27, 2014, 03:34:00 AM »
Thanks Aren that's very helpful. Will check out the tutorials again.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Scripting UI Event Triggers
« Reply #3 on: July 27, 2014, 09:02:03 AM »
http://www.tasharen.com/forum/index.php?topic=6711

Do something like this

  1. class MyButtonScript: MonoBehavior
  2. {
  3.         void OnClick()
  4.         {
  5.                 //Press down and up on the same collider
  6.                 //turn things on/off in here.
  7.         }
  8.        
  9.         void OnPress(bool pressedDown)
  10.         {
  11.                 if (pressedDown)
  12.                 {
  13.                         //touch down
  14.                 }
  15.                 else
  16.                 {
  17.                         //touch up.
  18.                 }
  19.         }
  20. }