Skip to content

Commit

Permalink
#168 Updated TileModifierPropertyDrawer to properly display RuleTile …
Browse files Browse the repository at this point in the history
…and other classes inherited from TileBase
  • Loading branch information
Vladimir Pechorin authored and h8man committed May 12, 2024
1 parent dd2842a commit f173cb7
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions NavMeshComponents/Editor/NavMeshModifierTilemapEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ private void AddUsedTiles(Tilemap tilemap, NavMeshModifierTilemap modifierTilema
[CustomPropertyDrawer(typeof(NavMeshModifierTilemap.TileModifier))]
class TileModifierPropertyDrawer : PropertyDrawer
{

private static Dictionary<Object, Texture2D> Previews;

private Rect ClaimAdvance(ref Rect position, float height)
{
Rect retVal = position;
Expand Down Expand Up @@ -117,10 +120,28 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
EditorGUI.PropertyField(tileRect, tileProperty);
TileBase tileBase = tileProperty.objectReferenceValue as TileBase;
TileData tileData = new TileData();
tileBase?.GetTileData(Vector3Int.zero, null, ref tileData);
if (tileData.sprite)
Texture textureToDraw;
try
{
EditorGUI.DrawPreviewTexture(previewRect, tileData.sprite?.texture, null, ScaleMode.ScaleToFit, 0);
tileBase?.GetTileData(Vector3Int.zero, null, ref tileData);
textureToDraw = tileData.sprite?.texture;
}
catch
{
try
{

textureToDraw = GetPreview(tileBase);
}
catch
{
textureToDraw = EditorGUIUtility.IconContent("console.erroricon.sml").image;
}
}

if (textureToDraw)
{
EditorGUI.DrawPreviewTexture(previewRect, textureToDraw, null, ScaleMode.ScaleToFit, 0);
}

Rect toggleRect = ClaimAdvance(ref position, 20);
Expand All @@ -138,6 +159,26 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
}

static Texture2D GetPreview(Object objectToPreview)
{
int maxResolution = 128;
Previews ??= new();
if (!Previews.TryGetValue(objectToPreview, out var preview) || preview == null)
{
var path = AssetDatabase.GetAssetPath(objectToPreview);
if (objectToPreview)
{
var editor = CreateEditor(objectToPreview);
preview = editor.RenderStaticPreview(path, null, maxResolution, maxResolution);
preview.Apply();
DestroyImmediate(editor);
Previews[objectToPreview] = preview;
}
}

return preview;
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property.isExpanded)
Expand Down

0 comments on commit f173cb7

Please sign in to comment.