private static void ChildToggle() { using (ProfilerSample.Get()) { if (!Preferences.NumericChildExpand || !IsRepaintEvent || !IsGameObject || CurrentGameObject.transform.childCount <= 0) { return; } var rect = RawRect; var childString = CurrentGameObject.transform.childCount.ToString("00"); var expanded = ReflectionHelper.GetTransformIsExpanded(CurrentGameObject); rect.xMax = rect.xMin - 1f; rect.xMin -= 15f; if (childString.Length > 2) { rect.xMin -= 4f; } tempChildExpandContent.text = childString; using (new GUIBackgroundColor(Styles.childToggleColor)) Styles.newToggleStyle.Draw(rect, tempChildExpandContent, false, false, expanded, false); } }
private static void IgnoreLockedSelection() { if (Preferences.AllowSelectingLocked || !IsFirstVisible || !IsRepaintEvent) { return; } using (ProfilerSample.Get()) { var selection = Selection.objects; var changed = false; for (var i = 0; i < selection.Length; i++) { if (selection[i] is GameObject && (selection[i].hideFlags & HideFlags.NotEditable) != 0 && !EditorUtility.IsPersistent(selection[i])) { selection[i] = null; changed = true; } } if (changed) { Selection.objects = selection; ReflectionHelper.SetHierarchySelectionNeedSync(); EditorApplication.RepaintHierarchyWindow(); } } }
public static NameColor GetRowCustomTint_Name(GameObject go) { using (ProfilerSample.Get()) { if (!go) { return(new NameColor()); } var NameRules = Preferences.PerNameRowColors.Value; if (NameRules == null) { return(new NameColor()); } var goName = go.name; for (var i = 0; i < NameRules.Count; i++) { if (/*alreadyColored[i].name*/ goName.EndsWith("_d")) { return(NameRules[i]); } } return(new NameColor()); } }
private static float DoTrailing(Rect rect) { if (!IsRepaintEvent || !Preferences.Trailing || !IsGameObject) { return(rect.xMax); } using (ProfilerSample.Get()) { var size = CurrentStyle.CalcSize(Utility.GetTempGUIContent(GameObjectName)); var iconsWidth = RightIconsWidth + LeftIconsWidth + CalcMiniLabelSize() + Preferences.RightMargin; if (size.x < rect.width - iconsWidth + 15f) { return(rect.xMax); } rect.xMin = rect.xMax - iconsWidth - 18f; if (Selection.gameObjects.Contains(CurrentGameObject)) { EditorGUI.DrawRect(rect, ReflectionHelper.HierarchyFocused ? Styles.selectedFocusedColor : Styles.selectedUnfocusedColor); } else { EditorGUI.DrawRect(rect, Styles.normalColor); } rect.x -= 16f; rect.yMax -= 3f; EditorGUI.LabelField(rect, trailingContent, CurrentStyle); return(rect.xMin + 28f); } }
public static void SetObjectIcon(Object obj, Texture2D texture) { using (ProfilerSample.Get()) { typeof(EditorGUIUtility).InvokeStaticMethod("SetIconForObject", obj, texture); EditorUtility.SetDirty(obj); } }
public static Color GetRowLayerTint(GameObject go) { if (!go) { return(Color.clear); } var layerColors = Preferences.PerLayerRowColors.Value; if (layerColors == null) { return(Color.clear); } using (ProfilerSample.Get()) for (var i = 0; i < layerColors.Count; i++) { if (layerColors[i] == go.layer) { return(layerColors[i].color); } } return(Color.clear); }
public static void ShowIconSelector(Object targetObj, Rect activatorRect, bool showLabelIcons, OnObjectIconChange onObjectChange) { using (ProfilerSample.Get()) try { var instance = ScriptableObject.CreateInstance(iconSelectorType); var update = new EditorApplication.CallbackFunction(() => { }); instance.InvokeMethod("Init", targetObj, activatorRect, showLabelIcons); update += () => { if (instance) { return; } onObjectChange(GetObjectIcon(targetObj)); EditorApplication.update -= update; }; EditorApplication.update += update; } catch (Exception e) { Debug.LogWarning("Failed to open icon selector\n" + e); } }
public static GUIStyle GetHierarchyLabelStyle(GameObject go) { using (ProfilerSample.Get()) { if (!go) { return(EditorStyles.label); } var prefabType = PrefabUtility.GetPrefabType(PrefabUtility.FindPrefabRoot(go)); var active = go.activeInHierarchy; switch (prefabType) { case PrefabType.PrefabInstance: case PrefabType.ModelPrefabInstance: return(active ? Styles.labelPrefab : Styles.labelPrefabDisabled); case PrefabType.MissingPrefabInstance: return(active ? Styles.labelPrefabBroken : Styles.labelPrefabBrokenDisabled); default: return(active ? Styles.labelNormal : Styles.labelDisabled); } } }
private static void DrawLeftSideIcons(Rect rect) { if (!IsGameObject || LeftIconsWidth == 0f) { return; } using (ProfilerSample.Get()) { rect.xMin += LabelSize; rect.xMin = Math.Min(rect.xMax - RightIconsWidth - LeftIconsWidth - CalcMiniLabelSize() - 5f - Preferences.Offset, rect.xMin); foreach (var icon in Preferences.LeftIcons.Value) { try { rect.xMax = rect.xMin + icon.Width; icon.DoGUI(rect); rect.xMin = rect.xMax; } catch (Exception e) { Debug.LogException(e); Preferences.ForceDisableButton(icon); } } } }
public static LayerColor GetRowCustomTint(GameObject go) { using (ProfilerSample.Get()) { if (!go) { return(new LayerColor()); } var layerColors = Preferences.PerLayerRowColors.Value; if (layerColors == null) { return(new LayerColor()); } var goLayer = go.layer; for (var i = 0; i < layerColors.Count; i++) { if (layerColors[i] == goLayer) { return(layerColors[i]); } } return(new LayerColor()); } }
private static void DrawMiniLabel(ref Rect rect) { if (!IsGameObject) { return; } rect.x -= 3f; using (ProfilerSample.Get()) switch (MiniLabelProviders.Length) { case 0: return; case 1: if (MiniLabelProviders[0].HasValue()) { MiniLabelProviders[0].Draw(ref rect); } break; default: var ml0 = MiniLabelProviders[0]; var ml1 = MiniLabelProviders[1]; var ml0HasValue = ml0.HasValue(); var ml1HasValue = ml1.HasValue(); if (ml0HasValue && ml1HasValue || !Preferences.CentralizeMiniLabelWhenPossible) { var topRect = rect; var bottomRect = rect; topRect.yMax = RawRect.yMax - RawRect.height / 2f; bottomRect.yMin = RawRect.yMin + RawRect.height / 2f; if (ml0HasValue) { ml0.Draw(ref topRect); } if (ml1HasValue) { ml1.Draw(ref bottomRect); } rect.xMin = Mathf.Min(topRect.xMin, bottomRect.xMin); } else if (ml1HasValue) { ml1.Draw(ref rect); } else if (ml0HasValue) { ml0.Draw(ref rect); } break; } }
public static void SetItemInformation(int id, Rect rect) { if (!Preferences.Enabled) { return; } using (ProfilerSample.Get("Enhanced Hierarchy")) using (ProfilerSample.Get()) try { CurrentGameObject = EditorUtility.InstanceIDToObject(id) as GameObject; IsGameObject = CurrentGameObject; IsRepaintEvent = Event.current.type == EventType.Repaint; IsFirstVisible = Event.current.type != LastEventType; LastEventType = Event.current.type; if (IsGameObject) { GameObjectName = CurrentGameObject.name; try { GameObjectTag = CurrentGameObject.tag; } catch { // I couldn't reproduce this, but it can happen if (Preferences.DebugEnabled) { Debug.LogWarning("Invalid gameobject tag", CurrentGameObject); } GameObjectTag = "Untagged"; } LabelSize = EditorStyles.label.CalcSize(Utility.GetTempGUIContent(GameObjectName)).x; LabelSize += Reflected.IconWidth + 5f; // Icon size var labelOnlyRect = rect; labelOnlyRect.xMax = labelOnlyRect.xMin + LabelSize; LabelOnlyRect = labelOnlyRect; HasTag = !CurrentGameObject.CompareTag(UNTAGGED) || !Preferences.HideDefaultTag; HasLayer = CurrentGameObject.layer != UNLAYERED || !Preferences.HideDefaultLayer; CurrentStyle = Utility.GetHierarchyLabelStyle(CurrentGameObject); CurrentColor = CurrentStyle.normal.textColor; CurrentGameObject.GetComponents(Components); } if (IsFirstVisible) { FinalRect = RawRect; } RawRect = rect; rect.xMin = 0f; //rect.xMax = EditorGUIUtility.currentViewWidth; FullSizeRect = rect; } catch (Exception e) { Utility.LogException(e); } }
private static void DrawTooltip(Rect rect, float fullTrailingWidth) { if (!Preferences.Tooltips || !IsGameObject || !IsRepaintEvent) { return; } using (ProfilerSample.Get()) { if (DragSelection != null) { return; } rect.xMax = Mathf.Min(fullTrailingWidth, rect.xMin + LabelSize); rect.xMin = 0f; if (!Utility.ShouldCalculateTooltipAt(rect)) { return; } var tooltip = new StringBuilder(100); tooltip.AppendLine(GameObjectName); tooltip.AppendFormat("\nTag: {0}", GameObjectTag); tooltip.AppendFormat("\nLayer: {0}", LayerMask.LayerToName(CurrentGameObject.layer)); if (GameObjectUtility.GetStaticEditorFlags(CurrentGameObject) != 0) { tooltip.AppendFormat("\nStatic: {0}", Utility.EnumFlagsToString(GameObjectUtility.GetStaticEditorFlags(CurrentGameObject))); } tooltip.AppendLine(); tooltip.AppendLine(); var components = CurrentGameObject.GetComponents <Component>(); foreach (var component in components) { if (component is Transform) { continue; } else if (component) { tooltip.AppendLine(ObjectNames.GetInspectorTitle(component)); } else { tooltip.AppendLine("Missing Component"); } } EditorGUI.LabelField(rect, Utility.GetTempGUIContent(null, tooltip.ToString().TrimEnd('\n', '\r'))); } }
public static Type FindClass(string name, Assembly assembly) { if (assembly == null) { return(null); } using (ProfilerSample.Get(name)) return(assembly.GetType(name, false, true)); }
private static void DrawTree(Rect rect) { if (Preferences.TreeOpacity <= ALPHA_THRESHOLD || !IsGameObject) { return; } if (!IsRepaintEvent && !Preferences.SelectOnTree) { return; } using (ProfilerSample.Get()) using (new GUIColor(CurrentColor, Preferences.TreeOpacity)) { var indent = 16f; if (Reflected.HierarchyArea.Supported) { indent = Reflected.HierarchyArea.IndentWidth; } rect.xMin -= 14f; rect.xMax = rect.xMin + 14f; if (CurrentGameObject.transform.childCount == 0 && CurrentGameObject.transform.parent) { GUI.DrawTexture(rect, Utility.LastInHierarchy(CurrentGameObject.transform) ? Styles.treeEndTexture : Styles.treeMiddleTexture); if (Preferences.SelectOnTree && GUI.Button(rect, GUIContent.none, Styles.labelNormal)) { Selection.activeTransform = CurrentGameObject.transform.parent; } } var currentTransform = CurrentGameObject.transform.parent; for (rect.x -= indent; rect.xMin > 0f && currentTransform && currentTransform.parent; rect.x -= indent) { if (!Utility.LastInHierarchy(currentTransform)) { using (new GUIColor(Utility.GetHierarchyColor(currentTransform.parent), Preferences.TreeOpacity)) { GUI.DrawTexture(rect, Styles.treeLineTexture); if (Preferences.SelectOnTree && GUI.Button(rect, GUIContent.none, Styles.labelNormal)) { Selection.activeTransform = currentTransform.parent; } } } currentTransform = currentTransform.parent; } } }
public static bool TransformIsLastChild(Transform t) { using (ProfilerSample.Get()) { if (!t) { return(true); } return(t.GetSiblingIndex() == t.parent.childCount - 1); } }
public static void UnlockAllObjects() { using (ProfilerSample.Get()) foreach (var obj in Resources.FindObjectsOfTypeAll <GameObject>()) { if (obj && (obj.hideFlags & HideFlags.HideInHierarchy) == 0 && !EditorUtility.IsPersistent(obj)) { UnlockObject(obj); } } }
public static Color OverlayColors(Color src, Color dst) { using (ProfilerSample.Get()) { var alpha = dst.a + src.a * (1f - dst.a); var result = (dst * dst.a + src * src.a * (1f - dst.a)) / alpha; result.a = alpha; return(result); } }
public static bool LastInHierarchy(Transform t) { using (ProfilerSample.Get()) { if (!t) { return(true); } return(t.parent.GetChild(t.parent.childCount - 1) == t); } }
public static Color GetRowLayerTint(Rect rect, GameObject go) { using (ProfilerSample.Get()) if (go) { return(Array.Find <LayerColor>(Preferences.PerLayerRowColors, layer => { return layer == go.layer; }).color); } else { return(Color.clear); } }
public static Color GetRowTint(Rect rect) { using (ProfilerSample.Get()) if (rect.y / RawRect.height % 2 >= 0.5f) { return(Preferences.OddRowColor); } else { return(Preferences.EvenRowColor); } }
private static void ColorSort(Rect rect) { if (!IsRepaintEvent) { return; } using (ProfilerSample.Get()) { rect.xMin = 0f; rect.xMax = rect.xMax + 50f; var rowTint = GetRowTint(); var rowLayerTint = GetRowLayerTint(); if (rowLayerTint.a > ALPHA_THRESHOLD) { using (new GUIColor(rowLayerTint)) GUI.DrawTexture(rect, Styles.fadeTexture, ScaleMode.StretchToFill); } if (rowTint.a > ALPHA_THRESHOLD) { EditorGUI.DrawRect(rect, rowTint); } if (!IsFirstVisible) { return; } rect.y = FinalRect.y; var height = ReflectionHelper.HierarchyWindowInstance.position.height; var count = (height - FinalRect.y) / FinalRect.height; if (FinalRect.height <= 0f) { count = 100f; } for (var i = 0; i < count; i++) { rect.y += RawRect.height; rowTint = GetRowTint(rect); if (rowTint.a > ALPHA_THRESHOLD) { EditorGUI.DrawRect(rect, rowTint); } } } }
public static void SetHierarchySelectionNeedSync() { using (ProfilerSample.Get()) try { if (HierarchyWindowInstance) { syncNeededProp.SetValue(HierarchyWindowInstance, true, null); } } catch (Exception e) { Utility.LogException(e); } }
public static void UnlockObject(GameObject go) { using (ProfilerSample.Get()) { go.hideFlags &= ~HideFlags.NotEditable; foreach (var comp in go.GetComponents <Component>().Where(comp => comp && !(comp is Transform))) { comp.hideFlags &= ~HideFlags.NotEditable; comp.hideFlags &= ~HideFlags.HideInHierarchy; } EditorUtility.SetDirty(go); } }
public static void SetHierarchySelectionNeedSync() { using (ProfilerSample.Get()) try { if (HierarchyWindowInstance) { SceneHierarchyOrWindow.SetInstanceProperty("selectionSyncNeeded", true); } } catch (Exception e) { Debug.LogWarningFormat("Enabling \"{0}\" because it caused an exception", Preferences.AllowSelectingLockedObjects.Label.text); Debug.LogException(e); Preferences.AllowSelectingLockedObjects.Value = true; } }
public static MethodInfo FindMethod(this Type type, string methodName, Type[] parameters = null) { using (ProfilerSample.Get(methodName)) try { return(cachedMethods[methodName]); } catch { if (parameters == null) { return(cachedMethods[methodName] = type.GetMethod(methodName, FULL_BINDING)); } else { return(cachedMethods[methodName] = type.GetMethod(methodName, FULL_BINDING, null, parameters, null)); } } }
public static bool GetTransformIsExpanded(GameObject go) { using (ProfilerSample.Get()) try { var data = TreeView.GetInstanceProperty <object>("data"); var isExpanded = data.InvokeMethod <bool, int>("IsExpanded", go.GetInstanceID()); return(isExpanded); } catch (Exception e) { Preferences.NumericChildExpand.Value = false; Debug.LogException(e); Debug.LogWarningFormat("Disabled \"{0}\" because it failed to get hierarchy info", Preferences.NumericChildExpand.Label.text); return(false); } }
public static void EnableFPSCounter() { var frames = 0; var fps = 0d; var lastTime = 0d; var titleProperty = ReflectionHelper.GetHierarchyTitleProperty(); var isTitleContent = titleProperty.Name == "titleContent"; var content = new GUIContent(); var evt = EventType.Repaint; EditorApplication.hierarchyWindowItemOnGUI += (id, rect) => { using (ProfilerSample.Get("Enhanced Hierarchy")) using (ProfilerSample.Get("FPS Counter")) { if (evt == Event.current.type) { return; } evt = Event.current.type; if (evt == EventType.Repaint) { frames++; } if (EditorApplication.timeSinceStartup - lastTime < 0.5d) { return; } fps = frames / (EditorApplication.timeSinceStartup - lastTime); lastTime = EditorApplication.timeSinceStartup; frames = 0; content.text = string.Format("{0:00.0} FPS", fps); content.image = Styles.warningIcon; if (isTitleContent) { titleProperty.SetValue(ReflectionHelper.HierarchyWindowInstance, content, null); } else { titleProperty.SetValue(ReflectionHelper.HierarchyWindowInstance, content.text, null); } } }; }
public static Texture2D LoadTexture(byte[] bytes, string name) { using (ProfilerSample.Get()) try { var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false, false); texture.name = name; texture.hideFlags = HideFlags.HideAndDontSave; texture.LoadImage(bytes); return(texture); } catch (Exception e) { Debug.LogErrorFormat("Failed to load texture \"{0}\": {1}", name, e); return(null); } }
public static void UnlockObject(GameObject go) { using (ProfilerSample.Get()) { go.hideFlags &= ~HideFlags.NotEditable; ApplyHideFlagsToPrefab(go); #if UNITY_2019_3_OR_NEWER if (!Preferences.AllowPickingLockedObjects) { SceneVisibilityManager.instance.EnablePicking(go, false); } #endif EditorUtility.SetDirty(go); } }