简体   繁体   English

Unity 中作为 SceneSwitcher 的 3d 对象

[英]3d Object as SceneSwitcher in Unity

I'm programming a mobile game for a school project and I need help.我正在为学校项目编写手机游戏,我需要帮助。 I want to switch a scene when I'm clicking on a 3d object.我想在单击 3d 对象时切换场景。 I know how to do it with mouse input, but with touch input, I have no clue.我知道如何用鼠标输入来做,但是用触摸输入,我不知道。 I tried it a lot, but I didn't find a solution.我尝试了很多,但我没有找到解决方案。 Can anyone help me or send me the link to a tutorial?任何人都可以帮助我或将教程的链接发送给我吗?

Unity 中的一个场景

视觉工作室中的代码

You can use Input.GetTouch to get touch information on the touch screen and then use RaycastHit to determine the selected object可以使用Input.GetTouch获取触摸屏上的触摸信息,然后使用RaycastHit确定选中的对象

1. Get touch informaton 1.获取联系信息

Touch touch = Input.GetTouch(0);
if (touch.tapCount == 1)
{
    if (GetObjectTouchByTag(touch.position) != null)
        SceneManager.LoadScene("SampleScene"); // switch scene with name SampleScene
    // ...
}

2. Get 3D GameObject with RaycastHit 2. 使用 RaycastHit 获取 3D GameObject

// Raycast object by with tag
public GameObject GetObjectTouchByTag(Vector3 position)
{
    Ray ray = Camera.main.ScreenPointToRay(position);
    RaycastHit[] raycastHits;
    raycastHits = Physics.RaycastAll(ray);
    foreach (RaycastHit hit in raycastHits)
    {
       if (hit.collider.gameObject.tag == "SampleObject")
       {
          return hit.collider.gameObject;
       }
    }
    return null;
}

Hope it can help you!希望它可以帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM