Skip to content

Instantly share code, notes, and snippets.

@0x0ade
Created August 5, 2017 11:05
Show Gist options
  • Save 0x0ade/bf079df95b924673be3149f9701416e8 to your computer and use it in GitHub Desktop.
Save 0x0ade/bf079df95b924673be3149f9701416e8 to your computer and use it in GitHub Desktop.
YO! Noid 2: Enter The Void - Custom aspect ratio MonoMod
#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it
using UnityEngine;
using MonoMod;
using System;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public static class Common {
public readonly static Vector2 DefaultScreenSize = new Vector2(512f, 240f);
public readonly static float DefaultScreenAspect = 512f / 240f;
public static void OnSceneChange(Scene from, Scene to) {
foreach (Canvas canvas in GameObject.FindObjectsOfType<Canvas>()) {
CanvasScaler scaler = canvas.GetComponent<CanvasScaler>();
if (scaler == null)
scaler = canvas.gameObject.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = DefaultScreenSize;
}
float camAspect = DefaultScreenSize.x / DefaultScreenSize.y;
Vector2 camSize = DefaultScreenSize;
foreach (Camera cam in GameObject.FindObjectsOfType<Camera>()) {
cam.ResetAspect();
cam.rect = new Rect(0f, 0f, 1f, 1f);
if (cam.targetTexture != null) {
RenderTexture rt = cam.targetTexture;
Console.WriteLine($"REFERENCE SIZE: {rt.width} {rt.height}");
cam.targetTexture = null;
rt.width = cam.pixelWidth;
rt.height = cam.pixelHeight;
cam.targetTexture = rt;
camAspect = cam.aspect;
camSize = new Vector2(cam.pixelWidth, cam.pixelHeight);
}
Component[] components = cam.GetComponents<Component>();
foreach (Component c in components)
Console.WriteLine($"CAM: {cam.name}; COMPONENT: {c.GetType().FullName}");
postVHSPro vhs = cam.GetComponent<postVHSPro>();
if (vhs != null) {
// GameObject.Destroy(vhs);
}
}
foreach (MeshRenderer mr in GameObject.FindObjectsOfType<MeshRenderer>()) {
if (!(mr.material.mainTexture is RenderTexture))
continue;
Console.WriteLine($"QUAD FOUND: {mr.gameObject.name}");
mr.transform.localScale = new Vector3(
mr.transform.localScale.y * camAspect,
mr.transform.localScale.y,
mr.transform.localScale.z
);
}
}
}
#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it
using UnityEngine;
using MonoMod;
using System;
using UnityEngine.SceneManagement;
public class patch_TitleScreen : TitleScreen {
private extern void orig_Start();
private void Start() {
orig_Start();
SceneManager.activeSceneChanged += Common.OnSceneChange;
Common.OnSceneChange(default(Scene), default(Scene));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment