From 81eef939e7a6fe9f60bbaccc1b416f530f6b68b4 Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Fri, 1 May 2020 22:51:44 +0900 Subject: [PATCH 1/7] fix PrefabBrush's name with naming rule --- Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs | 2 +- .../PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs index 63eb482..d599ad1 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs @@ -5,7 +5,7 @@ namespace UnityEditor.Tilemaps /// /// This Brush instances and places a selected prefab onto the targeted location and parents the instanced object to the paint target. /// - [CreateAssetMenu(fileName = "Prefab brush", menuName = "2D Extras/Brushes/Prefab brush", order = 359)] + [CreateAssetMenu(fileName = "New Prefab Brush", menuName = "2D Extras/Brushes/Prefab Brush", order = 359)] [CustomGridBrush(false, true, false, "Prefab Brush")] public class PrefabBrush : BasePrefabBrush { diff --git a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs index fe56a53..e0022f6 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs @@ -5,7 +5,7 @@ namespace UnityEditor.Tilemaps /// /// This Brush instances and places a randomly selected Prefabs onto the targeted location and parents the instanced object to the paint target. Use this as an example to quickly place an assorted type of GameObjects onto structured locations. /// - [CreateAssetMenu(fileName = "Prefab Random brush", menuName = "2D Extras/Brushes/Prefab Random brush", order = 359)] + [CreateAssetMenu(fileName = "New Prefab Random Brush", menuName = "2D Extras/Brushes/Prefab Random Brush", order = 359)] [CustomGridBrush(false, true, false, "Prefab Random Brush")] public class PrefabRandomBrush : BasePrefabBrush { From 8a65944dcd117139bab1dd1cb7aace951f740de9 Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Sat, 2 May 2020 00:41:08 +0900 Subject: [PATCH 2/7] update Paint and Eease behaviour for Prefab Brushes --- .../Brushes/PrefabBrushes/BasePrefabBrush.cs | 42 +++------------ .../PrefabBrushes/PrefabBrush/PrefabBrush.cs | 38 ++++++------- .../PrefabRandomBrush/PrefabRandomBrush.cs | 54 +++++++++++++++++-- 3 files changed, 78 insertions(+), 56 deletions(-) diff --git a/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs b/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs index 5d201d2..3a263c9 100644 --- a/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System.Collections.Generic; +using UnityEngine; namespace UnityEditor.Tilemaps { @@ -11,51 +12,24 @@ public class BasePrefabBrush : GridBrush /// public Vector3 m_Anchor = new Vector3(0.5f, 0.5f, 0.5f); - /// - /// GameObject to instantiating - /// - protected GameObject Prefab = null; - - protected static Transform GetObjectInCell(GridLayout grid, Transform parent, Vector3Int position) + protected List GetObjectsInCell(GridLayout grid, Transform parent, Vector3Int position) { + var results = new List(); var childCount = parent.childCount; for (var i = 0; i < childCount; i++) { var child = parent.GetChild(i); if (position == grid.WorldToCell(child.position)) { - return child; + results.Add(child.gameObject); } } - return null; + return results; } - public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int position) + protected void InstantiatePrefabOnGrid(GridLayout grid, GameObject brushTarget, Vector3Int position, GameObject prefab) { - if (brushTarget.layer == 31) - { - return; - } - - var erased = GetObjectInCell(grid, brushTarget.transform, position); - if (erased != null) - { - Undo.DestroyObjectImmediate(erased.gameObject); - } - } - - public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int position) - { - // Do not allow editing palettes - if (brushTarget.layer == 31 || brushTarget == null) - return; - - var tileObject = GetObjectInCell(grid, brushTarget.transform, position); - if (tileObject) - { - return; - } - var instance = (GameObject)PrefabUtility.InstantiatePrefab(Prefab); + var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab); if (instance != null) { Undo.MoveGameObjectToScene(instance, brushTarget.scene, "Paint Prefabs"); diff --git a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs index d599ad1..332f55b 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs @@ -1,3 +1,4 @@ +using System.Linq; using UnityEngine; namespace UnityEditor.Tilemaps @@ -13,10 +14,7 @@ public class PrefabBrush : BasePrefabBrush /// The selection of Prefab to paint from /// public GameObject m_Prefab; - /// - /// Use to remove all prefabs in the cell or just the one that is currently selected in m_Prefab - /// - public bool m_ForceDelete; + /// /// Paints Prefabs into a given position within the selected layers. /// The PrefabBrush overrides this to provide Prefab painting functionality. @@ -24,16 +22,20 @@ public class PrefabBrush : BasePrefabBrush /// Grid used for layout. /// Target of the paint operation. By default the currently selected GameObject. /// The coordinates of the cell to paint data to. - public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int position) { - - - Prefab = m_Prefab; - var tileObject = GetObjectInCell(grid, brushTarget.transform, position); - if (tileObject == null || tileObject.name != m_Prefab.name) + // Do not allow editing palettes + if (brushTarget.layer == 31 || brushTarget == null) { - base.Paint(grid, brushTarget, position); + return; + } + + var objectsInCell = GetObjectsInCell(grid, brushTarget.transform, position); + var existPrefabObjectInCell = objectsInCell.Any(objectInCell => PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == m_Prefab); + + if (!existPrefabObjectInCell) + { + base.InstantiatePrefabOnGrid(grid, brushTarget, position, m_Prefab); } } @@ -47,14 +49,17 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p /// The coordinates of the cell to erase data from. public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int position) { - var erased = GetObjectInCell(grid, brushTarget.transform, position); - if (erased == null) + if (brushTarget.layer == 31 || brushTarget.transform == null) { return; } - if (m_ForceDelete || (!m_ForceDelete && erased.gameObject.name == m_Prefab.name)) + + foreach (var objectInCell in GetObjectsInCell(grid, brushTarget.transform, position)) { - base.Erase(grid, brushTarget, position); + if (PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == m_Prefab) + { + Undo.DestroyObjectImmediate(objectInCell); + } } } } @@ -66,13 +71,11 @@ public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int p public class PrefabBrushEditor : BasePrefabBrushEditor { private SerializedProperty m_Prefab; - private SerializedProperty m_ForceDelete; protected override void OnEnable() { base.OnEnable(); m_Prefab = m_SerializedObject.FindProperty("m_Prefab"); - m_ForceDelete = m_SerializedObject.FindProperty("m_ForceDelete"); } /// @@ -84,7 +87,6 @@ public override void OnPaintInspectorGUI() base.OnPaintInspectorGUI(); m_SerializedObject.UpdateIfRequiredOrScript(); EditorGUILayout.PropertyField(m_Prefab, true); - EditorGUILayout.PropertyField(m_ForceDelete, true); m_SerializedObject.ApplyModifiedPropertiesWithoutUndo(); } } diff --git a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs index e0022f6..f33d149 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs @@ -1,3 +1,4 @@ +using System.Linq; using UnityEngine; namespace UnityEditor.Tilemaps @@ -10,14 +11,17 @@ namespace UnityEditor.Tilemaps public class PrefabRandomBrush : BasePrefabBrush { private const float k_PerlinOffset = 100000f; + /// /// The selection of Prefabs to paint from /// public GameObject[] m_Prefabs; + /// /// Factor for distribution of choice of Prefabs to paint /// public float m_PerlinScale = 0.5f; + /// /// Paints Prefabs into a given position within the selected layers. /// The PrefabBrush overrides this to provide Prefab painting functionality. @@ -27,11 +31,53 @@ public class PrefabRandomBrush : BasePrefabBrush /// The coordinates of the cell to paint data to. public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int position) { - var index = Mathf.Clamp(Mathf.FloorToInt(GetPerlinValue(position, m_PerlinScale, k_PerlinOffset) * m_Prefabs.Length), 0, m_Prefabs.Length - 1); - Prefab = m_Prefabs[index]; - base.Paint(grid, brushTarget, position); + // Do not allow editing palettes + if (brushTarget.layer == 31 || brushTarget == null) + { + return; + } + + var objectsInCell = GetObjectsInCell(grid, brushTarget.transform, position); + var existPrefabObjectInCell = objectsInCell.Any(objectInCell => + { + return m_Prefabs.Any(prefab => PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == prefab); + }); + + if (!existPrefabObjectInCell) + { + var index = Mathf.Clamp(Mathf.FloorToInt(GetPerlinValue(position, m_PerlinScale, k_PerlinOffset) * m_Prefabs.Length), 0, m_Prefabs.Length - 1); + var prefab = m_Prefabs[index]; + base.InstantiatePrefabOnGrid(grid, brushTarget, position, prefab); + } } - + + /// + /// Erases all Prefabs in a given position within the selected layers if ForceDelete is true. + /// Erase only selected Prefabs in a given position within the selected layers if ForceDelete is false. + /// The PrefabBrush overrides this to provide Prefab erasing functionality. + /// + /// Grid used for layout. + /// Target of the erase operation. By default the currently selected GameObject. + /// The coordinates of the cell to erase data from. + public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int position) + { + if (brushTarget.layer == 31 || brushTarget.transform == null) + { + return; + } + + foreach (var objectInCell in GetObjectsInCell(grid, brushTarget.transform, position)) + { + foreach (var prefab in m_Prefabs) + { + if (PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == prefab) + { + Undo.DestroyObjectImmediate(objectInCell); + } + } + } + } + private static float GetPerlinValue(Vector3Int position, float scale, float offset) { return Mathf.PerlinNoise((position.x + offset)*scale, (position.y + offset)*scale); From e7bb24684e90d1d14217dc68f46e354251728f7b Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Sat, 2 May 2020 01:31:46 +0900 Subject: [PATCH 3/7] update doc comment --- Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs | 7 +++---- .../PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs | 9 ++++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs index 332f55b..687d8dc 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs @@ -4,7 +4,7 @@ namespace UnityEditor.Tilemaps { /// - /// This Brush instances and places a selected prefab onto the targeted location and parents the instanced object to the paint target. + /// This Brush instances and places a containing prefab onto the targeted location and parents the instanced object to the paint target. /// [CreateAssetMenu(fileName = "New Prefab Brush", menuName = "2D Extras/Brushes/Prefab Brush", order = 359)] [CustomGridBrush(false, true, false, "Prefab Brush")] @@ -16,7 +16,7 @@ public class PrefabBrush : BasePrefabBrush public GameObject m_Prefab; /// - /// Paints Prefabs into a given position within the selected layers. + /// Paints GameObject from containg Prefab into a given position within the selected layers. /// The PrefabBrush overrides this to provide Prefab painting functionality. /// /// Grid used for layout. @@ -40,8 +40,7 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p } /// - /// Erases all Prefabs in a given position within the selected layers if ForceDelete is true. - /// Erase only selected Prefabs in a given position within the selected layers if ForceDelete is false. + /// Erases GameObject that is created from containg Prefab in a given position within the selected layers. /// The PrefabBrush overrides this to provide Prefab erasing functionality. /// /// Grid used for layout. diff --git a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs index f33d149..eb5ffca 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs @@ -23,8 +23,8 @@ public class PrefabRandomBrush : BasePrefabBrush public float m_PerlinScale = 0.5f; /// - /// Paints Prefabs into a given position within the selected layers. - /// The PrefabBrush overrides this to provide Prefab painting functionality. + /// Paints GameObject from containg Prefabs with randomly into a given position within the selected layers. + /// The PrefabRandomBrush overrides this to provide Prefab painting functionality. /// /// Grid used for layout. /// Target of the paint operation. By default the currently selected GameObject. @@ -52,9 +52,8 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p } /// - /// Erases all Prefabs in a given position within the selected layers if ForceDelete is true. - /// Erase only selected Prefabs in a given position within the selected layers if ForceDelete is false. - /// The PrefabBrush overrides this to provide Prefab erasing functionality. + /// Erases GameObject that is created from containg Prefabs in a given position within the selected layers. + /// The PrefabRandomBrush overrides this to provide Prefab erasing functionality. /// /// Grid used for layout. /// Target of the erase operation. By default the currently selected GameObject. From 1feaf09a66f2f9093a1993d4b5b70b1ee14bea62 Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Sat, 2 May 2020 01:55:57 +0900 Subject: [PATCH 4/7] update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 293fd35..b984408 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ Please use the `2017` branch or the `2017` tag for earlier versions of Unity (fr - **Coordinate**: This Brush displays the cell coordinates it is targeting in the SceneView. Use this as an example to create brushes which have extra visualization features when painting onto a Tilemap. - **Line**: This Brush helps draw lines of Tiles onto a Tilemap. The first click of the mouse sets the starting point of the line and the second click sets the ending point of the line and draws the lines of Tiles. Use this as an example to modify brush painting behaviour to making painting quicker with less actions. - **Random**: This Brush helps to place random Tiles onto a Tilemap. Use this as an example to create brushes which store specific data per brush and to make brushes which randomize behaviour. -- **Prefab**: This Brush instances and places a randomly selected Prefabs onto the targeted location and parents the instanced object to the paint target. Use this as an example to quickly place an assorted type of GameObjects onto structured locations. +- **Prefab**: This Brush instances and places the containing Prefab onto the targeted location and parents the instanced object to the paint target. Use this as an example to quickly place an assorted type of GameObjects onto structured locations. +- **PrefabRandom**: This Brush instances and places a randomly selected Prefabs onto the targeted location and parents the instanced object to the paint target. Use this as an example to quickly place an assorted type of GameObjects onto structured locations. - **GameObject**: This Brush instances, places and manipulates GameObjects onto the scene. Use this as an example to create brushes which targets objects other than tiles for manipulation. - **TintBrush**: Brush to edit Tilemap per-cell tint colors. - **TintBrushSmooth**: Advanced tint brush for interpolated tint color per-cell. Requires the use of custom shader (see TintedTilemap.shader) and helper component TileTextureGenerator. From 0fe0b5355cde399b5463834831712d83631e5dbe Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Sat, 2 May 2020 15:08:55 +0900 Subject: [PATCH 5/7] changed method name --- Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs | 2 +- Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs | 2 +- .../PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs b/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs index 3a263c9..b17b649 100644 --- a/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/BasePrefabBrush.cs @@ -27,7 +27,7 @@ protected List GetObjectsInCell(GridLayout grid, Transform parent, V return results; } - protected void InstantiatePrefabOnGrid(GridLayout grid, GameObject brushTarget, Vector3Int position, GameObject prefab) + protected void InstantiatePrefabInCell(GridLayout grid, GameObject brushTarget, Vector3Int position, GameObject prefab) { var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab); if (instance != null) diff --git a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs index 687d8dc..c34feff 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs @@ -35,7 +35,7 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p if (!existPrefabObjectInCell) { - base.InstantiatePrefabOnGrid(grid, brushTarget, position, m_Prefab); + base.InstantiatePrefabInCell(grid, brushTarget, position, m_Prefab); } } diff --git a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs index eb5ffca..5ad0e24 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs @@ -47,7 +47,7 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p { var index = Mathf.Clamp(Mathf.FloorToInt(GetPerlinValue(position, m_PerlinScale, k_PerlinOffset) * m_Prefabs.Length), 0, m_Prefabs.Length - 1); var prefab = m_Prefabs[index]; - base.InstantiatePrefabOnGrid(grid, brushTarget, position, prefab); + base.InstantiatePrefabInCell(grid, brushTarget, position, prefab); } } From 847fac87b8130abad1bf4a8bca6a28908ef79a66 Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Sun, 17 May 2020 00:05:02 +0900 Subject: [PATCH 6/7] added m_EraseAnyObjects to erase not related any game objects see below conversation https://github.com/Unity-Technologies/2d-extras/pull/200#issuecomment-625678371 --- .../PrefabBrushes/PrefabBrush/PrefabBrush.cs | 59 ++++++++++------- .../PrefabRandomBrush/PrefabRandomBrush.cs | 66 +++++++++++-------- 2 files changed, 71 insertions(+), 54 deletions(-) diff --git a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs index c34feff..642bea5 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs @@ -13,7 +13,13 @@ public class PrefabBrush : BasePrefabBrush /// /// The selection of Prefab to paint from /// - public GameObject m_Prefab; + [SerializeField] GameObject m_Prefab; + + /// + /// If true, erases any GameObjects that are in a given position within the selected layers with Erasing. + /// Otherwise, erases only GameObjects that are created from owned Prefab in a given position within the selected layers with Erasing. + /// + bool m_EraseAnyObjects; /// /// Paints GameObject from containg Prefab into a given position within the selected layers. @@ -40,7 +46,8 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p } /// - /// Erases GameObject that is created from containg Prefab in a given position within the selected layers. + /// If "Erase Any Objects" is true, erases any GameObjects that are in a given position within the selected layers. + /// If "Erase Any Objects" is false, erases only GameObjects that are created from owned Prefab in a given position within the selected layers. /// The PrefabBrush overrides this to provide Prefab erasing functionality. /// /// Grid used for layout. @@ -55,38 +62,40 @@ public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int p foreach (var objectInCell in GetObjectsInCell(grid, brushTarget.transform, position)) { - if (PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == m_Prefab) + if (m_EraseAnyObjects || PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == m_Prefab) { Undo.DestroyObjectImmediate(objectInCell); } } } - } - - /// - /// The Brush Editor for a Prefab Brush. - /// - [CustomEditor(typeof(PrefabBrush))] - public class PrefabBrushEditor : BasePrefabBrushEditor - { - private SerializedProperty m_Prefab; - - protected override void OnEnable() - { - base.OnEnable(); - m_Prefab = m_SerializedObject.FindProperty("m_Prefab"); - } /// - /// Callback for painting the inspector GUI for the PrefabBrush in the Tile Palette. - /// The PrefabBrush Editor overrides this to have a custom inspector for this Brush. + /// The Brush Editor for a Prefab Brush. /// - public override void OnPaintInspectorGUI() + [CustomEditor(typeof(PrefabBrush))] + public class PrefabBrushEditor : BasePrefabBrushEditor { - base.OnPaintInspectorGUI(); - m_SerializedObject.UpdateIfRequiredOrScript(); - EditorGUILayout.PropertyField(m_Prefab, true); - m_SerializedObject.ApplyModifiedPropertiesWithoutUndo(); + private PrefabBrush prefabBrush => target as PrefabBrush; + private SerializedProperty m_Prefab; + + protected override void OnEnable() + { + base.OnEnable(); + m_Prefab = m_SerializedObject.FindProperty(nameof(m_Prefab)); + } + + /// + /// Callback for painting the inspector GUI for the PrefabBrush in the Tile Palette. + /// The PrefabBrush Editor overrides this to have a custom inspector for this Brush. + /// + public override void OnPaintInspectorGUI() + { + base.OnPaintInspectorGUI(); + m_SerializedObject.UpdateIfRequiredOrScript(); + EditorGUILayout.PropertyField(m_Prefab, true); + prefabBrush.m_EraseAnyObjects = EditorGUILayout.Toggle("Erase Any Objects", prefabBrush.m_EraseAnyObjects); + m_SerializedObject.ApplyModifiedPropertiesWithoutUndo(); + } } } } diff --git a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs index 5ad0e24..5364df4 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs @@ -15,12 +15,18 @@ public class PrefabRandomBrush : BasePrefabBrush /// /// The selection of Prefabs to paint from /// - public GameObject[] m_Prefabs; + [SerializeField] GameObject[] m_Prefabs; /// /// Factor for distribution of choice of Prefabs to paint /// - public float m_PerlinScale = 0.5f; + [SerializeField] float m_PerlinScale = 0.5f; + + /// + /// If true, erases any GameObjects that are in a given position within the selected layers with Erasing. + /// Otherwise, erases only GameObjects that are created from owned Prefab in a given position within the selected layers with Erasing. + /// + bool m_EraseAnyObjects; /// /// Paints GameObject from containg Prefabs with randomly into a given position within the selected layers. @@ -52,7 +58,8 @@ public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int p } /// - /// Erases GameObject that is created from containg Prefabs in a given position within the selected layers. + /// If "Erase Any Objects" is true, erases any GameObjects that are in a given position within the selected layers. + /// If "Erase Any Objects" is false, erases only GameObjects that are created from owned Prefab in a given position within the selected layers. /// The PrefabRandomBrush overrides this to provide Prefab erasing functionality. /// /// Grid used for layout. @@ -69,7 +76,7 @@ public override void Erase(GridLayout grid, GameObject brushTarget, Vector3Int p { foreach (var prefab in m_Prefabs) { - if (PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == prefab) + if (objectInCell != null && (m_EraseAnyObjects || PrefabUtility.GetCorrespondingObjectFromSource(objectInCell) == prefab)) { Undo.DestroyObjectImmediate(objectInCell); } @@ -81,35 +88,36 @@ private static float GetPerlinValue(Vector3Int position, float scale, float offs { return Mathf.PerlinNoise((position.x + offset)*scale, (position.y + offset)*scale); } - } - - /// - /// The Brush Editor for a Prefab Brush. - /// - [CustomEditor(typeof(PrefabRandomBrush))] - public class PrefabRandomBrushEditor : BasePrefabBrushEditor - { - private PrefabRandomBrush prefabBrush { get { return target as PrefabRandomBrush; } } - - private SerializedProperty m_Prefabs; - - protected override void OnEnable() - { - base.OnEnable(); - m_Prefabs = m_SerializedObject.FindProperty("m_Prefabs"); - } /// - /// Callback for painting the inspector GUI for the PrefabBrush in the Tile Palette. - /// The PrefabBrush Editor overrides this to have a custom inspector for this Brush. + /// The Brush Editor for a Prefab Brush. /// - public override void OnPaintInspectorGUI() + [CustomEditor(typeof(PrefabRandomBrush))] + public class PrefabRandomBrushEditor : BasePrefabBrushEditor { - base.OnPaintInspectorGUI(); - m_SerializedObject.UpdateIfRequiredOrScript(); - prefabBrush.m_PerlinScale = EditorGUILayout.Slider("Perlin Scale", prefabBrush.m_PerlinScale, 0.001f, 0.999f); - EditorGUILayout.PropertyField(m_Prefabs, true); - m_SerializedObject.ApplyModifiedPropertiesWithoutUndo(); + private PrefabRandomBrush prefabRandomBrush => target as PrefabRandomBrush; + + private SerializedProperty m_Prefabs; + + protected override void OnEnable() + { + base.OnEnable(); + m_Prefabs = m_SerializedObject.FindProperty("m_Prefabs"); + } + + /// + /// Callback for painting the inspector GUI for the PrefabBrush in the Tile Palette. + /// The PrefabBrush Editor overrides this to have a custom inspector for this Brush. + /// + public override void OnPaintInspectorGUI() + { + base.OnPaintInspectorGUI(); + m_SerializedObject.UpdateIfRequiredOrScript(); + prefabRandomBrush.m_PerlinScale = EditorGUILayout.Slider("Perlin Scale", prefabRandomBrush.m_PerlinScale, 0.001f, 0.999f); + EditorGUILayout.PropertyField(m_Prefabs, true); + prefabRandomBrush.m_EraseAnyObjects = EditorGUILayout.Toggle("Erase Any Objects", prefabRandomBrush.m_EraseAnyObjects); + m_SerializedObject.ApplyModifiedPropertiesWithoutUndo(); + } } } } \ No newline at end of file From 74307f8b9abb9b62bafbdd8a54711354c893ec88 Mon Sep 17 00:00:00 2001 From: RyotaMurohoshi Date: Sun, 17 May 2020 03:52:34 +0900 Subject: [PATCH 7/7] added #pragma warning disable 0649 --- Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs | 2 ++ .../PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs index 642bea5..73812fa 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabBrush/PrefabBrush.cs @@ -10,10 +10,12 @@ namespace UnityEditor.Tilemaps [CustomGridBrush(false, true, false, "Prefab Brush")] public class PrefabBrush : BasePrefabBrush { + #pragma warning disable 0649 /// /// The selection of Prefab to paint from /// [SerializeField] GameObject m_Prefab; + #pragma warning restore 0649 /// /// If true, erases any GameObjects that are in a given position within the selected layers with Erasing. diff --git a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs index 5364df4..c5c05f9 100644 --- a/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs +++ b/Editor/Brushes/PrefabBrushes/PrefabRandomBrush/PrefabRandomBrush.cs @@ -12,6 +12,7 @@ public class PrefabRandomBrush : BasePrefabBrush { private const float k_PerlinOffset = 100000f; + #pragma warning disable 0649 /// /// The selection of Prefabs to paint from /// @@ -21,6 +22,7 @@ public class PrefabRandomBrush : BasePrefabBrush /// Factor for distribution of choice of Prefabs to paint /// [SerializeField] float m_PerlinScale = 0.5f; + #pragma warning restore 0649 /// /// If true, erases any GameObjects that are in a given position within the selected layers with Erasing.