- Home /
Editor Script only executes when clicking away.
I have an editor script which assigns functions and text to buttons when you click the custom inspector button 'Generate Button Functionality'. However when I click that button it doesn't show any changes until I click on something else in the hierarchy. I've messed around with serializedObject.ApplyModifiedProperties however that hasn't worked and can't find any other information online about this issue. Sometimes the function isn't even assigned when pressing the button and I have to click it multiple times and click away to see if it has updated, its very wierd.
This is the script that draws the custom button to the inspector.
using UnityEngine;
using UnityEditor;
using UnityEngine.Events;
namespace ManYouAreCool.Menu
{
[CustomEditor(typeof(ButtonCustomisation))]
public class ButtonDrawer : Editor
{
private bool generated;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
ButtonCustomisation script = (ButtonCustomisation)target;
GUILayout.BeginHorizontal();
if (GUILayout.Button("Generate Button Functionality"))
{
if (!generated)
{
script.AssignButtonFunctions();
generated = true;
}
else Debug.LogError("already generated");
}
if (GUILayout.Button("Reset Button Functionality"))
{
if (generated)
{
script.ResetButtonFunctions();
generated = false;
}
else Debug.LogError("not generated");
}
GUILayout.EndHorizontal();
}
}
}
This is the referenced script that has the button functionality.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using System;
using UnityEditor.Events;
using TMPro;
namespace ManYouAreCool.Menu
{
public class ButtonCustomisation : MonoBehaviour
{
#region Variables
[Tooltip("Type the exact name of the button in the hierarchy")]
[SerializeField] private string playButtonName;
[Tooltip("This is the text displayed on the button within the game")]
[SerializeField] private string playDisplayText;
[Tooltip("Type the exact name of the button in the hierarchy")]
[SerializeField] private string optionsButtonName;
[Tooltip("This is the text displayed on the button within the game")]
[SerializeField] private string optionsDisplayText;
[Tooltip("Type the exact name of the button in the hierarchy")]
[SerializeField] private string quitButtonName;
[Tooltip("This is the text displayed on the button within the game")]
[SerializeField] private string quitDisplayText;
[Tooltip("Type the exact name of the button in the hierarchy")]
[SerializeField] private string backButtonName;
[Tooltip("This is the text displayed on the button within the game")]
[SerializeField] private string backDisplayText;
[Tooltip("The scene loaded when pressing the 'Play' button")]
[SerializeField] private string gameScene;
[Tooltip("The panel holding everything within the main menu")]
[SerializeField] private GameObject mainMenu;
[Tooltip("The panel holding everything within the options menu")]
[SerializeField] private GameObject optionsMenu;
#endregion
/// <summary> AddPersistentListener creates an OnClick() function for a button in edit mode. </summary>
public void AssignButtonFunctions()
{
#if UNITY_EDITOR
UnityEventTools.AddPersistentListener(GameObject.Find(playButtonName).GetComponent<Button>().onClick, new UnityAction(Play));
GameObject.Find(playButtonName).GetComponentInChildren<TextMeshProUGUI>().text = playDisplayText;
UnityEventTools.AddPersistentListener(GameObject.Find(optionsButtonName).GetComponent<Button>().onClick, new UnityAction(Options));
GameObject.Find(optionsButtonName).GetComponentInChildren<TextMeshProUGUI>().text = optionsDisplayText;
UnityEventTools.AddPersistentListener(GameObject.Find(quitButtonName).GetComponent<Button>().onClick, new UnityAction(Quit));
GameObject.Find(quitButtonName).GetComponentInChildren<TextMeshProUGUI>().text = quitDisplayText;
UnityEventTools.AddPersistentListener(GameObject.Find(backButtonName).GetComponent<Button>().onClick, new UnityAction(Back));
GameObject.Find(backButtonName).GetComponentInChildren<TextMeshProUGUI>().text = backDisplayText;
#endif
}
/// <summary> RemovePersistentListener removes an OnClick() function for a button in edit mode. </summary>
public void ResetButtonFunctions()
{
#if UNITY_EDITOR
UnityEventTools.RemovePersistentListener(GameObject.Find(playButtonName).GetComponent<Button>().onClick, new UnityAction(Play));
GameObject.Find(playButtonName).GetComponentInChildren<TextMeshProUGUI>().text = "";
UnityEventTools.RemovePersistentListener(GameObject.Find(optionsButtonName).GetComponent<Button>().onClick, new UnityAction(Options));
GameObject.Find(optionsButtonName).GetComponentInChildren<TextMeshProUGUI>().text = "";
UnityEventTools.RemovePersistentListener(GameObject.Find(quitButtonName).GetComponent<Button>().onClick, new UnityAction(Quit));
GameObject.Find(quitButtonName).GetComponentInChildren<TextMeshProUGUI>().text = "";
UnityEventTools.RemovePersistentListener(GameObject.Find(backButtonName).GetComponent<Button>().onClick, new UnityAction(Back));
GameObject.Find(backButtonName).GetComponentInChildren<TextMeshProUGUI>().text = "";
#endif
}
#region Button Functions
public void Play() => SceneManager.LoadScene(gameScene);
private void Options()
{
mainMenu.SetActive(false);
optionsMenu.SetActive(true);
}
private void Back()
{
mainMenu.SetActive(true);
optionsMenu.SetActive(false);
}
private void Quit()
{
// quits game when built
Application.Quit();
// quits playmode in editor
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
#endregion
}
}
Because I have to click away to see if the OnClick() function was added correctly for example, I can't tell if it updates correctly within the inspector but not visually in the scene mode or it just updates after clicking away.