Forum rules - please read before posting.

Switch Cursor Prefab in Unity Ui Cursor Rendering

Hi Chris, I hope you can help me. So I am looking to switch the Unity UI Cursor rendering depending on witch player character is currently active. I had this all set up working well when using Software Cursor rendering where I replaced the textures for each cursor using the below action template.

Are you able to show me how I would tweak the below to switch the Cursor Prefab from RealWorldCursorUI prefab to VillageCursorUi prefab?

See screenshots:

https://www.dropbox.com/sh/ehefvkn2jv7k8kz/AABi-xlTGxtQIWKh94mS_QMSa?dl=0

Thanks!

    • Adventure Creator
    • by Chris Burton, 2013-2021
    • "ActionTemplate.cs"
    • This is a blank action template.
    • */

    using UnityEngine;
    using System.Collections.Generic;

    if UNITY_EDITOR

    using UnityEditor;

    endif

    namespace AC
    {
    [System.Serializable]
    public class ChangeCursorsAction : Action
    {

        // Declare properties here
        public override ActionCategory Category { get { return ActionCategory.Custom; } }
        public override string Title { get { return "Switch Cursor Textures"; } }
        public override string Description { get { return "This switches the cursor textures"; } }
    
        // Declare variables here
        public Texture2D _newMainPointerTexture;
        public Texture2D _newWaitTexture;
        public Texture2D _newUseTexture;
        public Texture2D _newTalkTexture;
        public Texture2D _newLookTexture;
    public Texture2D _newPickUpTexture;
    public Texture2D _newExitRightTexture;
    public Texture2D _newExitUpTexture;
    public Texture2D _newExitLeftTexture;
    public Texture2D _newExitDownTexture;
    
    public override float Run()
    {
        /* 
         * This function is called when the action is performed.
         * 
         * The float to return is the time that the game
         * should wait before moving on to the next action.
         * Return 0f to make the action instantenous.
         * 
         * For actions that take longer than one frame,
         * you can return "defaultPauseTime" to make the game
         * re-run this function a short time later. You can
         * use the isRunning boolean to check if the action is
         * being run for the first time, eg: 
         */
        if (_newMainPointerTexture != null)
        {
            AC.KickStarter.cursorManager.pointerIcon.ReplaceTexture (_newMainPointerTexture);
        }
    
        if (_newWaitTexture != null)
        {
            AC.KickStarter.cursorManager.waitIcon.ReplaceTexture (_newWaitTexture);
        }
    
        if (_newUseTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(0).ReplaceTexture (_newUseTexture);
        }
    
        if (_newTalkTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(1).ReplaceTexture (_newTalkTexture);
        }
    
        if (_newLookTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(2).ReplaceTexture (_newLookTexture);
        }
    
        if (_newPickUpTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(3).ReplaceTexture (_newPickUpTexture);
        }
    
        if (_newExitRightTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(4).ReplaceTexture (_newExitRightTexture);
        }
    
        if (_newExitUpTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(5).ReplaceTexture (_newExitUpTexture);
        }
    
        if (_newExitLeftTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(6).ReplaceTexture (_newExitLeftTexture);
        }
    
        if (_newExitDownTexture != null)
        {
            AC.KickStarter.cursorManager.GetCursorIconFromID(7).ReplaceTexture (_newExitDownTexture);
        }
    
        if (!isRunning)
        {
            isRunning = true;
            return defaultPauseTime;
        }
        else
        {
            isRunning = false;
            return 0f;
        }
    }
    
    
    public override void Skip()
    {
        /*
         * This function is called when the Action is skipped, as a
         * result of the player invoking the "EndCutscene" input.
         * 
         * It should perform the instructions of the Action instantly -
         * regardless of whether or not the Action itself has been run
         * normally yet.  If this method is left blank, then skipping
         * the Action will have no effect.  If this method is removed,
         * or if the Run() method call is left below, then skipping the
         * Action will cause it to run itself as normal.
         */
    
        Run();
    }
    

if UNITY_EDITOR

    public override void ShowGUI()
    {
        // Action-specific Inspector GUI code here
        _newMainPointerTexture = (Texture2D)EditorGUILayout.ObjectField("Main Texture:", _newMainPointerTexture, typeof(Texture2D), true);
        _newWaitTexture = (Texture2D)EditorGUILayout.ObjectField("Wait Texture:", _newWaitTexture, typeof(Texture2D), true);
        _newUseTexture = (Texture2D)EditorGUILayout.ObjectField("Use Texture:", _newUseTexture, typeof(Texture2D), true);
        _newTalkTexture = (Texture2D)EditorGUILayout.ObjectField("Talk Texture:", _newTalkTexture, typeof(Texture2D), true);
        _newLookTexture = (Texture2D)EditorGUILayout.ObjectField("Look Texture:", _newLookTexture, typeof(Texture2D), true);
        _newPickUpTexture = (Texture2D)EditorGUILayout.ObjectField("PickUp Texture:", _newPickUpTexture, typeof(Texture2D), true);
        _newExitRightTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Right Texture:", _newExitRightTexture, typeof(Texture2D), true);
        _newExitUpTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Up Texture:", _newExitUpTexture, typeof(Texture2D), true);
        _newExitLeftTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Left Texture:", _newExitLeftTexture, typeof(Texture2D), true);
        _newExitDownTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Down Texture:", _newExitDownTexture, typeof(Texture2D), true);


    }


    public override string SetLabel()
    {
        // (Optional) Return a string used to describe the specific action's job.

        return string.Empty;
    }

endif

}

}

