Search Unity

setCursor alternative

Discussion in 'Scripting' started by munkbusiness, Feb 4, 2019.

  1. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    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.

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0) && CarryoverInfo.Instance.hasPushPower && currentCursor != CurrentCursorEnum.green) {
    2.             Cursor.SetCursor(cursorGreen, hotSpot, cursorMode);
    3.             currentCursor = CurrentCursorEnum.green;
    4.             StartCoroutine(MouseClickChange());
    5. }
    (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.

    Code (CSharp):
    1. Vector3 mousePos = Input.mousePosition;
    2.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    3.         mousePos.z = -30;
    4.         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.
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    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.
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Can you turn on deep profiling? I can't replicate anything close to your numbers and I'm changing the cursor every frame.

    Code (csharp):
    1.  
    2.     public sealed class CursorController : MonoBehaviour
    3.    {
    4.        public Texture2D cursor1;
    5.        public Texture2D cursor2;
    6.      
    7.        private void Update()
    8.        {
    9.            Cursor.SetCursor((Time.frameCount % 2 == 0) ? cursor1 : cursor2, Vector3.zero, CursorMode.Auto);      
    10.        }
    11.    }
    12.  
    profiler.png

    Is your Texture2D properly imported with Cursor settings?
     
  4. munkbusiness

    munkbusiness

    Joined:
    Aug 22, 2017
    Posts:
    55
    @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: