Author Topic: Flash: change cursor to hand when hovering (code included inline)  (Read 2527 times)

mtoivo

  • Guest
Hi.

I created  a little bit of code to change the mouse cursor when hovering over a button. It sends message to actionscript class, that does the actual change. I named my NGUI-class as "UIBUttonFlashCursor":

  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Changes mouse cursor in Flash
  5. /// </summary>
  6.  
  7. [AddComponentMenu("NGUI/Interaction/Button Flash Cursor")]
  8. public class UIButtonFlashCursor : MonoBehaviour
  9. {
  10.  
  11.         bool mStarted = false;
  12.         bool mHighlighted = false;
  13.  
  14.         void Start () { mStarted = true; }
  15.  
  16.         void OnEnable () { if (mStarted && mHighlighted) OnHover(UICamera.IsHighlighted(gameObject)); }
  17.  
  18.         void OnHover (bool isOver)
  19.         {
  20.                 if (enabled)
  21.                 {
  22.                         if(isOver)
  23.                         {
  24.                                 FlashProxy.showCursor("button");
  25.                         }
  26.                         else
  27.                         {
  28.                                 FlashProxy.showCursor("arrow");
  29.                         }
  30.                         mHighlighted = isOver;
  31.                 }
  32.         }
  33.        
  34.  
  35. }

As you can see, it calls a method showCursor from static class FlashProxy

Here's the AS-version (FlashProxy.as):

  1. package
  2. {
  3.         import flash.ui.Mouse;
  4.         import flash.ui.MouseCursor;
  5.  
  6.         public class FlashProxy
  7.         {
  8.  
  9.                 public static function showCursor(type:String):void
  10.                 {
  11.                         Mouse.cursor = type;
  12.                 }
  13.         }
  14. }
  15.  

And the corresponding JS-version, which is only to make things work smoothly in editor (FlashProxy.js):

  1. #pragma strict
  2.  
  3. @NotConverted
  4. @NotRenamed
  5. public class FlashProxy
  6. {
  7.         @NotRenamed
  8.         public static function showCursor(type:String):void
  9.         {
  10.         }
  11. }

As usual, the .as version goes to "ActionScript" -folder in your assets, but this time also the js-version has to be placed differently, so that it compiles before NGUI. I placed it inside Plugins.

Now when you want to have this cursor change in your button, just drag the "UIBUttonFlashCursor.cs" into it.

Sometimes the cursor will stay as "hand", if the button click removes to button underneath mouse or something like that. I wonder would it be possible to trap that event inside NGUI and change the cursor to arrow? Or even better, is there a simple way to integrate this functionality somewhere inside UIRoot or similar? That when ever mouse hovers on NGUI-element, it changes to hand? Now I just call FlashProxy.showCursor("arrow") in the code whenever I know this kind of "bug" is coming up.
« Last Edit: October 14, 2012, 04:31:33 PM by mtoivo »