Author Topic: Custom mouse cursor  (Read 14231 times)

catacomber

  • Guest
Custom mouse cursor
« on: July 20, 2012, 11:35:06 PM »
I'd like to try a custom mouse cursor. I have script and an image and know how to do it using Unity GUI texture. Is there a way I can make a custom mouse cursor using NGUI? I did a search here to try to find something but couldn't. The mouse would turn a different color over certain objects.


var controlCenter : GameObject;
private var mouseOverColor : Color;


function Start () {

   guiTexture.enabled = false;  // disable the GUITexture object at start up
   mouseOverColor= controlCenter.GetComponent(GameManager).mouseOverColor;

}

function Update () {
   // gets the current cursor position as a Vector2 type variable
   var pos = Input.mousePosition;

   // feed its x and y positions back into the GUI Texture object’s parameters
   guiTexture.pixelInset.x = pos.x;
   guiTexture.pixelInset.y = pos.y - 32; // offset to top
}

function CursorColorChange (colorize: boolean) {

   if (colorize)  guiTexture.color = mouseOverColor;

   else  guiTexture.color = Color.white;
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom mouse cursor
« Reply #1 on: July 20, 2012, 11:54:32 PM »
I'm sorry, but my javascript skills are weak to say the least. I can only help out with C# questions.

For a custom mouse cursor all you have to do is have a game object follow the mouse -- implying inside Update() it would reposition itself to Input.mousePosition (or more accurately -- using the camera convert from screen to world coordinates, and position the cursor game object there).

I suggest you place it in a panel of its own so its movement doesn't cause buffers to be rebuilt.

catacomber

  • Guest
Re: Custom mouse cursor
« Reply #2 on: July 21, 2012, 12:00:51 AM »
Thanks, I just needed to know how to place it---will try inside a panel and then try to figure out how to refer to the object in the script---it's not going to be a guiTexture---will see what I can do.  Thanks.  :  )  Am learning C#-------- 

I may not even use it but wanted to try it. 

catacomber

  • Guest
Re: Custom mouse cursor
« Reply #3 on: July 21, 2012, 12:06:02 AM »
I'm interested in Windward by the way but have too many things to do right now with learning NGUI to think about it.  :  )  But it's intriguing and I see you posted about letting people make their own maps or something similar-----  :  ) 

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom mouse cursor
« Reply #4 on: July 21, 2012, 12:21:55 AM »
For the math behind it, check to see how the tooltip is placed (UITooltip). It's the same for the mouse cursor, except you'll be doing that every Update.

catacomber

  • Guest
Re: Custom mouse cursor
« Reply #5 on: July 22, 2012, 10:56:27 PM »
I just got your Ship Game Starter Kit for the good reviews it got. :  ) Any little help I can get in seeing how NGUI works is a good thing cause you never know what you might want to do.  :  ) 

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom mouse cursor
« Reply #6 on: July 22, 2012, 10:58:19 PM »
Ship game doesn't have NGUI though. That was the first kit I created (about a year before NGUI). Are you going to be adding NGUI to it?

catacomber

  • Guest
Re: Custom mouse cursor
« Reply #7 on: July 22, 2012, 11:06:48 PM »
I guess I will.  :  )  It doesn't matter. It got such excellent reviews I'm sure I'll learn something useful.  :  )

catacomber

  • Guest
Re: Custom mouse cursor
« Reply #8 on: July 22, 2012, 11:22:05 PM »
I'm making an rpg game with Unity--all I know are rpg games. I need NGUI for the main menu and mostly for dialogues with npc's, simple messages, some monster interaction in dialogues and a quest log and, maybe if I can integrate it, an inventory. But I loved the look of your ship game and of course the comments you got are good and am always curious about things.  :  )  I'm sure it will teach me something useful and besides I just wanted to support your good work. :  )

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom mouse cursor
« Reply #9 on: July 23, 2012, 05:28:29 AM »
:)

joreldraw

  • Guest
