Skip to content

Instantly share code, notes, and snippets.

@empika
Created May 22, 2017 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save empika/61bf4939635783c7e133c587465bd429 to your computer and use it in GitHub Desktop.
Save empika/61bf4939635783c7e133c587465bd429 to your computer and use it in GitHub Desktop.
Render stuff with pixels!
using System.Collections.Generic;
using Code.Events.EventTypes.Battle;
using Code.Events.EventTypes.World;
using Code.Utils;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace Code.Game.CameraUtils
{
public class PixelRenderer : SingletonMonoBehaviour<PixelRenderer>
{
public Camera Camera;
public Camera UICamera;
public Canvas Canvas;
public RawImage GameImage;
private int cachedScreenWidth = 0;
private int cachedScreenHeight = 0;
public RenderTexture renderTexture;
public Material RenderTextureMaterial;
public void Awake()
{
DontDestroyOnLoad(this.gameObject);
AddListener<WorldReadyEvent>(OnWorldReady);
AddListener<BattleReadyEvent>(OnBattleReady);
GrabCamera();
}
public void Update()
{
if (cachedScreenWidth != Screen.width || cachedScreenHeight != Screen.height)
{
UpdateEverything();
}
}
public void OnWorldReady(WorldReadyEvent worldReadyEvent)
{
GrabCamera();
UICamera.gameObject.SetActive(true);
Canvas.gameObject.SetActive(true);
UpdateEverything();
}
public void OnBattleReady(BattleReadyEvent worldReadyEvent)
{
UICamera.gameObject.SetActive(false);
Canvas.gameObject.SetActive(false);
}
public void UpdateEverything()
{
if (GameController.Instance == null)
{
return;
}
GrabCamera();
GameImage.gameObject.SetActive(false);
int scale = GameController.Instance.GetGameScale();
int renderedWidth = (int)Mathf.Ceil((float)Screen.width / (float)scale);
int renderedHeight = (int)Mathf.Ceil((float)Screen.height / (float)scale);
renderTexture = new RenderTexture(
renderedWidth,
renderedHeight,
24,
RenderTextureFormat.ARGB32
);
renderTexture.filterMode = FilterMode.Point;
renderTexture.wrapMode = TextureWrapMode.Clamp;
renderTexture.useMipMap = false;
renderTexture.autoGenerateMips = false;
renderTexture.hideFlags = HideFlags.DontSave;
RenderTextureMaterial.mainTexture = renderTexture;
Camera.orthographicSize = renderedHeight / 2f;
Camera.targetTexture = renderTexture;
GameImage.material = RenderTextureMaterial;
RectTransform gameImageRectTransform = GameImage.GetComponent<RectTransform>();
gameImageRectTransform.sizeDelta = new Vector2(
renderedWidth,
renderedHeight
);
gameImageRectTransform.localScale = new Vector3(
scale,
scale,
1
);
cachedScreenWidth = Screen.width;
cachedScreenHeight = Screen.height;
GameImage.gameObject.SetActive(true);
}
private void GrabCamera()
{
if (Camera == null)
{
GameObject mainCamera = GameObject.Find("Main Camera");
if (mainCamera != null)
{
Camera = mainCamera.GetComponent<Camera>();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment