Click and direction movement of Unity mobile game UI

Click of a Button

1.1 New UI -> Button

1.2 Right-click on the Button to add an empty object

1.3 Create a script and mount it on the empty object

 Add a click method to the script content to control the display and hiding of objects

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;

public class NewMonoBehaviour : MonoBehaviour
{
   public GameObject player;//获取物体
    private bool isActivity = true;
    private void Awake()
    {
        player = GameObject.Find("Player");
    }

    // Start is called before the first frame update
    void Start()
    {
    }

    void Update()
    {
    }

    // 按钮点击事件
    public void OnMyClick()
    {
        isActivity = !isActivity;
        //显示或者隐藏
        player.SetActive(isActivity);
    }
}

1.4 Associate the On Click position on the button with an empty object, and select the script method OnMyClick() of the empty object.

1.5 After running, it is possible to control the display and hiding of objects.

Two direction keys control movement

2.1 Add four direction buttons

 2.2 Add a script and mount it on four buttons at the same time

2.3 Write a script to determine which button was clicked based on the button name, so as to determine which direction to move in.

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;

public class Controll : MonoBehaviour,IPointerDownHandler, IPointerUpHandler
{
    public Rigidbody2D rbody;//获取刚体

    private void Awake()
    {
 
        rbody = GameObject.Find("Player").GetComponent<Rigidbody2D>();
    }

    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
        if (isMove) {
            move();
        }        
    }

    public bool isMove = false;//是否移动

    public void OnPointerDown(PointerEventData eventData)
    {
        isMove = true;
        getButton(eventData);
   
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        isMove = false;
    }

    //获取点击的哪个按钮,方向
    private void getButton(PointerEventData eventData) {
        GameObject gameObject = eventData.selectedObject;
        Debug.Log(gameObject.name);
        switch (gameObject.name) {
            case "ButtonUp":
                moveX = 0;
                moveY = 1;
                break;
            case "ButtonLeft":
                moveX = -1;
                moveY = 0;
                break;
            case "ButtonBottom":
                moveX = 0;
                moveY = -1;
                break;
            case "ButtonRight":
                moveX = 1;
                moveY = 0;
                break;
            default:
                moveX = 0;
                moveY = 0;
                break;
        }
    }

    /**
     * 移动
     **/
    public float speed = 10f;//移动速度
    private int moveX;//方向 -1左 1右
    private int moveY;//方向 -1上 1下
    public void move() {
       
        Vector2 position = rbody.position;
        position.x += moveX * speed * Time.deltaTime;
        position.y += moveY * speed * Time.deltaTime;
        //transform.position = position;
        rbody.MovePosition(position);
    }
}

2.4 When running, you can see that objects can move up, down, left, and right.

2.5 Summary:

  • The script implements the interface MonoBehaviour, IPointerDownHandler, and IPointerUpHandler for raising the press event.
  • Get the object through GameObject.Find("Player").GetComponent<Rigidbody2D>()
  • Add the variable whether to move isMove, and determine whether to intercept the move in the Update method.
  • When OnPointerDown is pressed, use eventData.selectedObject to get which button was clicked to determine the up, down, left, and right directions.
  • Add direction judgment, assign value when the button is pressed, int moveX; // Direction - 1 left 1 right int moveY; // Direction - 1 up 1 down
  • When the OnPointerUp button is lifted, ivMove becomes false and the movement position is no longer updated.

Guess you like

Origin blog.csdn.net/qq_29848853/article/details/132906159