Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Calling a function from a custom editor button and saving the changes.

Discussion in 'Immediate Mode GUI (IMGUI)' started by CBuckington, May 20, 2023.

  1. CBuckington


    Sep 28, 2020
    Hey everyone,

    I have been getting into editor scripting while creating my own custom versions of generic UI elements to avoid dropping yet another project due to frustration regarding UI stuff. I have hit a pretty major roadblock while trying to create a button that dynamically sets the pixel per unit multiplier on the image component of the visuals of my button. This is necessary as I want to reuse the same button prefab but have it look consistent in its pixelarted look among many sizes. (I have attached a picture to demonstrate what I want to achieve)

    My usual approach with editor scripts is to create a SerializedProperty for each variable that I want to expose and use serializedObject.FindProperty("_myVariableName") after setting the serializedObject.targetObject as the name of my script.

    As I want to change the pixelsPerUnitMultiplier on the image component of the gameObject in my scene by simply pressing a custom button in its inspector I first created a function in the UIButton class that would handle the functionality.

    Code (CSharp):
    1.     public void AutoSetPixelPerUnitMultiplier()
    2.     {
    3.         RectTransform rt = GetComponent<RectTransform>();
    4.         float height = rt.sizeDelta.y;
    5.         float ppuMult = 15/height;
    6.         _image.pixelsPerUnitMultiplier = ppuMult;
    7.     }

    As I found no other way to call a function in an editor class but to directly use the target I tried the following

    Code (CSharp):
    1.         UIButton _target = (UIButton)target;
    2.         Undo.RecordObject(_target, "ppu change");
    3.         if (GUILayout.Button("Auto set pixel per unit multiplier"))
    4.         {
    5.             _target.AutoSetPixelPerUnitMultiplier();
    6.         }

    At first this works fine but after pressing play the changes get reset to their previous values.
    I tried using EditorWindow.SaveChanges() but the values still got reset. As I am just working on the specific gameObject in the scene and not its prefab I also ruled out PrefabUtility.RecordPrefabInstancePropertyModifications().

    My only idea was to use a variable in the main script and serializedProperty in the editor script called pixelsPerUnitMultiplier and change its value in the editor script. Then I could do something like this in the UIButton script.

    Code (CSharp):
    1.     #if UNITY_EDITOR
    2.     private void Update()
    3.     {
    5.            _image.pixelsPerUnitMultiplier = _pixelsPerUnitMultiplier;
    6.     }
    7.     #endif

    But I feel like this approach is just begging to throw some major errors later on. Can anybody help me with getting my objects to keep their changed values after the button press? Thanks in advance!

    Attached Files:

    Last edited: May 22, 2023