setCursor alternative

Hey

I am in the optimizations phase of my game progress. I noticed that whenever I click my cursor, Cursor.setCursor uses around 1/3 of the total CPU time on that single command. I use this command whenever I click to give player feedback by changing the cursor icon for 0.2 seconds.

if(Input.GetMouseButtonDown(0) && CarryoverInfo.Instance.hasPushPower && currentCursor != CurrentCursorEnum.green) {
            Cursor.SetCursor(cursorGreen, hotSpot, cursorMode);
            currentCursor = CurrentCursorEnum.green;
            StartCoroutine(MouseClickChange());
}

(MouseclickChange is a coroutine that sets the cursor back to default afterwards).

Now 4-8ms here and there isn’t an issue, but in my game a player can often click up to 8 times a second, so this introduces a heavy performance issue.

I tried making a object that follows the cursor position instead.

Vector3 mousePos = Input.mousePosition;
        mousePos = Camera.main.ScreenToWorldPoint(mousePos);
        mousePos.z = -30;
        transform.position = mousePos;

But compared to the previous cursor this selfmade-cursor lags slightly behind and is just less responsive, which is a no go as cursor control is the most important aspect of the game.

Is there another solution for getting click “animations” on the cursor without heavy performance dips?

Image of profiler in build:


When deep profiling in the editor I can see that it is Cursor.SetCursor_Injected( that is the culprit.

I am on 2018.3.2f1

Thanks a lot for your time.

This very likely will not be viable based on project type but you could try locking the cursor and setting mouse movement to rotate a head object then just setting a sprite to center of camera screen space.

Can you turn on deep profiling? I can’t replicate anything close to your numbers and I’m changing the cursor every frame.

    public sealed class CursorController : MonoBehaviour
   {
       public Texture2D cursor1;
       public Texture2D cursor2;
      
       private void Update()
       {
           Cursor.SetCursor((Time.frameCount % 2 == 0) ? cursor1 : cursor2, Vector3.zero, CursorMode.Auto);      
       }
   }

Is your Texture2D properly imported with Cursor settings?

@beanie4now Haha thanks, but no my cursor needs to move across the screen, so I cannot simply lock it center, on the contrary my character is locked center.

@GroZZleR
Changed to a strong PC, so numbers are generally lower, but cursor is still hitting hard relatively.

Here is a screenshot:

It is SetCursor_injected as mentioned in my OP that seems to be the problem.

Also pretty sure my cursor settings are correct, but maybe you can spot something: