Last active
August 4, 2019 18:54
-
-
Save baobao/3354a834076d1ddbf5b5c225c89f517e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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