• 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,不再更新移动位置
  • 相关阅读:
    福州市仓山区融丰锦秀山庄别墅设计
    c++ 中struct和class
    书本整理
    想找一个英文的二元分类数据集,类似sst2这种
    java项目之抗疫医疗用品销售平台ssm+jsp
    实验二: 密码恢复
    如何指定网站内搜索关键字(借用已有搜索引擎)
    暑期留校——区间DP-板子题石子合并
    分享:身份证读卡器java工程乱码解决办法
    时序分解 | Matlab实现EEMD集合经验模态分解时间序列信号分解
  • 原文地址:https://blog.csdn.net/qq_29848853/article/details/132906159