Unity 2018 Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

To set a custom mouse pointer when the mouse moves over a UI control GameObject,
do the following:

  1. Create a new Unity 2D project.
  2. Import the provided IconsCursors folder. Select all three images in the Project panel, and in the Inspector change the Texture Type to Cursor. This will allow us to use these images as mouse cursors without any errors occurring.
  3. Import the provided _Scripts folder, containing the CustomCursorPointer C# script class.
  4. Add a UI Button GameObject to the scene, leaving this named as Button.
  1. Add an instance of the CustomCursorPointer C# script class to the Button GameObject.
  2. With the Button GameObject selected in the Hierarchy, drag the CursorZoom image into the public Cursor Texture 2D variable slot in the Inspector for the Customer Cursor Pointer (Script) component.
  3. In the Inspector, add an Event Triggers component to GameObject Button. Choose menu: Add Component | Event | Event Trigger.
  4. Add a PointerEnter event to your Event Trigger component, click on the plus (+) button to add an event handler slot, and drag GameObject Button into the Object slot.
  5. From the Function drop-down menu, choose CustomCursorPointer and then choose the OnMouseEnter method:
  1. Add a Pointer Exit event to your Event Trigger component, and make it call the OnMouseExit() method from CustomCursorPointer when this event is received.
  2. Save and run the current Scene. When the mouse pointer moves over the UI Button, it will change to the custom CursorZoom image that you chose.