I have an application I would like to change resolution to, and stretch across two monitors,
When I do so width (see script below):
- Code: Select all
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint uFlags);
The touch overlay get out sync with the new resolution,
I'm sure there is an easy fix to this, I can you disable / enable the canvas in your example scene Valentin , so guess I need to do something similar to the touchlayer(s).
I'm running unity 2017.2 , with the newest touchscript (asset store)
Try to import my quickly made script below, to have an idea of what I'm struggling with.
So basically updating the touch layers , to the new resolutions.
Couldn't attach the script , see below:
- Code: Select all
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Linq;
using UnityEngine.UI;
public class ScreenResPosUtil : MonoBehaviour
{
#region DLL Imports
[SerializeField]
private Canvas canvas;
[SerializeField]
private CanvasScaler canvasScaler;
[SerializeField]
private GameObject refTouchManager;
[SerializeField]
private Vector2 res1 = new Vector2(1920,1080);
[SerializeField]
private Vector2 res2 = new Vector2(2550,1440);
private const string UnityWindowClassName = "UnityWndClass";
const uint SWP_SHOWWINDOW = 0x0040;
const int GWL_STYLE = -16;
const int WS_BORDER = 1;
const int HWND_TOPMOST = -1;
const int HWND_NOTOPMOST = -2;
const int HWND_TOP = 0;
const int HWND_BOTTOM = 1;
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(System.String className, System.String windowName);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
private void SetBorderPosAndSize(IntPtr hwnd, int x, int y, int resX, int resY)
{
SetWindowLong(hwnd, GWL_STYLE, WS_BORDER);
SetWindowPos(hwnd, HWND_TOP, x, y, resX, resY, SWP_SHOWWINDOW);
}
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumThreadWindows(uint dwThreadId, EnumWindowsProc lpEnumFunc, IntPtr lParam);
#endregion
#region Private fields
private static IntPtr windowHandle = IntPtr.Zero;
private static IntPtr windowHandle2 = IntPtr.Zero;
#endregion
#region Monobehavior implementation
/// <summary>
/// Called when this component is initialized
/// </summary>
void Start()
{
uint threadId = GetCurrentThreadId();
EnumThreadWindows(threadId, (hWnd, lParam) =>
{
var classText = new StringBuilder(UnityWindowClassName.Length + 1);
GetClassName(hWnd, classText, classText.Capacity);
if (classText.ToString() == UnityWindowClassName)
{
windowHandle = hWnd;
return false;
}
return true;
}, IntPtr.Zero);
if (windowHandle != IntPtr.Zero)
{
SetNewResPos(0, 0, (int)res1.x, (int)res1.y);
}
}
void SetNewResPos(int posX, int posY, int resX, int resY)
{
#if UNITY_STANDALONE_WIN
if (windowHandle != IntPtr.Zero)
{
SetBorderPosAndSize(windowHandle, posX, posY, resX, resY);
StartCoroutine(ResetCanvas());
}
#endif
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
SetNewResPos(0, 0, (int)res1.x, (int)res1.y);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
SetNewResPos(2560, 0, (int)res2.x, (int)res2.y);
}
}
IEnumerator ResetCanvas()
{
canvas.enabled = false;
//refTouchManager.SetActive(canvas.enabled);
yield return new WaitForSeconds(1f);
canvas.enabled = !canvas.enabled;
//refTouchManager.SetActive(canvas.enabled);
}
#endregion
}
#endif