Skip to content

Instantly share code, notes, and snippets.

@fallenblood7080
Last active August 8, 2023 10:08
Show Gist options
  • Save fallenblood7080/8e610de71cf5822ac390d00a0767a02b to your computer and use it in GitHub Desktop.
Save fallenblood7080/8e610de71cf5822ac390d00a0767a02b to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEngine;
public class CalculateCameraBox : MonoBehaviour
{
private Camera _camera;
private EdgeCollider2D _camEdgeCollider;
private float _sizeX, _sizeY, _ratio;
[SerializeField] private bool isPortrait;
[SerializeField][Range(1f,10f)] private float sizeRatioX = 2, sizeRatioY = 2;
public void Calculate()
{
_camera = GetComponent<Camera>();
if (!TryGetComponent(out _camEdgeCollider))
{
Debug.LogWarning("Edge Collider not Founded");
return;
}
_ratio = isPortrait ? (float)Screen.width / (float)Screen.height : (float)Screen.height / (float)Screen.width;
_sizeY = _camera.orthographicSize * 2;
_sizeX = _sizeY * _ratio;
List<Vector2> points = new(5)
{
new Vector2(-_sizeX /sizeRatioX, -_sizeY /sizeRatioY),
new Vector2(_sizeX / sizeRatioX, -_sizeY /sizeRatioY),
new Vector2(_sizeX / sizeRatioX, _sizeY /sizeRatioY),
new Vector2(-_sizeX /sizeRatioX, _sizeY/sizeRatioY),
new Vector2(-_sizeX /sizeRatioX, -_sizeY /sizeRatioY),
};
_camEdgeCollider.SetPoints(points);
}
}
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(CalculateCameraBox))]
public class CalculateCameraBoxEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
CalculateCameraBox calculateCameraBox = (CalculateCameraBox)target;
if (GUILayout.Button("Create"))
{
calculateCameraBox.Calculate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment