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

View File

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