当前位置:   article > 正文

【Unity3DRPG入门学习笔记第六卷】SetCursor 设置鼠标指针_unity setcursor

unity setcursor

在上一卷使用了 UnityEvent,还用了序列化,在编辑器界面拖拽绑定事件对象,不太好维护,本卷换一种方式实现,先看 MouseManager.cs 中的更改

using System;
  • 1
public event Action<Vector3> m_OnMouseClicked;  // 鼠标点击事件
  • 1

MouseManager.cs

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

public class MouseManager : MonoBehaviour
{
    [Header("鼠标控制移动参数")]
    private RaycastHit m_HitInfo;                   // 射线击中信息
    private static MouseManager m_Instance;         // 单例对象

    public event Action<Vector3> m_OnMouseClicked;  // 鼠标点击事件
    public Texture2D m_Point, m_Doorway, m_Attack, m_Target, m_Arrow;   // 鼠标指针纹理

    // --- private -------------------------------------
    private MouseManager() { }

    private void Awake()
    {
        m_Instance = this;  // 初始化单例
    }

    private void Update()
    {
        SetCursorTexture(); // 切换鼠标贴图
        MouseControl();     // 鼠标控制
    }

    // --- public --------------------------------------
    public static MouseManager GetInstance()
    {
        if (m_Instance == null)
            m_Instance = new MouseManager();
        return m_Instance;
    }


    // --- private -------------------------------------
    /// <summary>
    /// @brief 切换鼠标贴图
    /// </summary>
    private void SetCursorTexture()
    {
        // 获取相机到鼠标点击点的射线
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out m_HitInfo))
        {
            // 更换鼠标纹理
            switch (m_HitInfo.collider.gameObject.tag)
            {
                case "Ground":
                    Cursor.SetCursor(m_Target, new Vector2(16, 16), CursorMode.Auto);
                    break;
            }
        }
    }

    /// <summary>
    /// @brief 鼠标控制
    /// </summary>
    private void MouseControl()
    {
        // 如果在边界内点击了左键
        if (Input.GetMouseButtonDown(0) && m_HitInfo.collider != null)
        {
            if (m_HitInfo.collider.gameObject.CompareTag("Ground")) // 如果是地面
                m_OnMouseClicked?.Invoke(m_HitInfo.point);          // 如果对象不为空,则调用所有已注册的回调
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

PlayerController.cs

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

public class PlayerController : MonoBehaviour
{
    private NavMeshAgent m_Agent;   // 导航网格动力源

    private void Awake()
    {
        m_Agent = GetComponent<NavMeshAgent>();
    }

    private void Start()
    {
        // 添加事件调用的方法
        MouseManager.GetInstance().m_OnMouseClicked += MoveToTarget;
    }

    private void Update()
    {
        
    }

    /// <summary>
    /// @brief 移动到目标点
    /// </summary>
    /// <param name="target"></param>
    public void MoveToTarget(Vector3 target)
    {
        m_Agent.destination = target;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

本卷演示



The End.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/93225?site
推荐阅读
相关标签
  

闽ICP备14008679号