Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nick123451 · Sep 27, 2021 at 02:32 PM · editoreditor-scriptingeditor scripting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

177 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hint at Inspector variables that have tooltips 0 Answers

Ensure Child Components Present on GameObject in Editor 1 Answer

How to change a selection effect in editor 0 Answers

How to modify child class's enum depending on value of parent enum in custom editor,Custom editor to modify nested classes enum options 1 Answer

Play Unity Editor from VSCode? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges