Skip to content

Instantly share code, notes, and snippets.

@naichilab
Created July 4, 2018 13:57
Show Gist options
  • Save naichilab/59fb973fc3491fde6a66560d06ec1013 to your computer and use it in GitHub Desktop.
Save naichilab/59fb973fc3491fde6a66560d06ec1013 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
namespace naichilab
{
public class CameraSizeInitializer : MonoBehaviour
{
public const float PixelToUnits = 1f;
// ゲーム内解像度
public const int BaseScreenWidth = 640;
public const int BaseScreenHeight = 1136;
[SerializeField]
private Camera SizeSynchronizeCamera;
void Awake ()
{
Camera cam = gameObject.GetComponent<Camera> ();
float baseAspect = (float)BaseScreenHeight / (float)BaseScreenWidth;
float nowAspect = (float)Screen.height / (float)Screen.width;
float changeAspect;
if (baseAspect > nowAspect) {
changeAspect = nowAspect / baseAspect;
cam.rect = new Rect ((1.0f - changeAspect) * 0.5f, 0.0f, changeAspect, 1.0f);
} else {
changeAspect = baseAspect / nowAspect;
cam.rect = new Rect (0.0f, (1.0f - changeAspect) * 0.5f, 1.0f, changeAspect);
}
cam.orthographicSize = (float)BaseScreenWidth / PixelToUnits * (float)BaseScreenHeight / (float)BaseScreenWidth / 2f;
if (SizeSynchronizeCamera != null) {
SizeSynchronizeCamera.orthographicSize = cam.orthographicSize;
}
// Debug.Log (cam.orthographicSize);
// BaseScreenHeight / PixelToUnits / 2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment