Author Topic: Button sending message to custom script  (Read 5864 times)

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Button sending message to custom script
« on: February 04, 2013, 11:04:46 AM »
I believe that I have this solved, but I'm not sure if this is the best way to do it.

I created a super basic custom button.
- I added the UIButtonMessage Script
   - 1st instance = Target = myObject, FunctionName = customStart, Trigger=OnPress
   - 2nd instance = Target = myObject, functionName = customStop, Trigger=OnRelease

I have a GameObject which I call myObject which has a basic script.
  - custom function function customStart() - sets myVar = 1;
  - custom function function customStop() - sets myVar = 0;

My debug logs tell me this works on both Windows and my Android device (ie press button = 1, release button = 0).

Is there a better way to do this?  I've been digging around for a few days and this is all I've found that really works.

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: Button sending message to custom script
« Reply #1 on: February 04, 2013, 12:56:29 PM »
For me this is the best way to design something like that:
I always have a controller / handler script, which is located on an empty gameobject in the scene. Then i have x of my event gameobjects such as buttons or for example checkboxes, with a event script on it. The controller / handler class has all the methods and saves all the needed references to gameobjects, prefabs and so on. From the event scripts i just call methods located at the handler gameobject.

Simple example
There are some buttons which should play a music track on click. I dont want that more sounds are played at once, so my setup would look something like that:

Just one MusicBoxHandler:
- MusicBoxHandler script, with a Play(Audioclip clip) method
- AudioSource

One or more MusicBoxEvents;
- Script which has a reference to the MusicBoxHandler and JUST calls methods from the handler.

Hope i could help you :)
Regards

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Button sending message to custom script
« Reply #2 on: February 04, 2013, 01:02:10 PM »
Sounds like a good solution, but I'm not really sure I know how to do this.

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: Button sending message to custom script
« Reply #3 on: February 04, 2013, 01:05:02 PM »
Where's the problem respectively, whats the task you're working on?

Regards

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Button sending message to custom script
« Reply #4 on: February 04, 2013, 01:08:47 PM »
High level -
I want to set up some Buttons that drive the actions of an object/character/vehicle/whatever
I want to set up some text that changes based on the object (like damage, score, whatever)
I want to set up some other ngui that changes based on the object (like health bar, texture that is fully highlighted based on percentage)

So all the above are driven by a script.  I can't figure out a clean way to make my script communicate with all the different NGUI.

I suppose my issue is how to link my script to my object and to NGUI.

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Button sending message to custom script
« Reply #5 on: February 04, 2013, 02:28:55 PM »
bah, easy fix

function myFunction(buttonObject : GameObject){
  var buttonName:String=buttonObject.name;
  if(buttonName == "startButton")
      dosomething();
}