using System; using System.Collections.Generic; using UnityEngine.Scripting.APIUpdating; namespace UnityEngine.Tilemaps { /// /// Rule Override Tiles are Tiles which can override a subset of Rules for a given Rule Tile to provide specialised behaviour while keeping most of the Rules originally set in the Rule Tile. /// [MovedFrom(true, "UnityEngine")] [Serializable] [HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@latest/index.html?subfolder=/manual/RuleOverrideTile.html")] public class AdvancedRuleOverrideTile : RuleOverrideTile { /// /// Gets the overriding TilingRuleOutput of a given TilingRule. /// /// The original TilingRule that is overridden public RuleTile.TilingRuleOutput this[RuleTile.TilingRule originalRule] { get { foreach (var overrideRule in m_OverrideTilingRules) if (overrideRule.m_Id == originalRule.m_Id) return overrideRule; return null; } set { for (int i = m_OverrideTilingRules.Count - 1; i >= 0; i--) { if (m_OverrideTilingRules[i].m_Id == originalRule.m_Id) { m_OverrideTilingRules.RemoveAt(i); break; } } if (value != null) { var json = JsonUtility.ToJson(value); var overrideRule = JsonUtility.FromJson(json); m_OverrideTilingRules.Add(overrideRule); } } } /// /// The Default Sprite set when creating a new Rule override. /// public Sprite m_DefaultSprite; /// /// The Default GameObject set when creating a new Rule override. /// public GameObject m_DefaultGameObject; /// /// The Default Collider Type set when creating a new Rule override. /// public Tile.ColliderType m_DefaultColliderType = Tile.ColliderType.Sprite; /// /// A list of TilingRule Overrides /// public List m_OverrideTilingRules = new List(); /// /// Applies overrides to this /// /// A list of overrides to apply /// The input overrides list is not valid public void ApplyOverrides(IList> overrides) { if (overrides == null) throw new System.ArgumentNullException("overrides"); for (int i = 0; i < overrides.Count; i++) this[overrides[i].Key] = overrides[i].Value; } /// /// Gets overrides for this /// /// A list of overrides to fill /// Returns the number of valid overrides for Rules /// The input overrides list is not valid public void GetOverrides(List> overrides, ref int validCount) { if (overrides == null) throw new System.ArgumentNullException("overrides"); overrides.Clear(); if (m_Tile) { foreach (var originalRule in m_Tile.m_TilingRules) { RuleTile.TilingRuleOutput overrideRule = this[originalRule]; overrides.Add(new KeyValuePair(originalRule, overrideRule)); } } validCount = overrides.Count; foreach (var overrideRule in m_OverrideTilingRules) { if (!overrides.Exists(o => o.Key.m_Id == overrideRule.m_Id)) { var originalRule = new RuleTile.TilingRule() { m_Id = overrideRule.m_Id }; overrides.Add(new KeyValuePair(originalRule, overrideRule)); } } } /// /// Updates the Rules with the Overrides set for this AdvancedRuleOverrideTile /// public override void Override() { if (!m_Tile || !m_InstanceTile) return; PrepareOverride(); var tile = m_InstanceTile; tile.m_DefaultSprite = m_DefaultSprite; tile.m_DefaultGameObject = m_DefaultGameObject; tile.m_DefaultColliderType = m_DefaultColliderType; foreach (var rule in tile.m_TilingRules) { var overrideRule = this[rule]; if (overrideRule != null) { JsonUtility.FromJsonOverwrite(JsonUtility.ToJson(overrideRule), rule); } } } } }