鼠标美化

功能

修改鼠标的外观,用动画区分按下与否

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 修改鼠标的图案(隐藏鼠标,设置UI中一张图片的位置为鼠标的实时位置)
/// </summary>
[RequireComponent(typeof(Animator))]
public class MyCursor : MonoBehaviour
{
    private Animator anim;
    private string animPressParam = "IsPressing";
    // Start is called before the first frame update
    void Start()
    {
        Init();
    }

    // Update is called once per frame
    void Update()
    {
        HideCursor();
        SetPos();
        OnMouseLeftKeyUsed();
    }

    void Init()
    {
        HideCursor();
        anim = GetComponent<Animator>();
    }

    void SetPos()
    {
        transform.position = Input.mousePosition;
    }

    void OnMouseLeftKeyUsed()
    {
        OnCursorDown(0);
        OnCursorUp(0);
    }

    void HideCursor()
    {
        if (Cursor.visible)
            Cursor.visible = false;
    }

    void OnCursorDown(int keyId)
    {
        if(Input.GetMouseButtonDown(keyId))
        {
            // Debug.Log("鼠标按下");
            if (!anim.GetBool(animPressParam))
                anim.SetBool(animPressParam, true);
        }
    }

    void OnCursorUp(int keyId)
    {
        if (Input.GetMouseButtonUp(keyId))
        {
            // Debug.Log("鼠标抬起");
            if (anim.GetBool(animPressParam))
                anim.SetBool(animPressParam, false);

        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。