• Unity之手游UI的点击和方向移动


    一 Button的点击

    1.1 新建UI -> Button

    1.2  在Button上面右击添加空物体

    1.3  创建脚本挂载到空物体上面

     脚本内容添加点击方法,来控制物体的显示隐藏

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6. public class NewMonoBehaviour : MonoBehaviour
    7. {
    8. public GameObject player;//获取物体
    9. private bool isActivity = true;
    10. private void Awake()
    11. {
    12. player = GameObject.Find("Player");
    13. }
    14. // Start is called before the first frame update
    15. void Start()
    16. {
    17. }
    18. void Update()
    19. {
    20. }
    21. // 按钮点击事件
    22. public void OnMyClick()
    23. {
    24. isActivity = !isActivity;
    25. //显示或者隐藏
    26. player.SetActive(isActivity);
    27. }
    28. }

    1.4 按钮上On Click的位置关联空物体,并选择空物体的脚本方法OnMyClick()

    1.5 运行后就可能控制物体显示隐藏了

    二 方向键控制移

    2.1 添加四个方向按钮

     2.2 添加一个脚本,同时挂载到四个按钮上面

    2.3 编写脚本,通过按钮名字判断是点击的哪个按钮,从而判断往哪个方向移动

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6. public class Controll : MonoBehaviour,IPointerDownHandler, IPointerUpHandler
    7. {
    8. public Rigidbody2D rbody;//获取刚体
    9. private void Awake()
    10. {
    11. rbody = GameObject.Find("Player").GetComponent();
    12. }
    13. void Start()
    14. {
    15. }
    16. // Update is called once per frame
    17. void Update()
    18. {
    19. if (isMove) {
    20. move();
    21. }
    22. }
    23. public bool isMove = false;//是否移动
    24. public void OnPointerDown(PointerEventData eventData)
    25. {
    26. isMove = true;
    27. getButton(eventData);
    28. }
    29. public void OnPointerUp(PointerEventData eventData)
    30. {
    31. isMove = false;
    32. }
    33. //获取点击的哪个按钮,方向
    34. private void getButton(PointerEventData eventData) {
    35. GameObject gameObject = eventData.selectedObject;
    36. Debug.Log(gameObject.name);
    37. switch (gameObject.name) {
    38. case "ButtonUp":
    39. moveX = 0;
    40. moveY = 1;
    41. break;
    42. case "ButtonLeft":
    43. moveX = -1;
    44. moveY = 0;
    45. break;
    46. case "ButtonBottom":
    47. moveX = 0;
    48. moveY = -1;
    49. break;
    50. case "ButtonRight":
    51. moveX = 1;
    52. moveY = 0;
    53. break;
    54. default:
    55. moveX = 0;
    56. moveY = 0;
    57. break;
    58. }
    59. }
    60. /**
    61. * 移动
    62. **/
    63. public float speed = 10f;//移动速度
    64. private int moveX;//方向 -1左 1右
    65. private int moveY;//方向 -1上 1下
    66. public void move() {
    67. Vector2 position = rbody.position;
    68. position.x += moveX * speed * Time.deltaTime;
    69. position.y += moveY * speed * Time.deltaTime;
    70. //transform.position = position;
    71. rbody.MovePosition(position);
    72. }
    73. }

    2.4 运行可以看到物体可以往上下左右方法移动

    2.5 总结:

    • 脚本实现抬起按下事件的接口MonoBehaviour,IPointerDownHandler, IPointerUpHandler。
    • 通过GameObject.Find("Player").GetComponent(),获取物体
    • 添加变量是否移动isMove,在Update方法里面判断是否拦截移动
    • OnPointerDown 按下的时候通过eventData.selectedObject,获取点击的哪个按钮,来判断上下左右方向
    • 添加方向判断判断,按钮按下的时候赋值,int moveX;//方向 -1左 1右     int moveY;//方向 -1上 1下
    • OnPointerUp 按键抬起的时候,ivMove变为false,不再更新移动位置
  • 相关阅读:
    Win10 update version 22H2
    redis学习-linux上redis的安装
    LeetCode 周赛上分之旅 #47 前后缀分解结合单调栈的贡献问题
    C++学习笔记(三十六)
    【云原生之kubernetes实战】在k8s环境下部署Mikochi文件管理工具(配置持久化存储)
    spring security教程(一)--认证
    yolov7 openvino c++推理记录
    电脑技巧:27个Office使用小技巧,值得收藏
    基于 element,阅读 ScrollBar 滚动组件源码
    算法宝典2——Java版本(此系列持续更新,这篇文章目前3道)(有题目的跳转链接)(此份宝典包含了二叉树的算法题)
  • 原文地址:https://blog.csdn.net/qq_29848853/article/details/132906159