From f01c034949f8c2170f4cd49ea0b0a7e27fe7a71a Mon Sep 17 00:00:00 2001 From: drojf <1249449+drojf@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:47:50 +1100 Subject: [PATCH 1/2] Log when two layers use the same priority --- Assets.Scripts.Core.Scene/Layer.cs | 17 +++++++++ Assets.Scripts.Core.Scene/SceneController.cs | 38 ++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/Assets.Scripts.Core.Scene/Layer.cs b/Assets.Scripts.Core.Scene/Layer.cs index c6a84b68..a06ce54a 100644 --- a/Assets.Scripts.Core.Scene/Layer.cs +++ b/Assets.Scripts.Core.Scene/Layer.cs @@ -1,4 +1,5 @@ using Assets.Scripts.Core.AssetManagement; +using MOD.Scripts.Core; using MOD.Scripts.Core.Scene; using System.Collections; using System.IO; @@ -651,6 +652,22 @@ public void SetPriority(int newpriority) Vector3 localPosition2 = base.transform.localPosition; targetPosition = new Vector3(x, localPosition2.y, (float)Priority * -0.1f); base.transform.localPosition = targetPosition; + + try + { + if (GameSystem.Instance.SceneController.PriorityInUseByOtherLayer(this, out int layerUsingPriority, out int thisLayerNumber)) + { + // In-engine "Priority" is 1 higher than the priority you specify in the game script. + MODLogger.Log( + $"WARNING: Attempted to use [Layer {thisLayerNumber}] with priority {Priority - 1}, " + + $"but [Layer {layerUsingPriority}] is already using priority {Priority - 1}. " + + $"This layer may not be drawn correctly/graphical artifacts may occur.", true); + } + } + catch (System.Exception e) + { + MODLogger.Log($"Error while checking Layer Priority: {e}", true); + } } public void FadeInLayer(float time) diff --git a/Assets.Scripts.Core.Scene/SceneController.cs b/Assets.Scripts.Core.Scene/SceneController.cs index 0337451e..5b5f9a7a 100644 --- a/Assets.Scripts.Core.Scene/SceneController.cs +++ b/Assets.Scripts.Core.Scene/SceneController.cs @@ -119,6 +119,44 @@ public void RemoveLayerReference(Layer l) } } + public bool PriorityInUseByOtherLayer(Layer layerToCheck, out int layerUsingPriority, out int layerToCheckNumber) + { + layerToCheckNumber = -1; + layerUsingPriority = -1; + + for (int id = 0; id < layers.Length; id++) + { + if (layers[id] == null) + { + continue; + } + + if (LayerPool.IsInPool(layers[id].gameObject)) + { + continue; + } + + if (!layers[id].IsInUse) + { + continue; + } + + // Don't check layer against itself + if (layers[id].gameObject == layerToCheck.gameObject) + { + layerToCheckNumber = id; + continue; + } + + if (layers[id].Priority == layerToCheck.Priority) + { + layerUsingPriority = id; + } + } + + return layerUsingPriority != -1; + } + public int GetActiveLayerMask() { if (activeScene == 0) From 4672106a3a6df9903a0742c39dda4b5a83da8683 Mon Sep 17 00:00:00 2001 From: drojf <1249449+drojf@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:19:02 +1100 Subject: [PATCH 2/2] Add workaround if two layers drawn with same priority - Adjust Z-position so that layers don't overlap, even if drawn with same priority - The layer number is used as a secondary priority - higher layer numbers with the same priority will be drawn ontop --- Assets.Scripts.Core.Scene/Layer.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Assets.Scripts.Core.Scene/Layer.cs b/Assets.Scripts.Core.Scene/Layer.cs index a06ce54a..bf838606 100644 --- a/Assets.Scripts.Core.Scene/Layer.cs +++ b/Assets.Scripts.Core.Scene/Layer.cs @@ -657,11 +657,17 @@ public void SetPriority(int newpriority) { if (GameSystem.Instance.SceneController.PriorityInUseByOtherLayer(this, out int layerUsingPriority, out int thisLayerNumber)) { + float originalZ = base.transform.localPosition.z; + + // This should work for all possible layers (technically would work for layers between 0-999) + base.transform.localPosition -= new Vector3(0, 0, thisLayerNumber * .0001f); + // In-engine "Priority" is 1 higher than the priority you specify in the game script. MODLogger.Log( $"WARNING: Attempted to use [Layer {thisLayerNumber}] with priority {Priority - 1}, " + - $"but [Layer {layerUsingPriority}] is already using priority {Priority - 1}. " + - $"This layer may not be drawn correctly/graphical artifacts may occur.", true); + $"but [Layer {layerUsingPriority}] is already using priority {Priority - 1}.\n" + + $"Z-position has been adjusted from {originalZ} to {base.transform.localPosition.z} to try to fix it.\n" + + $"NOTE: This layer may not be drawn correctly/graphical artifacts may occur.", true); } } catch (System.Exception e)