Comments

  • edited August 2022
    using UnityEngine;
    using System.Collections.Generic;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ChangeCursorsAction : Action
        {
    
            // Declare properties here
            public override ActionCategory Category { get { return ActionCategory.Custom; } }
            public override string Title { get { return "Switch Cursor Textures"; } }
            public override string Description { get { return "This switches the cursor textures"; } }
    
            // Declare variables here
            public Texture2D _newMainPointerTexture;
            public Texture2D _newWaitTexture;
            public Texture2D _newUseTexture;
            public Texture2D _newTalkTexture;
            public Texture2D _newLookTexture;
            public Texture2D _newPickUpTexture;
            public Texture2D _newExitRightTexture;
            public Texture2D _newExitUpTexture;
            public Texture2D _newExitLeftTexture;
            public Texture2D _newExitDownTexture;
            public GameObject newCursorPrefab;
    
            public override float Run()
            {
                if (_newMainPointerTexture != null)
                {
                    AC.KickStarter.cursorManager.pointerIcon.ReplaceTexture (_newMainPointerTexture);
                }
    
                if (_newWaitTexture != null)
                {
                    AC.KickStarter.cursorManager.waitIcon.ReplaceTexture (_newWaitTexture);
                }
    
                if (_newUseTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(0).ReplaceTexture (_newUseTexture);
                }
    
                if (_newTalkTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(1).ReplaceTexture (_newTalkTexture);
                }
    
                if (_newLookTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(2).ReplaceTexture (_newLookTexture);
                }
    
                if (_newPickUpTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(3).ReplaceTexture (_newPickUpTexture);
                }
    
                if (_newExitRightTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(4).ReplaceTexture (_newExitRightTexture);
                }
    
                if (_newExitUpTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(5).ReplaceTexture (_newExitUpTexture);
                }
    
                if (_newExitLeftTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(6).ReplaceTexture (_newExitLeftTexture);
                }
    
                if (_newExitDownTexture != null)
                {
                    AC.KickStarter.cursorManager.GetCursorIconFromID(7).ReplaceTexture (_newExitDownTexture);
                }
    
                if (newCursorPrefab)
                {
                    UnityUICursor oldUICursor = GameObject.FindObjectOfType<UnityUICursor> ();
                    if (oldUICursor)
                    {
                        Destroy (oldUICursor.gameObject);
                    }
                    Instantiate (newCursorPrefab);
                }
    
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI()
            {
                _newMainPointerTexture = (Texture2D)EditorGUILayout.ObjectField("Main Texture:", _newMainPointerTexture, typeof(Texture2D), true);
                _newWaitTexture = (Texture2D)EditorGUILayout.ObjectField("Wait Texture:", _newWaitTexture, typeof(Texture2D), true);
                _newUseTexture = (Texture2D)EditorGUILayout.ObjectField("Use Texture:", _newUseTexture, typeof(Texture2D), true);
                _newTalkTexture = (Texture2D)EditorGUILayout.ObjectField("Talk Texture:", _newTalkTexture, typeof(Texture2D), true);
                _newLookTexture = (Texture2D)EditorGUILayout.ObjectField("Look Texture:", _newLookTexture, typeof(Texture2D), true);
                _newPickUpTexture = (Texture2D)EditorGUILayout.ObjectField("PickUp Texture:", _newPickUpTexture, typeof(Texture2D), true);
                _newExitRightTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Right Texture:", _newExitRightTexture, typeof(Texture2D), true);
                _newExitUpTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Up Texture:", _newExitUpTexture, typeof(Texture2D), true);
                _newExitLeftTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Left Texture:", _newExitLeftTexture, typeof(Texture2D), true);
                _newExitDownTexture = (Texture2D)EditorGUILayout.ObjectField("Exit Down Texture:", _newExitDownTexture, typeof(Texture2D), true);
    
                newCursorPrefab = (GameObject) EditorGUILayout.ObjectField ("New cursor prefab:", newCursorPrefab, typeof (GameObject), false);
            }
    
            #endif
    
        }
    
    }
    
  • edited August 2022

    Hi Chris, first apologies for the mess of my original post! I thought I had selected all the code and applied the code tab to it! I have updated your new code but it does not reveal a section to it to now switch the cursor prefab? Ideally I would have two scripts, my original where if using software I can change the textures and a new action where if I am using Unity UI I am able to choose a new Unity Ui Cursor Rendering prefab. My custom action has so far not changed with the new code.

    UPDATE there is an error in console:

    Assets/Sleepytime Village/Scripts/CustomActions/ChangeCursorsAction.cs(112,31): error CS0266: Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

  • Try it now.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.