Search Unity

Help with on mouse over code

Discussion in 'Editor & General Support' started by FalsAlarm, Nov 14, 2017.

  1. FalsAlarm

    FalsAlarm

    Joined:
    Dec 12, 2016
    Posts:
    21
    Below is my code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.IO;
    6.  
    7. public class StartButtonManager: MonoBehaviour {
    8.  
    9.     CursorMode cursorMode = CursorMode.Auto;
    10.  
    11.     public Texture2D handCursorTexture;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         handCursorTexture = LoadPNG("./Assets/hand_cursor.png");
    16.         //handCursorTexture.LoadImage();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.     }
    23.     public void OnMouseEnter()
    24.     {
    25.         print("cursor placed!");
    26.         Cursor.SetCursor(handCursorTexture, Vector2.zero, cursorMode);
    27.     }
    28.  
    29.     public void OnMouseExit()
    30.     {
    31.         print("cursor removed!");
    32.         //Cursor.SetCursor(null, Vector2.zero, cursorMode);
    33.     }
    34.  
    35.     public Texture2D LoadPNG(string filePath) {
    36.  
    37.         Texture2D tex = null;
    38.         byte[] fileData;
    39.  
    40.         if (File.Exists(filePath))
    41.         {
    42.             print("file exists");
    43.             fileData = File.ReadAllBytes(filePath);
    44.             tex = new Texture2D(2, 2);
    45.             if (tex.LoadImage(fileData))//..this will auto-resize the texture dimensions.
    46.             {
    47.                 print("texture loaded succesfully");
    48.             }
    49.             else
    50.             {
    51.                 print("texture failed to load");
    52.             }
    53.         } else
    54.         {
    55.             print("file doesn't exist");
    56.         }
    57.         return tex;
    58.     }
    59. }
    60.  
    I'm using Event Trigger component and i'm using pointer enter and pointer exit functions to map to the above functions respectively.

    The point changes correct on enter, but it doesn't remove at all.

    Also, i receive the following error message inside my on mouse enter function.