Skip to content

Instantly share code, notes, and snippets.

@marcosecchi
Created May 6, 2016 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcosecchi/98521e18dad00206a52a6c1b652c26f4 to your computer and use it in GitHub Desktop.
Save marcosecchi/98521e18dad00206a52a6c1b652c26f4 to your computer and use it in GitHub Desktop.
Check if the current selection is in the Unity3D Hierarchy Panel
using UnityEngine;
using UnityEditor;
public class HierarchyUtils {
/// <summary>
/// Determines if the current selection in the Hierarchy view.
/// </summary>
/// <returns><c>true</c> if is selection in hierarchy; otherwise, <c>false</c>.</returns>
private static bool IsSelectionInHierarchy() {
Object[] objs = Selection.objects;
if(objs.Length == 0) return false;
GameObject go;
foreach(Object obj in objs) {
go = obj as GameObject;
if(go == null) return false;
if(!IsInScene(go)) return false;
}
return true;
}
/// <summary>
/// Determines if a GameObject is in the Scene.
/// </summary>
/// <returns><c>true</c> if the GameObject is in the Scene view; otherwise, <c>false</c>.</returns>
/// <param name="go">The Game Object.</param>
private static bool IsInScene(GameObject go) {
return go != null && (PrefabUtility.GetPrefabType(go).ToString().EndsWith("Instance") || PrefabUtility.GetPrefabType(go) == PrefabType.None);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment