Author Topic: UiButtonColor on game object  (Read 2419 times)

Chad Goodson

  • Guest
UiButtonColor on game object
« on: August 27, 2013, 08:43:12 AM »
I am trying to make a cube change color when pressed. I have attached the UiButtonColor script to the cube and a box collider. I also set the target on the script to be the cube. When I run the game and click the cube nothing happens. What am I doing wrong?

OnlineCop

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UiButtonColor on game object
« Reply #1 on: August 27, 2013, 09:15:28 AM »
If you are using the stock Cube, I would suggest using the TweenColor component instead.

  1. public Cube myCube; // Attach this to your cube
  2.  
  3. void Start() {
  4.    UIEventManager.Get(myCube.gameObject).onClick += MyCubeWasClicked; // <-- Tween the color faster if you want it to be "instantly" colorized
  5. }
  6.  
  7. void MyCubeWasClicked(GameObject go) {
  8.    TweenColor tween = TweenColor.Begin(go, 1.0f, Color.green);
  9. }
  10.  
  11. void Destroy() {
  12.    UIEventManager.Get(myCube.gameObject).onClick -= MyCubeWasClicked;
  13. }
  14.  

Chad Goodson

  • Guest
Re: UiButtonColor on game object
« Reply #2 on: August 27, 2013, 09:26:29 AM »
Thank you. Is this Java Script or C Sharp? Will I be able to use it on imported models too?

OnlineCop

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 51
    • View Profile
Re: UiButtonColor on game object
« Reply #3 on: August 28, 2013, 01:03:10 PM »
The example code I provided was in C#. You can convert C# to Javascript by swapping the order of the [type][variable] declarations to [variable]:[type], like so:
  1. public Cube myCube; // This is the C# style
  2. public myCube : Cube; // This is the Javascript style
  3.  
To convert functions, replace the return type (such as "void") with the word "function" and append its return type to the end:
  1. void MyCubeWasClicked(GameObject go) { // C# style
  2. function MyCubeWasClicked(go : GameObject) : void { // JS style, declares functions with a "function" keyword, and the return type goes on the end
  3.  

You can use the same NGUI-based tweens in the same way for imported models, but remember that by design, they only modify the current/specified GameObject: it does not traverse the hierarchy and modify the child GameObjects. This way, if your model is of a 3D face, applying a color tween will affect, say, the skin only but leave the rest of the children (like eyes, hair, eyebrows, etc.) alone. To modify those, you will need to recurse through its hierarchy yourself and apply the same tween to them all.