Search Unity

Custom editor window for editing prefabs

Discussion in 'Prefabs' started by metinevren, Mar 6, 2019.

  1. metinevren

    metinevren

    Joined:
    May 17, 2014
    Posts:
    30
    The changes I make through my custom window are not saved to the prefab. And if I make a change to the prefab through inspector window, the prefab in my editor window gets lost. If I drag the prefab into the scene and make changes through my editor window, it changes the object in the scene but not the original prefab. I am very confused with the new system. Why doesn't my editor window apply changes like it used to? How do I edit my prefabs in the new system through my custom editor window?
     
    jrumps likes this.
  2. metinevren

    metinevren

    Joined:
    May 17, 2014
    Posts:
    30
    A simplified version of my code:
    I drag the prefab from project window to the objectfield.

    Code (CSharp):
    1. public class WavemachineEditorWindow : EditorWindow
    2. {
    3.     private Wavemachine wavemachine;
    4.  
    5.     void OnGUI()
    6.     {
    7.         wavemachine = (Wavemachine)EditorGUILayout.ObjectField(wavemachine, typeof(Wavemachine), false, GUILayout.Width(200));
    8.  
    9.         wavemachine.someValue = EditorGUILayout.IntField("Value:", wavemachine.someValue);
    10.  
    11.         if (GUI.changed)
    12.         {
    13.             EditorUtility.SetDirty(wavemachine);
    14.         }
    15.     }
    16. }
     
  3. metinevren

    metinevren

    Joined:
    May 17, 2014
    Posts:
    30
    Okay I kinda figured out the PropertyField thing. This line seems to be working BUT it feels VERY IMPRACTICAL.

    inside OnGUI():
    Code (CSharp):
    1.  
    2. myWM = new UnityEditor.SerializedObject(wavemachine);
    3. myWM.Update();
    4. myWM.FindProperty("waves.Array.data[0].wavethreads.Array.data[0].subwaves.Array.data[0].amount").intValue = EditorGUILayout.IntField("amount:", myWM.FindProperty("waves.Array.data[0].wavethreads.Array.data[0].subwaves.Array.data[0].amount").intValue);
    5. myWM.ApplyModifiedProperties();
    6.  
    Am I doing this the right way?

    I have many variables that I edit with my custom editor window, do I have to type them all in this way? In the manual it says shift+right clicking gives the property's path. Is it possible get it via code so I can maybe write my own method that takes the variable as parameter, and generates the above code for it.
     
  4. Nith666

    Nith666

    Joined:
    Aug 1, 2012
    Posts:
    57