Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active August 4, 2019 18:54
Show Gist options
  • Save baobao/3354a834076d1ddbf5b5c225c89f517e to your computer and use it in GitHub Desktop.
Save baobao/3354a834076d1ddbf5b5c225c89f517e to your computer and use it in GitHub Desktop.
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEditor;
/// <summary>
/// SpriteRendererのSpriteがnullだったら背景を赤くするEditor拡張
/// </summary>
public class SpriteMissingChecker
{
[InitializeOnLoadMethod]
private static void AddHierarchyItemOnGUI()
{
EditorApplication.hierarchyWindowItemOnGUI -= HierarchyWindowItemOnGUI;
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
EditorApplication.projectWindowItemOnGUI -= ProjectWindowItemOnGUI;
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
}
/// <summary>
/// ProjectWindow OnGUI
/// </summary>
private static void ProjectWindowItemOnGUI(string guid, Rect selectionRect)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
if (Directory.Exists(path) ||
path.IndexOf(".prefab") < 0)
{
return;
}
// prefabのみ対象
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (IsValid(prefab))
{
Draw(selectionRect);
}
}
/// <summary>
/// HierarchyWindow OnGUI
/// </summary>
private static void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
var gameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (gameObject == null)
{
return;
}
if (IsValid(gameObject))
{
Draw(selectionRect);
}
}
/// <summary>
/// 見た目の処理
/// 背景を赤くする
/// </summary>
static void Draw(Rect rect)
{
var bg = GUI.backgroundColor;
GUI.backgroundColor = Color.red;
GUI.Box(rect, "");
GUI.backgroundColor = bg;
}
/// <summary>
/// 子階層全てのSpriteRendererのSpriteにnullが存在したらtrue
/// </summary>
static bool IsValid(GameObject go)
{
var arr = go.GetComponentsInChildren<SpriteRenderer>();
return arr.Any(x => x.sprite == null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment