功能
修改鼠标的外观,用动画区分按下与否
代码
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);
}
}
}