Skip to content

Instantly share code, notes, and snippets.

@erodozer
Last active December 20, 2017 04:17
Show Gist options
  • Save erodozer/dc07e6db86d0b27cc1a98fe377134c8f to your computer and use it in GitHub Desktop.
Save erodozer/dc07e6db86d0b27cc1a98fe377134c8f to your computer and use it in GitHub Desktop.
Redirect Unity3D input into a letterboxed fixed area. Good for fixed resolution games with aspect ratio preserved scaling.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class RedirectInput : StandaloneInputModule {
// letterboxed fixed area
[SerializeField] Vector2 fixedResolution;
private Vector2 m_cursorPos;
public override void UpdateModule()
{
//scale input to our fixed resolution
Rect zone = GetInputZone();
float scale = GetScale();
Vector2 pos = new Vector2(
(Input.mousePosition.x - zone.xMin) / scale,
(Input.mousePosition.y - zone.yMin) / scale
);
//m_cursorPos = Input.mousePosition;
m_cursorPos = pos;
}
private float GetScale() {
float screenAspect = (float)Screen.width / (float)Screen.height;
float rectAspect = fixedResolution.x / fixedResolution.y;
float scaleFactor;
if (screenAspect > rectAspect)
scaleFactor = Screen.height / fixedResolution.y;
else
scaleFactor = Screen.width / fixedResolution.x;
return scaleFactor;
}
private Rect GetInputZone(){
var area = fixedResolution * GetScale();
return new Rect(
Screen.width / 2 - area.x / 2,
Screen.height / 2 - area.y / 2,
area.x, area.y
);
}
private readonly MouseState m_MouseState = new MouseState();
protected override MouseState GetMousePointerEventData(int id = 0)
{
// Populate the left button...
PointerEventData leftData;
var created = GetPointerData(kMouseLeftId, out leftData, true);
leftData.Reset();
if (created)
leftData.position = m_cursorPos;
Vector2 pos = m_cursorPos;
leftData.delta = pos - leftData.position;
leftData.position = pos;
leftData.scrollDelta = Input.mouseScrollDelta;
leftData.button = PointerEventData.InputButton.Left;
eventSystem.RaycastAll(leftData, m_RaycastResultCache);
var raycast = FindFirstRaycast(m_RaycastResultCache);
leftData.pointerCurrentRaycast = raycast;
m_RaycastResultCache.Clear();
// copy the apropriate data into right and middle slots
PointerEventData rightData;
GetPointerData(kMouseRightId, out rightData, true);
CopyFromTo(leftData, rightData);
rightData.button = PointerEventData.InputButton.Right;
PointerEventData middleData;
GetPointerData(kMouseMiddleId, out middleData, true);
CopyFromTo(leftData, middleData);
middleData.button = PointerEventData.InputButton.Middle;
m_MouseState.SetButtonState(PointerEventData.InputButton.Left, StateForMouseButton(0), leftData);
m_MouseState.SetButtonState(PointerEventData.InputButton.Right, StateForMouseButton(1), rightData);
m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, StateForMouseButton(2), middleData);
return m_MouseState;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment