- Home /
Question by
DubiDuboni · Oct 06, 2019 at 03:19 PM ·
c#guiguilayouttoggle buttonflag
How can I enable/disable a script or a method in a script from another editorwindow script ?
In the editorwindow script I have a toggle gui :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class CompareObjects : EditorWindow
{
bool enabledisable = false;
[MenuItem("Tools/Hierarchy Editor")]
public static void ShowWindow()
{
GetWindow<CompareObjects>("CompareObjects");
}
private void OnGUI()
{
CustomHierarchy.gameObjectFontColor = EditorGUILayout.ColorField("Original Font Color", CustomHierarchy.gameObjectFontColor);
CustomHierarchy.prefabOrgFontColor = EditorGUILayout.ColorField("Prefab Original Font Color", CustomHierarchy.prefabOrgFontColor);
CustomHierarchy.prefabModFontColor = EditorGUILayout.ColorField("Prefab Modified Font Color", CustomHierarchy.prefabModFontColor);
CustomHierarchy.inActiveColor = EditorGUILayout.ColorField("Inactive Color", CustomHierarchy.inActiveColor);
enabledisable = EditorGUILayout.Toggle("Enable", enabledisable);
if (enabledisable)
if (GUILayout.Button("Disable"))
{
}
}
}
And with this toggle I want to enable/disable the CustomHierarchy script : Either to disable the whole CustomHierarchy script as component or only the method name CustomHierarchy :
using System.Linq;
using UnityEditor;
using UnityEngine;
//Adapted from Unity3DCollege YouTube Video Tutorial https://www.youtube.com/watch?v=pdDrY8Mc2lU
[InitializeOnLoad]
public class CustomHierarchy : MonoBehaviour
{
private static Vector2 offset = new Vector2(0, 2);
public static Color gameObjectFontColor = Color.black;
public static Color prefabOrgFontColor = Color.black;
public static Color prefabModFontColor = Color.white;
public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
public static Color containsBoxColliderColor = Color.yellow;
static CustomHierarchy()
{
EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
}
private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
Color fontColor = gameObjectFontColor;
Color backgroundColor = new Color(.76f, .76f, .76f);
FontStyle styleFont = FontStyle.Normal;
var obj = EditorUtility.InstanceIDToObject(instanceID);
GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (Selection.instanceIDs.Contains(instanceID))
{
backgroundColor = new Color(0.24f, 0.48f, 0.90f);
}
if (obj != null)
{
var prefabType = PrefabUtility.GetPrefabType(obj);
if (gameObj.activeInHierarchy == false)
{
backgroundColor = inActiveColor;
}
if (gameObj.GetComponent<BoxCollider>() != null)
{
backgroundColor = containsBoxColliderColor;
}
if (prefabType == PrefabType.PrefabInstance)
{
styleFont = FontStyle.Bold;
PropertyModification[] prefabMods = PrefabUtility.GetPropertyModifications(obj);
foreach (PropertyModification prefabMod in prefabMods)
{
if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
{
fontColor = prefabModFontColor;
break;
}
}
if (fontColor != prefabModFontColor) fontColor = prefabOrgFontColor;
}
Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
EditorGUI.DrawRect(selectionRect, backgroundColor);
EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
{
normal = new GUIStyleState() { textColor = fontColor },
fontStyle = styleFont
}
);
}
}
}
I'm not sure what's more logic to disable/enable only this line :
EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
Or to disable/enable the whole CustomHierarchy script. But the idea is stop the CustomHierarchy from being doing the line :
EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
Comment