Re: Custom mouse cursor
« Reply #10 on: July 23, 2012, 07:50:28 AM »
Here is a custom cursor script i wrote for my project. This is personalized for some kind of object.
But i think you can understand and adapt for you.

  1. //----------------------------------------------
  2. // Custom Cursor: Customize cursor based on NGUI objects
  3. // Writted by: Joreldraw
  4. //----------------------------------------------
  5.  
  6.  
  7. // Use:  Add to your main or simple attach to a Empty child.
  8.  
  9. using UnityEngine;
  10. using System.Collections;
  11.  
  12.  
  13. public class CustomCursor : MonoBehaviour {
  14.        
  15.         private Texture2D cursorImage;
  16.         public Texture2D PointerCursor;
  17.         public Texture2D ClickCursor;
  18.         public Texture2D ClickedCursor;
  19.         public Texture2D TextCursor;
  20.         public Texture2D MoveCursor;
  21.         public Texture2D MovingCursor;
  22.         public Texture2D RotateCursor;
  23.         private GameObject Seleccion;
  24.         private bool Dragging = false;
  25.        
  26.        
  27.         void Start()
  28.         {
  29.                 if (Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer)
  30.                 {
  31.                
  32.             Screen.showCursor = false;
  33.                 cursorImage = PointerCursor;
  34.                 }
  35.         }
  36.  
  37.         void OnGUI()
  38.         {
  39.         if (Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer)
  40.                 {
  41.                                 Seleccion = null;
  42.                                
  43.                             Vector3 mousePos = Input.mousePosition;
  44.                                
  45.                             Rect pos = new Rect (mousePos.x,Screen.height - mousePos.y,cursorImage.width,cursorImage.height);
  46.                                
  47.                                
  48.                                
  49.                                 if (UICamera.hoveredObject)
  50.                        
  51.                                 {
  52.                                         Seleccion =  UICamera.hoveredObject.transform.gameObject ;
  53.                                 }
  54.                                 else Seleccion = null;
  55.                                
  56.                                
  57.                                 if (Seleccion)
  58.                                 {
  59.                                         if (TextCursor)
  60.                                         {
  61.                                         UIInput NguiInput = Seleccion.GetComponentInChildren<UIInput>();
  62.                                        
  63.                                                 if (NguiInput) cursorImage = TextCursor;
  64.                                         }
  65.                                        
  66.                                        
  67.                                        
  68.                                         if (ClickCursor)
  69.                                         {
  70.                                         UIButtonColor NguiButton = Seleccion.GetComponent<UIButtonColor>();
  71.                                         UIButtonSound NguiSound = Seleccion.GetComponent<UIButtonSound>();
  72.                                         MeshRenderer Rigido = Seleccion.GetComponent<MeshRenderer>();
  73.                                                
  74.                                                 if (NguiButton || NguiSound || Rigido)
  75.                                                 {
  76.                                                 cursorImage = ClickCursor;
  77.                                                 if(Input.GetMouseButton(0) && ClickedCursor){ cursorImage = ClickedCursor;}
  78.                                                 }
  79.                                                
  80.                                         }
  81.                                         if (RotateCursor)
  82.                                         {
  83.                                         Rotando RotarScript = Seleccion.GetComponent<Rotando>();
  84.                                        
  85.                                                 if (RotarScript) cursorImage = RotateCursor;
  86.                                         }
  87.                                
  88.                                         if (MoveCursor)
  89.                                         {
  90.                                         UIDragObject NguiDragable = Seleccion.GetComponent<UIDragObject>();
  91.                                                
  92.                                                          if (NguiDragable && !Dragging) cursorImage = MoveCursor;
  93.                                                 else if (NguiDragable && Dragging ) {cursorImage = MovingCursor;}              
  94.                                         }
  95.                                
  96.                                
  97.                                        
  98.                                
  99.                                 }
  100.                                
  101.                             GUI.Label(pos,cursorImage);
  102.                 }
  103.         }
  104.        
  105.        
  106.         void Update()
  107.         {
  108.                 if (Application.platform != RuntimePlatform.Android && Application.platform != RuntimePlatform.IPhonePlayer)
  109.                 {
  110.                
  111.                         Vector2 mouseFlatPos = new Vector2 (Input.mousePosition.x,Input.mousePosition.y);
  112.                         if (mouseFlatPos != UICamera.lastTouchPosition && cursorImage != PointerCursor) cursorImage = PointerCursor;
  113.                        
  114.                         if (!Dragging)
  115.                         {
  116.                                 if(Input.GetMouseButtonDown(0)) { Dragging = true;}
  117.                                
  118.                         }
  119.                        
  120.                         if (Dragging)
  121.                         {
  122.                                 if(Input.GetMouseButtonUp(0)){ Dragging = false;}
  123.                                
  124.                         }
  125.                        
  126.                
  127.                        
  128.                 }
  129.         }
  130.                
  131. }
  132.  

catacomber

  • Guest
Re: Custom mouse cursor
« Reply #11 on: July 24, 2012, 12:00:59 AM »
Thank you so much. I'll try it!   :)

abreu20011

  • Guest
Re: Custom mouse cursor
« Reply #12 on: January 31, 2013, 10:05:59 PM »
Just attach a Cursor to one sprite, and then the spirit follows the mouse.
Later write this sentence

  1. Screen.showCursor = false;

renderhjs

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Custom mouse cursor
« Reply #13 on: March 11, 2014, 08:28:20 PM »
Here is some snippet I wrote today to get the proper local coordinate based on the raw screen coordinate

  1. public static Vector3 GetScreenToGuiSpace(Vector3 pos, Transform localSpace, Camera cameraNGUI){
  2.  
  3.                 // Since the screen can be of different than expected size, we want to convert
  4.                 // mouse coordinates to view space, then convert that to world position.
  5.                 pos.x = Mathf.Clamp01(pos.x / Screen.width);
  6.                 pos.y = Mathf.Clamp01(pos.y / Screen.height);
  7.                
  8.                 //PROJECT INTO WORLD SPACE
  9.                 Vector3 posWorld = cameraNGUI.ViewportToWorldPoint(pos);//ABSOLUTE
  10.                
  11.                 //CONVERT WORLD SPACE TO LOCAL SPACE
  12.                 return localSpace.InverseTransformPoint(posWorld);
  13.                
  14.                
  15.         }

Use it like this
  1. spriteCursor.transform.localPosition = GetScreenToGuiSpace( Input.mousePosition, spriteCursor.transform.parent, nguiCamera);
Where nguiCamera is a Camera reference of the Camera that is used by your NGUI setup in the scene.

Hope this helps others

flavoredCoffee

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 15
    • View Profile
    • CWGTech Unity & Programming blog
Re: Custom mouse cursor
« Reply #14 on: April 13, 2014, 09:20:10 AM »
renderhjs - your code works fine when the camera covers the entire screen, but if not, then you'll need to use:

  1.         pos.x = Mathf.Clamp01(pos.x / cameraNGUI.pixelWidth);
  2.         pos.y = Mathf.Clamp01(pos.y / cameraNGUI.pixelHeight);
  3.  

Just a minor point, and thanks for sharing.