Unity 实现双屏显示或多个屏幕显示

Unity创建一个新项目

 MainCamera 的 TargetDisplay设置成Display1      在创建一个camera 设置成Display2

设置成 一个摄像机看着一个模型

在创建一个空物体挂上脚本 脚本如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FenPing : MonoBehaviour
{
    //第一种
    private void Awake()
    {
        Screen.fullScreen = true;
        if (Display.displays.Length > 1)
        {
            Display.displays[1].Activate();
            Display.displays[1].SetRenderingResolution(Display.displays[1].systemWidth, Display.displays[1].systemHeight);

        }

        if (Display.displays.Length > 2)
        {
            Display.displays[2].Activate();
            Display.displays[2].SetRenderingResolution(Display.displays[2].systemWidth, Display.displays[2].systemHeight);

        }          
    }
    //第二种
   //void Awake()
   // {
   //     for (int i = 0; i < Display.displays.Length; i++)
   //     {
   //         Display.displays[i].Activate();
   //         Screen.SetResolution(Display.displays[i].renderingWidth, Display.displays[i].renderingHeight, true);
   //     }
   // }

}

完成打包测试即可

前提电脑是多屏幕的,一个屏幕还是只显示一个  我的是两个屏幕(效果图如下)

 

 补充

Unity窗口化设置

 

 设置之后打包就是窗口化 顶部也能拖动窗口

 

 

 

 

项目要无边框设置,窗口拖动如下:

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using UnityEngine.UI;
public class Drag_i : MonoBehaviour
{
    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("user32.dll")]
    public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

    //边框参数
    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int GWL_STYLE_ = 16;
    const int WS_BORDER = 1;
    const int WS_POPUP = 0x800000;
    const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
    const int SW_SHOWMAXIMIZED = 3; //{最大化, 激活} 
    public void btn_onclick()
    { //最小化 
        ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
    }
    public void btn_onclickxx()
    { //最大化
        ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
    }
    IntPtr Handle;
    float xx;
    bool bx;
    private void Awake()
    {
#if UNITY_STANDALONE_WIN
        //框体大小设置
        float windowWidth = 1024;
        float windowHeight = 768;
        //计算框体显示位置
        float posX = (Screen.currentResolution.width - windowWidth) / 2;
        float posY = (Screen.currentResolution.height - windowHeight) / 2;
        Rect rect = new Rect(posX, posY, windowWidth, windowHeight);

        //去边框
        //SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);//将网上的WS_BORDER替换成WS_POPUP  测试之后,不换好像有点卡顿,又好像无影响
        //显示边框
        SetWindowLong(GetForegroundWindow(), GWL_STYLE_, WS_POPUP);
        Handle = GetForegroundWindow();   //FindWindow ((string)null, "popu_windows");
        SetWindowPos(GetForegroundWindow(), 0, (int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, SWP_SHOWWINDOW);
#endif
    }
    void Start()
    {
        bx = false;
        xx = 0f;

    }



    //update 是设置窗口可以拖动
    void Update()
    {
     
#if UNITY_STANDALONE_WIN
        if (Input.GetMouseButtonDown(0))
        {

            xx = 0f;
            bx = true;
        }
        if (bx && xx >= 0.3f)
        { //这样做为了区分界面上面其它需要滑动的操作
            ReleaseCapture();
            SendMessage(Handle, 0xA1, 0x02, 0);
            SendMessage(Handle, 0x0202, 0, 0);


        }
        if (bx)
            xx += Time.deltaTime;
        if (Input.GetMouseButtonUp(0))
        {

            xx = 0f;
            bx = false;

        }

#endif
    }
}

 

最后一种要实现框体自由缩放拖拽功能的后续有时间补充

 

具体实现可以看这篇博客

https://www.cnblogs.com/Roz-001/p/14892900.html

 

 

   

posted @ 2021-01-14 11:12  剑起苍穹  阅读(6191)  评论(1编辑  收藏  举报
/*鼠标点击特效*/