首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >unity ui和collider2d

unity ui和collider2d
EN

Stack Overflow用户
提问于 2018-12-24 03:54:07
回答 2查看 199关注 0票数 1

对于一个简单的函数来说,这是一个相当长的代码,但我似乎不知道如何才能缩短它。有什么建议吗?

这是我为一个初学者项目创建的一个简单脚本,我用rigidbody2d和boxcollider2d将脚本放在player对象中,是的,它可以切换按钮游戏对象,这在某种意义上是我想要的,但我希望它只使用一个按钮。如果你也能帮上忙,我将不胜感激。

代码语言:javascript
复制
//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);
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-12-24 11:24:24

你能做的不多,因为函数很简单。你可以让它变得更好,同时仍然减少一些代码行。如果你创建更多的客栈或铁匠,使用标签而不是名字将是未来的证明。通过使用碰撞器调用方法,您可以轻松地扩展功能。我通常会将这些检查添加到客栈和铁匠自己,他们将寻找球员。

代码语言:javascript
复制
//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)
    }
}  
票数 1
EN

Stack Overflow用户

发布于 2018-12-24 07:25:30

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

代码语言:javascript
复制
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");

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53906733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档