统一多显示器 - 点击只适用于MainCamera标签

问题描述 投票:0回答:1

我有与团结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

c# unity3d camera click game-engine
1个回答
0
投票

所以,我看着这个问题,你显然需要做的是检查其上显示鼠标当前上取决于从不同的相机投下光线。

幸运的是,我发现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 ...
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.