簡體   English   中英

為什么事件觸發器不能在 unity 2d 中工作?

[英]Why doesn't Event Trigger work in unity 2d?

我在 unity 2d 中創建了一個 UI 按鈕,它工作正常。 但我認為事件觸發器組件不起作用。 我想在光標位於按鈕上時使按鈕變大,並在光標退出時恢復正常大小。 這是我的腳本...

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

public class PlayAgainButton : MonoBehaviour
{
    public void PointerEnter()
    {
        Debug.Log("Enter");
        transform.localScale = new Vector2(1.2f, 1.2f);
    }

    public void PointerExit()
    {
        Debug.Log("Exit");
        transform.localScale = new Vector2(1f, 1f);
    }

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

    // Update is called once per frame
    void Update()
    {
        
    }
}

當我玩游戲時,控制台說光標已進入和退出

安慰

但是按鈕的比例永遠不會改變。

這里有什么問題?

您需要添加事件系統。 像這樣嘗試。


using UnityEngine;
using UnityEngine.EventSystems;

public class ButtonTest : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Enter");
        this.transform.localScale = new Vector2(1.2f, 1.2f);
    }



    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Exit");
        this.transform.localScale = new Vector2(1f, 1f);
       
    }
}       
    
 

使用鍵盤的 T 鍵將場景中的手柄模式設置為畫布模式。 鍵 w,e,r,t 分別用於操作位置、旋轉、縮放和畫布。 您將看到角落中的藍色圓圈,您可以使用它來設置大小。

檢查使其更大以觸發您的事件。 對於按鈕組件情況,效果區域由畫布大小決定。 情況也可能如此。

  1. 檢查您的 Button 是否在布局中 - 布局將設置大小並覆蓋您在代碼中所做的更改。
  2. Button 可能使用RectTransform (因為它是一個 UI 元素),因此您可以嘗試設置它的 localScale。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM