// ===================================================================== // Copyright 2013-2017 Fluffy Underware // All rights reserved // // http://www.fluffyunderware.com // ===================================================================== using UnityEngine; using UnityEditor; using System.Collections.Generic; using FluffyUnderware.DevTools.Extensions; namespace FluffyUnderware.DevToolsEditor { public static class DTSelection { static GameObject mCurrentGameObject; static int mCurrentGameObjectsCount; public static event DT.Callback OnSelectionChange; internal static bool MuteEvents { get; set; } public static int Count { get { return mCurrentGameObjectsCount; } } internal static void Initialize() { EditorApplication.update -= checkSelection; //TODO listen on Selection.selectionChanged instead? EditorApplication.update += checkSelection; } public static bool HasComponent(bool allowMultiSelection = false) where T : Component { if (allowMultiSelection) return mCurrentGameObjectsCount >= 1 && getSelection().Count > 0; else return mCurrentGameObjectsCount == 1 && mCurrentGameObject != null && mCurrentGameObject.GetComponent(typeof(T)) != null; } public static bool HasComponent(bool allowMultiSelection = false) where T : Component where T1 : Component { return HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection); } public static bool HasComponent(bool allowMultiSelection = false) where T : Component where T1 : Component where T2 : Component { return HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection); } public static bool HasComponent(bool allowMultiSelection = false) where T : Component where T1 : Component where T2 : Component where T3 : Component { return HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection); } public static bool HasComponent(bool allowMultiSelection = false) where T : Component where T1 : Component where T2 : Component where T3 : Component where T4 : Component { return HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection) || HasComponent(allowMultiSelection); } public static T GetAs(bool allowMultiSelection = false) where T : Component { List ar = getSelection(); if (ar.Count == 1 || (ar.Count > 0 && allowMultiSelection)) return (T)ar[0]; else return null; } public static List GetAllAs() where T : Component { return getSelection(); } public static GameObject GetGameObject(bool allowMultiSelection = false) { GameObject[] selectedGameObjects = Selection.gameObjects; if (selectedGameObjects.Length == 1 || (selectedGameObjects.Length > 1 && allowMultiSelection)) return Selection.activeGameObject; else return null; } public static Vector3 GetPosition() { Vector3 p = Vector3.zero; List tt = DTSelection.GetAllAs(); if (tt.Count > 0) { for (int i = 0; i < tt.Count; i++) p += tt[i].position; p /= tt.Count; } return p; } static List getSelection() where T : Component { Object[] S = Selection.GetFiltered(typeof(T), SelectionMode.TopLevel | SelectionMode.ExcludePrefab | SelectionMode.Editable); List L = new List(); foreach (Object O in S) if (O != null) { if (Selection.activeTransform == ((Component)O).transform) L.Insert(0, (T)O); else L.Add((T)O); } return L; } public static void Clear() { Selection.objects = new Object[0]; mCurrentGameObject = null; mCurrentGameObjectsCount = 0; raiseOnSelectionChange(); } public static void Set(params Object[] objects) { if (objects.Length > 0) { Selection.objects = objects; mCurrentGameObject = Selection.activeGameObject; mCurrentGameObjectsCount = Selection.gameObjects.Length; raiseOnSelectionChange(); } else Clear(); } public static void AddGameObjects(params Component[] objects) { List gl = new List(objects.Length); foreach (Component c in objects) if (c != null && c.gameObject != null && !gl.Contains(c.gameObject) && !Selection.Contains(c)) gl.Add(c.gameObject); if (gl.Count > 0) { GameObject[] cur = Selection.gameObjects; Selection.objects = cur.AddRange(gl.ToArray()); } } public static void SetGameObjects(params Component[] objects) { if (objects.Length > 0) { List gl = new List(objects.Length); foreach (Component c in objects) if (c != null && c.gameObject != null && !gl.Contains(c.gameObject)) gl.Add(c.gameObject); Selection.objects = gl.ToArray(); mCurrentGameObject = Selection.activeGameObject; mCurrentGameObjectsCount = Selection.gameObjects.Length; raiseOnSelectionChange(); } else Clear(); } static void checkSelection() { GameObject[] selectedGameObjects = Selection.gameObjects; if (!MuteEvents && Selection.activeGameObject != mCurrentGameObject || mCurrentGameObjectsCount != selectedGameObjects.Length) { mCurrentGameObject = Selection.activeGameObject; mCurrentGameObjectsCount = selectedGameObjects.Length; raiseOnSelectionChange(); } } static void raiseOnSelectionChange() { if (OnSelectionChange != null) OnSelectionChange(); } } }