Skip to content

Instantly share code, notes, and snippets.

@AnEmortalKid
Created June 11, 2023 19:19
Show Gist options
  • Save AnEmortalKid/e87c6bc960df647690e9a406b9441ff3 to your computer and use it in GitHub Desktop.
Save AnEmortalKid/e87c6bc960df647690e9a406b9441ff3 to your computer and use it in GitHub Desktop.
Prop Palette
using System.Collections.Generic;
using System.Linq;
using DontDrown.Prop;
using FullscreenEditor;
using UnityEngine;
#if UNITY_EDITOR
namespace DontDrown.Editor.Props
{
using UnityEditor;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;
using Sirenix.Utilities;
public class PropsGrid : OdinEditorWindow
{
[MenuItem("Tools/DontDrown/Prop Palette")]
private static void OpenWindow()
{
var window = GetWindow<PropsGrid>();
window.titleContent.text = "Prop Palette";
// Nifty little trick to quickly position the window in the middle of the editor.
window.position = GUIHelper.GetEditorWindowRect().AlignCenter(400, 420);
}
[TabGroup("All")]
[TableMatrix(SquareCells = true, IsReadOnly = true, HideRowIndices = true, HideColumnIndices = true)]
public GameObject[,] PrefabMatrix;
[TabGroup("Shallow Beach")]
[TableMatrix(SquareCells = true, IsReadOnly = true, HideRowIndices = true, HideColumnIndices = true)]
public GameObject[,] ShallowPrefabMatrix;
[TabGroup("Coral Castle")]
[TableMatrix(SquareCells = true, IsReadOnly = true, HideRowIndices = true, HideColumnIndices = true)]
public GameObject[,] CoralsPrefabMatrix;
[OnInspectorInit]
private void CreateData()
{
Dictionary<PropMetadata.PropType, IList<GameObject>> prefabsByType = new();
prefabsByType.Add(PropMetadata.PropType.SHALLOW_BEACHES, new List<GameObject>());
prefabsByType.Add(PropMetadata.PropType.CORAL_CASTLES, new List<GameObject>());
// find all game objects under the prefab folder
IList<GameObject> allPropPrefabs = new List<GameObject>();
string[] propGuids = AssetDatabase.FindAssets("t:Object", new[] { "Assets/Prefab/Prop" });
foreach (var guid in propGuids)
{
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(guid));
if (go != null && go.CompareTag("Prop"))
{
allPropPrefabs.Add(go);
PropMetadata metadata = go.GetComponent<PropMetadata>();
if (metadata != null)
{
PropMetadata pm = metadata;
PropMetadata.PropType type = pm.GetFieldValue<PropMetadata.PropType>("type");
prefabsByType[type].Add(go);
}
}
}
PrefabMatrix = MakeGrid(allPropPrefabs);
AddToGrid(PrefabMatrix, allPropPrefabs);
var shallowPrefabs = prefabsByType[PropMetadata.PropType.SHALLOW_BEACHES];
ShallowPrefabMatrix = MakeGrid(shallowPrefabs);
AddToGrid(ShallowPrefabMatrix, shallowPrefabs);
var coralPrefabs = prefabsByType[PropMetadata.PropType.CORAL_CASTLES];
CoralsPrefabMatrix = MakeGrid(coralPrefabs);
AddToGrid(CoralsPrefabMatrix, coralPrefabs);
}
private GameObject[,] MakeGrid(IList<GameObject> gameObjects)
{
// from the prefabs count how many rows we need
var rowsNeeded = (gameObjects.Count / 4) + (gameObjects.Count % 4 != 0 ? 1 : 0);
return new GameObject[4, rowsNeeded];
}
private void AddToGrid(GameObject[,] grid, IList<GameObject> gameObjects)
{
var sortedPrefabs = gameObjects.OrderBy(p => p.name).ToList();
// ez loop
int index = 0;
for (int row = 0; row < grid.GetLength(1); row++)
{
for (int col = 0; col < grid.GetLength(0); col++)
{
if (index < sortedPrefabs.Count)
{
grid[col, row] = sortedPrefabs[index];
index++;
}
}
}
}
}
}
#endif
using UnityEngine;
namespace DontDrown.Prop
{
/// <summary>
/// Categorizes the props
/// </summary>
public class PropMetadata : MonoBehaviour
{
[SerializeField] private PropType type;
public enum PropType
{
TUTORIAL = 1,
SHALLOW_BEACHES = 2,
CORAL_CASTLES = 3
}
}
}
@AnEmortalKid
Copy link
Author

AnEmortalKid commented Jun 11, 2023

A ListMatrix in odin that displays prefabs in a certain folder, with a certain tag and categorized with additional metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment