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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with EditorScripts after Runtime SOLVED

Discussion in 'Scripting' started by Hapciupalit, Dec 18, 2017.

  1. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    So I've been struggling for a few hours now, with a strange thing, so let me explain you the problem.
    I have two scripts. One script have one method that I want to call it in the edit mode, so I built the EditorScript and added the Run button which calls the function. In edit mode everything works fine, but when I run the game, some of the modifications that the script did changed back.

    Here is a photo with my problem
    In the first picture I have the GameObject before running the script
    In the second picture is what I get after running the script
    In the third is what I get after playing the game.


    Here is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using PixelCrushers.DialogueSystem;
    6.  
    7. public class ActionButton : MonoBehaviour {
    8.  
    9.     public Sprite buttonSprite;
    10.  
    11.     private Sprite emptySprite;
    12.  
    13.  
    14.     public void Run(){
    15.  
    16.         emptySprite = Resources.Load<Sprite>("Empty");
    17.  
    18.         string tName = buttonSprite.name;
    19.  
    20.         string sufix = tName.Remove(0,tName.IndexOf("_!_")+3);
    21.  
    22.         int sufixSepIndex = sufix.LastIndexOf('_');
    23.  
    24.         int widthStart = sufix.IndexOf('w')+1;
    25.         int widthEnd = sufix.IndexOf('h',widthStart);
    26.         float width = float.Parse(sufix.Substring(widthStart, widthEnd-widthStart));
    27.  
    28.         print(width);
    29.  
    30.         int heightStart = sufix.IndexOf('h')+1;
    31.         int heightEnd = sufix.IndexOf('_',heightStart);
    32.         float height = float.Parse(sufix.Substring(heightStart,heightEnd-heightStart));
    33.  
    34.         print(height);
    35.  
    36.         int xStart = sufix.IndexOf('x')+1;
    37.         int xEnd = sufix.IndexOf('y',xStart);
    38.         float x = float.Parse(sufix.Substring(xStart,xEnd - xStart));
    39.  
    40.         int yStart = sufix.IndexOf('y')+1;
    41.         float y = float.Parse(sufix.Substring(yStart));
    42.  
    43.         print(string.Concat("width :",width,"___ height :",height, "\nx :", x, "___ y :", y));
    44.  
    45.         //position
    46.  
    47.         RectTransform rt = gameObject.GetComponent<RectTransform>();
    48.         rt.anchoredPosition = new Vector3(x,-y,0);
    49.         rt.anchorMin = new Vector2(0, 1);
    50.         rt.anchorMax = new Vector2(0, 1);
    51.         rt.sizeDelta = new Vector2(width,height);
    52.  
    53.         //change image
    54.         gameObject.GetComponent<Image>().sprite = emptySprite;
    55.  
    56.         //set button
    57.         Button btn = gameObject.GetComponent<Button>();
    58.         btn.transition = Selectable.Transition.SpriteSwap;
    59.  
    60.         SpriteState ss = new SpriteState();
    61.         ss.highlightedSprite = buttonSprite;
    62.         ss.pressedSprite = buttonSprite;
    63.  
    64.         btn.spriteState = ss;
    65.  
    66.         //change name
    67.         string conversationName = gameObject.GetComponent<ConversationTrigger>().conversation;
    68.         gameObject.name = conversationName;
    69.     }
    70.  
    71. }
    72.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(ActionButton))]
    7. public class ActionButtonEditor : Editor {
    8.  
    9.     public override void OnInspectorGUI(){
    10.      
    11.         DrawDefaultInspector();
    12.  
    13.         ActionButton actionButton = (ActionButton)target;
    14.  
    15.         GUILayout.Space(10);
    16.  
    17.         if(GUILayout.Button("Run")){
    18.             actionButton.Run();
    19.         }
    20.     }
    21.  
    22. }
    23.  
    Thank you!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Code (csharp):
    1. if(GUILayout.Button("Run")){
    2.     Undo.RecordObject(actionButton);
    3.     actionButton.Run();
    4. }
    Unity doesn't automatically understand that objects have been changed, and need to be saved to disk. One of the easiest ways to tell Unity to do that is to use Undo.RecordObject. As an added bonus, you now support Undo!
     
    Hapciupalit likes this.
  3. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    I made those changes, but the problem persists.

    Code (CSharp):
    1. if(GUILayout.Button("Run")){
    2.             Undo.RecordObject(actionButton,"Change the button attributes");
    3.             actionButton.Run();
    4.         }
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Ah, right - you need to make sure that everything that you're editing is getting marked as dirty. Didn't read your code properly - you're editing some other components than the button itself.

    I believe that calling Undo.RecordObject on the button's gameObject should do the trick, as all the other components seem to be attached to that as well.
     
  5. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    Thats not going to work cause as soon as you return from Play-mode changes to scene-specific data are lost. you need to apply the changes to an asset (or serialize the changes externally, either to file or server) if you want them saved at run-time.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    OP writes that "In edit mode everything works fine, but when I run the game, some of the modifications that the script did changed back", so I don't think they're trying to have Play-mode changes carry over to edit mode.
     
  7. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    And to get the Button GameObject in the Editor script, I should create another field, in my ActionButton Script where I drag&drop the gameObject that holds the button and then it will be something like
    Code (CSharp):
    1. Undo.RecordObject(myGameObject);
    ?
    Or there is any smarter way that I don't know
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    It's the gameObject of the button. it's actionButton.gameObject.
     
  9. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Still the same problem...
     
  10. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103
    Anyone have any ideas?
     
  11. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Try recording all of the objects that you're changing:

    Code (csharp):
    1.     public override void OnInspectorGUI(){
    2.    
    3.         DrawDefaultInspector();
    4.         ActionButton actionButton = (ActionButton)target;
    5.         GUILayout.Space(10);
    6.         if(GUILayout.Button("Run")){
    7.  
    8.             Undo.RecordObjects(new object[] {
    9.                 actionButton.gameObject, ,
    10.                 actionButton.GetComponent<Image>(),
    11.                 actionButton.GetComponent<Button>() },
    12.                 "ActionButton.Run");
    13.  
    14.             actionButton.Run();
    15.         }
    16.     }
     
  12. Hapciupalit

    Hapciupalit

    Joined:
    Apr 24, 2015
    Posts:
    103

    Thank you! This solution worked.
     
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,533
    Glad I could help!