统一的按钮,无需使用UI



是否有一种统一的方法可以在不使用UI层的情况下创建一个简单的2D按钮。我有一个带有很多按钮的游戏,我不想在UI中制作整个应用程序。也欢迎更多控件:开关,滑块等。

ps。我看到了Ngui,到目前为止我不喜欢它。还有其他吗?

是否有一个统一的方法可以创建一个简单的2D按钮而不使用 UI层

您可以将Sprite/Sprite Render用作Button。首先创建GameObject并将EventSystemStandaloneInputModule附加到它。将Physics2DRaycaster连接到相机,实现IPointerClickHandler并覆盖OnPointerClick功能。通过转到 GameObject -> 2D对象 -> sprite ,然后将您的脚本连接到Sprite。这是一个完整的代码:

using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class SPRITEBUTTON: MonoBehaviour, IPointerClickHandler,
                                  IPointerDownHandler, IPointerEnterHandler,
                                  IPointerUpHandler, IPointerExitHandler
{
    void Start()
    {
        //Attach Physics2DRaycaster to the Camera
        Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
        addEventSystem();
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Mouse Clicked!");
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Mouse Down!");
    }
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Mouse Enter!");
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        Debug.Log("Mouse Up!");
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Mouse Exit!");
    }
    //Add Event System to the Camera
    void addEventSystem()
    {
        GameObject eventSystem = null;
        GameObject tempObj = GameObject.Find("EventSystem");
        if (tempObj == null)
        {
            eventSystem = new GameObject("EventSystem");
            eventSystem.AddComponent<EventSystem>();
            eventSystem.AddComponent<StandaloneInputModule>();
        }
        else
        {
            if ((tempObj.GetComponent<EventSystem>()) == null)
            {
                tempObj.AddComponent<EventSystem>();
            }
            if ((tempObj.GetComponent<StandaloneInputModule>()) == null)
            {
                tempObj.AddComponent<StandaloneInputModule>();
            }
        }
    }
}

编辑:

如果这是一个3D GameObject/网格,则需要在其中添加一个简单的碰撞器。如果只是精灵,则必须在精灵中添加一个2D对撞机。

另一种更简单的方法就是添加一个boxCollider2D组件,然后将以下方法添加到一个新组合中,例如uibutton,您将在其中执行按钮操作:<</p>

  1. void onMouseover()
  2. void onMousedown()
  3. void onmouseup()

这避免了使用事件系统,独立inputmodule和physics2draycaster。

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class UIButton : MonoBehaviour {
    public Sprite regular;
    public Sprite mouseOver;
    public Sprite mouseClicked;
    public TextMeshPro buttonText;
    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () {
    }
    private void OnMouseDown()
    {
    }
    private void OnMouseEnter()
    {
    }
    private void OnMouseExit()
    {
    }
    private void OnMouseUpAsButton()
    {
    }
}

在Unity 2018.1中测试。我最初注意到这一点,上述方法是在此模型中未检测到正确的鼠标按钮,而是在Eventsystemmodel中检测到的。

最新更新