【问题标题】:Unity Multiple Display - Click works only on MainCamera tagUnity Multiple Display - Click 仅适用于 MainCamera 标签
【发布时间】:2018-04-14 16:09:07
【问题描述】:

我在 Unity 2017.4.0f1 上遇到了多个显示器的问题。我需要创建 3 个摄像头并在 3 个不同的监视器上显示它们的视口,并且效果很好。但是,当我尝试单击连接到另一个相机的另一个显示器上的对象时,单击对象不起作用。似乎点击只适用于具有MainCamera 标签的相机。

谁能帮我理解和解决这个问题?非常感谢。


编辑:这是点击代码:

Ray raycam;
RaycastHit hit;

Vector2 displayleft = new Vector2(-72, 0);
Vector2 displaycenter = new Vector2(1366, 0);

raycam = cam2.ScreenPointToRay(Input.mousePosition);


if (Input.mousePosition.x < displaycenter.x && Input.mousePosition.x > 0)
{
    Debug.Log("1");

    if (Input.GetKey(KeyCode.A))
    {
        instruction.text = "1";
    }
    raycam = cam1.ScreenPointToRay(Input.mousePosition);

}
else if (Input.mousePosition.x < displayleft.x)
{
    if (Input.GetKey(KeyCode.A))
    {
        instruction.text = "2";
    }
    Debug.Log("2");

    raycam = cam2.ScreenPointToRay(Input.mousePosition);
}
else if (Input.mousePosition.x > displaycenter.x)
{
    if (Input.GetKey(KeyCode.A))
    {
        instruction.text = "3";
    }
    Debug.Log("3");

    raycam = cam3.ScreenPointToRay(Input.mousePosition);
}

if (Input.GetMouseButtonDown(0))
{

    if (Physics.Raycast(raycam, out hit))

    {

        hit.transform.root.GetComponent<Animator>().speed = 0f;
        GameObject ChildGameObject1 = hit.transform.GetChild(0).gameObject;
        GameObject ChildGameObject2 = ChildGameObject1.transform.GetChild(0).gameObject;
        ChildGameObject2.GetComponent<Animator>().SetBool("prova", true);

        StartCoroutine(Activation(hit));

    }

}

This is my configuration

【问题讨论】:

  • 点击游戏对象不起作用是什么意思?你能分享一些你使用的代码吗?
  • 当然,这里是代码: void FixedUpdate() { if (Input.GetMouseButtonDown(0)) { Ray ray; RaycastHit 命中;射线 = Camera.main.ScreenPointToRay(Input.mousePosition); if(Physics.Raycast(ray, out hit)) { hit.transform.root.GetComponent().speed = 0f;游戏对象 ChildGameObject1 = hit.transform.GetChild (0).gameObject;游戏对象 ChildGameObject2 = ChildGameObject1.transform.GetChild (0).gameObject; ChildGameObject2.GetComponent().SetBool("prova", true); StartCoroutine(Activation(hit));}}}
  • 我的意思是点击只能在MainCamera displaytarget上正常工作,但是当我切换到另一个显示器时,点击不起作用
  • 请将代码编辑到您的问题中。
  • 您的问题很明显:您只是从主摄像机投射光线,由您的Camera.main 声明给出。

标签: c# unity3d camera click game-engine


【解决方案1】:

所以我调查了这个问题,你显然需要做的是检查鼠标当前在哪个显示器上,并根据它从不同的相机投射光线。

幸运的是,我找到了允许这样做的this 代码,这使我们能够获得光标当前所在的显示。

请注意,此代码未经测试,可能无法正常工作。

首先我们获取光标当前所在显示器的索引。

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePosition lpMousePosition);

[StructLayout(LayoutKind.Sequential)]
public struct MousePosition 
{
    public int x;
    public int y;
}

private static int GetHoveredDisplay ()
{
    // Get the absolute mouse coordinates
    MousePosition mp;
    GetCursorPos(out mp);
    // Get the relative mouse coordinates
    Vector3 r = Display.RelativeMouseAt(new Vector3(mp.x, mp.y));
    // Use the z coordinate
    int displayIndex = (int)r.z;
    return displayIndex;
}

现在,我们可以将此索引插入到您的代码中。

我们需要创建一个新数组,其中填充了您的显示摄像头,按显示索引排序。

public Camera[] cameras;

接下来,我们从光标当前所在显示器的摄像头投射光线。

if (Input.GetMouseButtonDown(0))
{
    int di = GetHoveredDisplay();
    Camera currentDisplayCamera = cameras[di];

    Ray ray = currentDisplayCamera.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit))
    {
        // etc ...
    }

}

【讨论】:

  • 感谢您的代码。部分有效,但我总是遇到同样的问题:在这种情况下,光线投射仅适用于我添加到相机阵列中的第一台相机。正确检测点击了哪个显示器,但光线投射不能正常工作:例如,如果我点击显示器左侧,右侧的触发器就会激活。我认为这是分辨率问题,因为所有 3 台显示器都有不同的分辨率。会不会是这个问题?
  • @CristianGagl 当然,我猜这可能是个问题。不过,我没有资源来验证这一点,因为我只有一台显示器。
  • 看来不是分辨率问题。我编辑了我的问题并添加了一张显示我的配置的图片。所有代码仅适用于一个显示器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2023-03-12
  • 1970-01-01
  • 2016-08-22
相关资源
最近更新 更多