fix: Ensure UpdateTransform is called at most once per frame

This commit is contained in:
2025-02-21 21:38:26 +08:00
parent 04ba735ea6
commit f783d45e23
2 changed files with 18 additions and 7 deletions

View File

@@ -26,7 +26,7 @@ namespace Cryville.EEW.Unity.Map {
_tiles.CacheDir = Application.temporaryCachePath; _tiles.CacheDir = Application.temporaryCachePath;
_camera.orthographicSize = 0.5f / MathF.Max(1, (float)_camera.pixelWidth / _camera.pixelHeight); _camera.orthographicSize = 0.5f / MathF.Max(1, (float)_camera.pixelWidth / _camera.pixelHeight);
_elementLayerZ = m_layerElement.transform.position.z; _elementLayerZ = m_layerElement.transform.position.z;
UpdateTransform(); _mapElementUpdated = true;
} }
void OnDestroy() { void OnDestroy() {
_tiles.Dispose(); _tiles.Dispose();
@@ -39,7 +39,9 @@ namespace Cryville.EEW.Unity.Map {
static readonly Rect _viewportRect = new(0, 0, 1, 1); static readonly Rect _viewportRect = new(0, 0, 1, 1);
Vector3? ppos; Vector3? ppos;
bool _mapElementUpdated;
void Update() { void Update() {
bool flag = false;
var cpos = Input.mousePosition; var cpos = Input.mousePosition;
bool isMouseInViewport = _viewportRect.Contains(_camera.ScreenToViewportPoint(Input.mousePosition)); bool isMouseInViewport = _viewportRect.Contains(_camera.ScreenToViewportPoint(Input.mousePosition));
if (Input.GetMouseButtonDown(0) && isMouseInViewport) { if (Input.GetMouseButtonDown(0) && isMouseInViewport) {
@@ -49,18 +51,25 @@ namespace Cryville.EEW.Unity.Map {
var delta = _camera.ScreenToWorldPoint(pos0) - _camera.ScreenToWorldPoint(cpos); var delta = _camera.ScreenToWorldPoint(pos0) - _camera.ScreenToWorldPoint(cpos);
transform.position += delta; transform.position += delta;
ppos = cpos; ppos = cpos;
UpdateTransform(); flag = true;
} }
if (Input.GetMouseButtonUp(0)) { if (Input.GetMouseButtonUp(0)) {
ppos = null; ppos = null;
} }
if (Input.mouseScrollDelta.y != 0 && isMouseInViewport) { if (Input.mouseScrollDelta.y != 0 && isMouseInViewport) {
Scale *= Mathf.Pow(2, -Input.mouseScrollDelta.y / 8); Scale *= Mathf.Pow(2, -Input.mouseScrollDelta.y / 8);
UpdateTransform(); flag = true;
}
if (_mapElementUpdated) {
_mapElementUpdated = false;
ZoomToMapElement();
flag = true;
}
if (flag) {
UpdateTransform(); // Ensure UpdateTransform is called at most once per frame for tiles to unload correctly
} }
} }
void ZoomToMapElement() {
public void OnMapElementUpdated() {
var aabb = m_layerElement.AABB; var aabb = m_layerElement.AABB;
if (aabb is not RectangleF b) return; if (aabb is not RectangleF b) return;
if (b.Width * _camera.pixelHeight < _camera.pixelWidth * b.Height) if (b.Width * _camera.pixelHeight < _camera.pixelWidth * b.Height)
@@ -70,7 +79,9 @@ namespace Cryville.EEW.Unity.Map {
Scale *= 0.6f; Scale *= 0.6f;
if (Scale < 0.01f) Scale = 0.01f; if (Scale < 0.01f) Scale = 0.01f;
transform.localPosition = new PointF(b.X + b.Width / 2, b.Y + b.Height / 2).ToVector2(); transform.localPosition = new PointF(b.X + b.Width / 2, b.Y + b.Height / 2).ToVector2();
UpdateTransform(); }
public void OnMapElementUpdated() {
_mapElementUpdated = true;
} }
void UpdateTransform() { void UpdateTransform() {

View File

@@ -4,7 +4,7 @@ MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []
executionOrder: 0 executionOrder: 5
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:
assetBundleName: assetBundleName: