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

 

 

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM