Question by
katy_cat · Apr 24, 2020 at 09:04 AM ·
performanceeditor-scriptingperformance optimizationcustom editor
Why is my Custom Inspector Causing Performance Issues?
I've attached the editor script and screenshots of the profiler breakdown (Both images taken from the same frame while multiselecting about 7 objects using this custom inspector).
Any help would be appreciated on how to improve the performance of this, I'm sure there is a simple solution that I'm just not seeing.
using UnityEngine;
using UnityEditor;
[CanEditMultipleObjects]
[CustomEditor(typeof(Button3D))]
public class Button3DEditor : Editor
{
SerializedProperty mesh;
SerializedProperty canvasRen;
SerializedProperty text;
SerializedProperty uguiText;
SerializedProperty highlightedColor;
SerializedProperty highlightedTextColor;
SerializedProperty pressColor;
SerializedProperty pressTextColor;
SerializedProperty disabledColor;
SerializedProperty disabledTextColor;
SerializedProperty highlightedMat;
SerializedProperty pressedMat;
SerializedProperty disabledMat;
SerializedProperty actionType;
SerializedProperty highlightedScale;
SerializedProperty pressedScale;
SerializedProperty disabledScale;
SerializedProperty highlightedTexture;
SerializedProperty pressedTexture;
SerializedProperty disabledTexture;
private GUIContent ActionType;
private GUIContent CanvasRenderer;
private GUIContent TextObject;
private GUIContent MeshObject;
private GUIContent HighlightColor;
private GUIContent PressedColor;
private GUIContent DisabledColor;
private GUIContent HighlightTexture;
private GUIContent PressedTexture;
private GUIContent DisabledTexture;
private GUIContent HighlightMaterial;
private GUIContent PressedMaterial;
private GUIContent DisabledMaterial;
private GUIContent HighlightScale;
private GUIContent PressedScale;
private GUIContent DisabledScale;
private GUIContent HighlightTextColor;
private GUIContent PressedTextColor;
private GUIContent DisabledTextColor;
private void OnEnable()
{
highlightedColor = serializedObject.FindProperty("highlightedColor");
highlightedTextColor = serializedObject.FindProperty("highlightedTextColor");
pressColor = serializedObject.FindProperty("pressColor");
pressTextColor = serializedObject.FindProperty("pressTextColor");
disabledColor = serializedObject.FindProperty("disabledColor");
disabledTextColor = serializedObject.FindProperty("disabledTextColor");
highlightedMat = serializedObject.FindProperty("highlight");
disabledMat = serializedObject.FindProperty("disabled");
pressedMat = serializedObject.FindProperty("pressedMat");
text = serializedObject.FindProperty("text");
uguiText = serializedObject.FindProperty("uguiText");
mesh = serializedObject.FindProperty("mesh");
canvasRen = serializedObject.FindProperty("canvasRen");
actionType = serializedObject.FindProperty("actionType");
highlightedScale = serializedObject.FindProperty("highlightedScale");
pressedScale = serializedObject.FindProperty("pressScale");
disabledScale = serializedObject.FindProperty("disabledScale");
highlightedTexture = serializedObject.FindProperty("highlightedTex");
pressedTexture = serializedObject.FindProperty("pressedTex");
disabledTexture = serializedObject.FindProperty("disabledTex");
ActionType = new GUIContent("Action Type");
CanvasRenderer = new GUIContent("Canvas Renderer");
TextObject = new GUIContent("Text Object");
MeshObject = new GUIContent("Mesh Object");
HighlightColor = new GUIContent("Color when highlighted");
PressedColor = new GUIContent("Color when pressed");
DisabledColor = new GUIContent("Color when disabled");
HighlightTexture = new GUIContent("Highlight Texture");
PressedTexture = new GUIContent("Pressed Texture");
DisabledTexture = new GUIContent("Disabled Texture");
HighlightMaterial = new GUIContent("Highlighted Material");
PressedMaterial = new GUIContent("Pressed Material");
DisabledMaterial = new GUIContent("Disabled Material");
HighlightScale = new GUIContent("Highlight Scale Factor");
PressedScale = new GUIContent("Pressed Scale Factor");
DisabledScale = new GUIContent("Disabled Scale Factor");
HighlightTextColor = new GUIContent("Highlighted Text Color");
PressedTextColor = new GUIContent("Press Text Color");
DisabledTextColor = new GUIContent("Disabled Text Color");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
base.OnInspectorGUI();
EditorGUILayout.PropertyField(actionType, ActionType);
//mesh options
if ((target as Button3D).gameObject.transform as RectTransform != null)
{
EditorGUILayout.PropertyField(canvasRen, CanvasRenderer);
EditorGUILayout.PropertyField(uguiText, TextObject);
}
else
{
EditorGUILayout.PropertyField(mesh, MeshObject);
EditorGUILayout.PropertyField(text, TextObject);
}
if ((mesh != null && (mesh.objectReferenceValue as MeshRenderer) != null) || (canvasRen != null && (canvasRen.objectReferenceValue as CanvasRenderer) != null))
{
if (((Button3D.ActionType)actionType.intValue & Button3D.ActionType.ColorChange) != 0)
{
EditorGUILayout.PropertyField(highlightedColor, HighlightColor);
EditorGUILayout.PropertyField(pressColor, PressedColor);
EditorGUILayout.PropertyField(disabledColor,DisabledColor);
}
if (((Button3D.ActionType)actionType.intValue & Button3D.ActionType.TextureChange) != 0)
{
EditorGUILayout.PropertyField(highlightedTexture, HighlightTexture);
EditorGUILayout.PropertyField(pressedTexture,PressedTexture);
EditorGUILayout.PropertyField(disabledTexture,DisabledTexture);
}
if (((Button3D.ActionType)actionType.intValue & Button3D.ActionType.MaterialChange) != 0)
{
EditorGUILayout.PropertyField(highlightedMat, HighlightMaterial);
EditorGUILayout.PropertyField(pressedMat, PressedMaterial);
EditorGUILayout.PropertyField(disabledMat,DisabledMaterial);
}
if (((Button3D.ActionType)actionType.intValue & Button3D.ActionType.ScaleChange) != 0)
{
EditorGUILayout.PropertyField(highlightedScale, HighlightScale);
EditorGUILayout.PropertyField(pressedScale, PressedScale);
EditorGUILayout.PropertyField(disabledScale, DisabledScale);
}
}
if ((text != null && (text.objectReferenceValue as TMPro.TextMeshPro) != null) || (uguiText != null && (uguiText.objectReferenceValue as UnityEngine.UI.Text) != null))
{
EditorGUILayout.PropertyField(highlightedTextColor, HighlightTextColor);
EditorGUILayout.PropertyField(pressTextColor, PressedTextColor);
EditorGUILayout.PropertyField(disabledTextColor, DisabledTextColor);
}
if (serializedObject.hasModifiedProperties)
serializedObject.ApplyModifiedProperties();
}
}
Comment
Your answer
Follow this Question
Related Questions
How to edit array/list property with custom PropertyDrawer? 3 Answers
GameView.GetMainGameViewTargetSize() problem 0 Answers
Profiler knurl ... 0 Answers
any physical/practical limit to number of lights in view? 0 Answers
Different pc performance problem 0 Answers