将“E”按钮更改为 ui 按钮 unity 2d

问题描述 投票:0回答:1

所以我在这里为我的平台游戏制作了一个脚本。该脚本允许玩家推拉盒子,当光线播放器触摸盒子然后按下“E”键时,玩家将使用 joint2D 抓住盒子,玩家将在左右移动时播放动画。

using System.Collections;
using UnityEditor;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;

public class trigger : MonoBehaviour
{
    private PlayerMovement1 move;
    [SerializeField] public LayerMask jumpableGround;

    private PlayerPush grab;
    public float distance = 1f;
    public LayerMask boxmask;

    [SerializeField]private Animator anim;
    [SerializeField]private Rigidbody2D rb1, rb2;
    Vector2 direction = Vector2.right;
    public GameObject Blockbig2;
    private Collider2D coll;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        rb1 = GetComponent<Rigidbody2D>();
        move = GetComponent<PlayerMovement1>();
        grab = GetComponent<PlayerPush>();
    
       
    }
    public void Update ()
    {
        Physics2D.queriesStartInColliders = false;
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance, boxmask);
       

        if (hit.collider != null && hit.collider.gameObject.tag == "block" && Input.GetKeyDown(KeyCode.E))
        {

            move.enabled = false;
            grab.enabled = true;
            anim.SetBool("Push_idle", true);
            anim.SetBool("player_idle", false);
            Blockbig2.GetComponent<FixedJoint2D>().enabled = true;
            Blockbig2 = hit.collider.gameObject;
            Blockbig2.GetComponent<FixedJoint2D>().connectedBody = this.GetComponent<Rigidbody2D>();
            rb2.constraints = RigidbodyConstraints2D.None;
        } 
      

        else if(Input.GetKeyUp(KeyCode.E))
        {

            anim.SetBool("player_push", false);
            anim.SetBool("Push_idle", false);
            anim.SetBool("player_idle", true);
            Blockbig2.GetComponent<FixedJoint2D>().enabled = false;
            move.enabled = true;
            grab.enabled = false;
            rb2.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
        }
 
    }

void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(transform.position, (Vector2)transform.position + Vector2.right * transform.localScale.x * distance);
        

    }
  

}

那么如何将此脚本更改为 ui 按钮?

c# unityscript
1个回答
0
投票

有一个简单的方法可以解决这个问题:

我们首先需要创建两个方法来检查按钮(UI)是否被按下。

首先,在你的脚本中创建一个 bool 来决定它是否被按下:

public bool ButtonPressed = false;

添加两个方法,我们将在其中更改按钮的状态:

public void ButtonDown()
{
    ButtonPressed = true;
}
public void ButtonUp()
{
    ButtonPressed = false;
}

接下来,进入 Unity,并选择您创建的按钮 UI。 转到检查器并添加一个名为 Event Trigger 的新组件,然后单击 Add New Event Type

选择指针向下指针向上。 然后将脚本所在的游戏对象拖放到游戏对象输入字段中。并作为一个函数,选择事件对应的方法。 (PointerDown 与“YourScript”.ButtonDown() 和 PointerUp 与“YourScript”.ButtonUp()) 它看起来应该类似于:screenshot.png

接下来,转到您的脚本并将

Input.GetKeyDown(KeyCode.E)
更改为
ButtonPressed == true
Input.GetKeyUp(KeyCode.E)
ButtonPressed == false
.

© www.soinside.com 2019 - 2024. All rights reserved.