using System.Collections; using System.Collections.Generic; using UnityEngine; public class KeepLevelAnchored : MonoBehaviour { Camera _camera; Rect _levelBound; bool _boundCalculated = false; // Start is called before the first frame update void Start() { _camera = Camera.main; LevelManager.Instance.LevelLoaded += CalculateBound; if (!LevelManager.Instance.CurrentLevel) return; CalculateBound(LevelManager.Instance.CurrentLevel); } private void CalculateBound(GatherAndDefend.LevelEditor.Level level) { _boundCalculated = false; _levelBound = level.CalculateBounds(); _boundCalculated = true; } void OnDestroy() { LevelManager.Instance.LevelLoaded -= CalculateBound; } // Update is called once per frame void Update() { if (!_boundCalculated) return; //move right var xMax = _levelBound.xMax; var width = _camera.ScreenToWorldPoint(Vector2.one * _camera.pixelWidth).x; var rightDiff = width - xMax; var camPos = _camera.transform.position; camPos.x -= rightDiff; _camera.transform.position = camPos; //resize var xMin = _levelBound.xMin; var left = _camera.ScreenToWorldPoint(Vector2.zero).x; var center = _camera.ScreenToWorldPoint(Vector2.one * _camera.pixelWidth / 3).x; var leftDiff = xMax - xMin; var centerDiff = xMax - center; var ratio = centerDiff / leftDiff; _camera.orthographicSize /= ratio; return; } }