团结ui和collider2d

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了团结ui和collider2d相关的知识,希望对你有一定的参考价值。

这是一个很简单的函数的长代码,但我似乎不知道如何缩短它。任何建议?

这是我为初学者项目创建的一个简单脚本,我将脚本放在了玩家对象内,同时包含了rigidbody2d和boxcollider2d,是的,它可以正常工作,它切换了按钮游戏对象,这是我在某种意义上的目的但是我希望它只使用一个按钮。如果你能帮助我,我会很感激。

//different button objects
public GameObject smithbutton;
public GameObject innbutton;

private void OnTriggerEnter2D(Collider2D col)
{
//debugs which collider player is in
    if (col.gameObject.name == "Blacksmith")
    {
        Debug.Log("This is the Blacksmith");
    }
    if (col.gameObject.name == "Inn")
    {
        Debug.Log("This is the Inn");
    }
}

private void OnTriggerStay2D(Collider2D col)
{
//once playerobject stays, button will toggle till player leaves
    if (col.gameObject.name == "Blacksmith")
    {
        Debug.Log("still on the Blacksmith's door");
        smithbutton.SetActive(true);
    }
    if (col.gameObject.name == "Inn")
    {
        Debug.Log("still on the Inn's door");
        innbutton.SetActive(true);
    }
}

private void OnTriggerExit2D(Collider2D col)
{
//once playerobject exits, button will toggle and disappear
    if (col.gameObject.name == "Blacksmith")
    {
        smithbutton.SetActive(false);
    }
    if (col.gameObject.name == "Inn")
    {
        innbutton.SetActive(false);
    }
}
答案

你不需要OnTriggerStay,你可以在OnTriggerEnter中做到这一点并摆脱Stay函数。

public Button mybtn;
bool isBlacksmith; // keep track of where you are (you can later make it an enum if there are more than 2 places and check that)

void Start()
{
    //when the button is clicked buttonFunction will be called
    mybtn.onClick.AddListener(buttonFunction);
}
private void OnTriggerEnter2D(Collider2D col)
{
    if (col.gameObject.name == "Blacksmith")
    {
        Debug.Log("Entered Blacksmith's door");
        mybtn.gameObject.SetActive(true);
        isBlacksmith = true;
    }
    if (col.gameObject.name == "Inn")
    {
        Debug.Log("Entered Inn's door");
        mybtn.gameObject.SetActive(true);
        isBlacksmith = false;
    }
}

private void OnTriggerExit2D(Collider2D col)
{
    //once playerobject exits, button will toggle and disappear
    if (col.gameObject.name == "Blacksmith" || col.gameObject.name == "Inn")
    {
        smithbutton.SetActive(false);
    }
}

public void buttonFunction()
{
    if (isBlacksmith)
        Debug.Log("In Blacksmith");
    else
        Debug.Log("In Inn");

}
另一答案

你无能为力,因为功能非常简单。你可以做得更好,但仍然减少一些线。如果您创建更多的旅馆或更多的铁匠,使用标签而不是名称将是未来的证明。使用对撞机调用方法可以让您轻松扩展功能。我通常会自己将这些支票添加到Inn和Blacksmiths,他们会寻找玩家。

//different button objects
[SerializeField] private GameObject smithbutton;
[SerializeField] private GameObject innbutton;

private void OnTriggerEnter2D(Collider2D col)
{
//debugs which collider player is in
    if (col.gameObject.tag == "Blacksmith")
    {
        ButtonActivationToggle(smithbutton, col);
    }
    if (col.gameObject.tag == "Inn")
    {
        ButtonActivationToggle(innbutton, col);
    }
}

private void OnTriggerExit2D(Collider2D col)
{
//once playerobject exits, button will toggle and disappear
    if (col.gameObject.tag == "Blacksmith")
    {
        ButtonActivationToggle(smithbutton, col);
    }
    if (col.gameObject.tag == "Inn")
    {
        ButtonActivationToggle(innbutton, col);
    }
}

public void ButtonActivationToggle(GameObject button, Collider2D collider)
{
    bool tmp = false;
    tmp = button.activeInHierarchy ? false : true;
    button.SetActive(tmp);
    if (button.activeInHierarchy)
    {
        Debug.Log("This is the " + gameObject.collider.tag)
    }
}  

以上是关于团结ui和collider2d的主要内容,如果未能解决你的问题,请参考以下文章

unity, Collider2D.attachedRigidbody

从 fragmentActivity 刷新片段 UI

我们可以在活动 xml 中编写 UI 以及在片段 xm 中编写 UI 吗?

php 一个短代码片段准备在WooCommerce Thank You页面上输出货件跟踪UI。

Unity入门计划Collision2D类&Collider2D类

使用绑定从片段访问父活动的 UI 元素