【问题标题】:Buttons in Unity, without using UI?Unity中的按钮,不使用UI?
【发布时间】:2016-06-19 10:24:12
【问题描述】:

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

PS。我看到了 NGUI,到目前为止我不喜欢它。还有什么?

【问题讨论】:

  • 2DToolkit 完美地做到了这一点,它内置了所有功能。 (2DTK 在许多方面仍然非常有用:没有它我们永远不会做任何 2D 项目。) ngui 不是解决方案。或者您可以创建自己的,正如程序员在下面完美解释的那样。不过我不得不怀疑——为什么不直接使用 Unity 的 UI?有什么问题?
  • 我也想知道同样的事情。你有一个完整的 Unity uGUI,但他正在谈论使用 NGUI,甚至从头开始重新制作所有控件......
  • 可能是我弄错了,但是在 2D 游戏中使用 Unity UI 按钮对我来说看起来很奇怪。我不能把它放在我的对象层次结构中,它的比例与我的精灵不同,我不能将它附加到特定的排序层等。所以我有点迷路了。
  • 你是对的。你不能把它放在你的对象层次结构中。 UI 必须在 Canvas GameObject 下。这很好,因为您的 UI 应该与您的游戏对象分开。同样的事情也适用于您的 UI 代码。它应该与您的游戏逻辑分开,以便于故障排除和可移植性。这根本不应该是一个问题。您是否想对 UI 做任何由于对象层次结构而无法做的事情?
  • @MaximLavrov 您可以使画布成为游戏对象的子对象。请参阅 unity 网站上的坦克教程中的 the Tank Health video

标签: c# unity3d unity5 unity3d-ui


【解决方案1】:

Unity 中有没有一种方法可以创建一个简单的 2D 按钮而不使用 界面层

您可以使用Sprite/Sprite Render 作为Button。首先创建一个游戏对象并将EventSystemStandaloneInputModule 附加到它。将Physics2DRaycaster附加到相机,实现IPointerClickHandler并覆盖OnPointerClick函数。通过转到 GameObject->2D Object->Sprite 创建一个 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/Mesh,那么您需要向它添加一个简单的碰撞器。如果它只是 Sprite,那么您必须为 sprite 添加一个 2D 碰撞器。

【讨论】:

  • 很好的答案,谢谢!您只是忘了提及,您还需要向精灵添加一个碰撞器。
  • Raycast 不会检测到没有碰撞器的游戏对象。我以为你已经知道了。
  • 对于像我这样的新手来说,一切都是新的。我什至不知道 Raycast 是什么 :) 对我来说,它现在只是一个魔法。我认为最好将其添加到其他新手的答案中。
  • 刚刚更新了问题。如果你是 Unity 新手,那么你应该关注这个 unity3d.com/learn/tutorials 它对新手有很多好处。
  • Crikey,这绝对令人难以置信。完美运行。感谢您的帮助。
【解决方案2】:

另一种更简单的方法是添加一个 BoxCollider2D 组件,然后将以下方法添加到新组件中,例如 UIButton,您将在其中执行按钮操作:

  1. 无效 OnMouseOver()
  2. 无效 OnMouseDown()
  3. 无效 OnMouseUp()

这避免了使用 EventSystem、StandaloneInputModule 和 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()
    {

    }
}

在统一 2018.1 中测试。我最初注意到这一点和上述方法的一个区别是,在此模型中未检测到鼠标右键单击,但在 EventSystemModel 中检测到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多