diff --git a/Assets.Scripts.Core.Scene/Layer.cs b/Assets.Scripts.Core.Scene/Layer.cs index c6a84b68..bf838606 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,28 @@ 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)) + { + 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}.\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) + { + 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)