Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug [SRP, b10] Material Inspector does not respond to clicks after saving project

Discussion in 'Unity 6 Beta' started by Neonage, Mar 11, 2024.

  1. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    288
    Happens only when:
    - URP or HDRP is used as active pipeline. No issue with BIRP.
    - Material is selected from assets in isolation, not on game-object.
    Doesn't happen in Properties window. Custom ShaderGUI doesn't affect this.

    How to reproduce:
    1) Select any Material in Assets
    2) Change any property
    3) Save project
    4) Inspector stops responding to clicks
    5) ??? Now you have to re-select material

    As I constantly press Ctrl + S, you may imagine how unbearable it becomes.

    Some other very strange behavior:
    1) If you undo changes and save again, it starts working again (and ofc breaks on next save).
    2) If you do all these steps when changing material on game-object, and then select this material in assets - the issue will be gone only for that particular material. (o_Oo_Oo_O???) Issue will be back after Editor restart.
     
    Last edited: Mar 11, 2024
  2. NOT_Lonely

    NOT_Lonely

    Joined:
    Feb 2, 2013
    Posts:
    551
    I have the same problem on Unity 2022.3.19f1 URP.
    Have you solved this?
     
  3. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    288
    Try this:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6. using System.Reflection;
    7.  
    8. public class MaterialInspectorSaveFix : AssetModificationProcessor // is there a better callback method for OnSave?
    9. {
    10.     struct InspectorWindowData
    11.     {
    12.         public EditorWindow window;
    13.         public Dictionary<int, float> editorsHeight;
    14.  
    15.         static MethodInfo OnSelectionChangedMethod;
    16.      
    17.         public VisualElement QueryEditorsList()
    18.         {
    19.             return window.rootVisualElement.Query(className: "unity-inspector-editors-list").First();
    20.         }
    21.  
    22.         public void InvokeOnSelectionChanged()
    23.         {
    24.             if (OnSelectionChangedMethod == null)
    25.                 OnSelectionChangedMethod = window.GetType().GetMethod("OnSelectionChanged", BindingFlags.NonPublic | BindingFlags.Instance);
    26.             OnSelectionChangedMethod.Invoke(window, null);
    27.         }
    28.     }
    29.  
    30.     static MethodInfo LayoutSetMethod;
    31.  
    32.     static void SetLayoutProperty(VisualElement element, Rect rect)
    33.     {
    34.         if (LayoutSetMethod == null)
    35.             LayoutSetMethod = typeof(VisualElement).GetProperty("layout", BindingFlags.Public | BindingFlags.Instance).GetSetMethod(true);
    36.  
    37.         LayoutSetMethod.Invoke(element, new object[] { rect });
    38.     }
    39.    
    40.     static string[] OnWillSaveAssets (string[] paths)
    41.     {
    42.         var selection = Selection.objects;
    43.  
    44.         bool hasAnyMaterialSelected = false;
    45.         foreach (var obj in selection)
    46.         {
    47.             if (obj is Material)
    48.             {
    49.                 hasAnyMaterialSelected = true;
    50.                 break;
    51.             }
    52.         }
    53.         if (!hasAnyMaterialSelected)
    54.             return paths;
    55.        
    56.         var allWindows = Resources.FindObjectsOfTypeAll(typeof(EditorWindow));
    57.         var inspectorWindows = new List<InspectorWindowData>(1);
    58.         foreach (EditorWindow window in allWindows)
    59.         {
    60.             var type = window.GetType();
    61.             if (type.FullName == "UnityEditor.InspectorWindow")
    62.             {
    63.                 var data = new InspectorWindowData() { window = window, editorsHeight = new Dictionary<int, float>() };
    64.  
    65.                 foreach (var editor in data.QueryEditorsList().Children())
    66.                 {
    67.                     data.editorsHeight.Add(editor.GetHashCode(), editor.layout.height);
    68.                 }
    69.  
    70.                 inspectorWindows.Add(data);
    71.             }
    72.         }
    73.        
    74.         // We DO NOT want to mess with Selection, as it'll register garbage Undo.
    75.         //Selection.objects = null;
    76.  
    77.         //foreach (var inspector in inspectorWindows)
    78.         //    inspector.InvokeOnSelectionChanged();
    79.  
    80.         //Selection.objects = selection;
    81.  
    82.         foreach (var inspector in inspectorWindows)
    83.         {
    84.             inspector.InvokeOnSelectionChanged();
    85.  
    86.             foreach (var editor in inspector.QueryEditorsList().Children())
    87.             {
    88.                 var layout = editor.layout;
    89.                 if (inspector.editorsHeight.TryGetValue(editor.GetHashCode(), out var height))
    90.                 {
    91.                     // dunno where EditorElement sets the layout, so we'll do it manually
    92.                     layout.height = height;
    93.                     SetLayoutProperty(editor, layout);
    94.                 }
    95.             }
    96.         }
    97.        
    98.         return paths;
    99.     }
    100. }
    101. #endif
    Let me know if it works on your version or if there any issues with it!

    While writing this fix, I've found the cause of this issue.
    If you look in UITK Debugger, the Material EditorElement is not re-initialized properly - height is 65, thus you can only click on it's header. It's not the case with Properties window, only the Inspector.
    Before save:
    upload_2024-3-27_17-7-31.png
    After save:
    upload_2024-3-27_17-7-36.png

    There's also another small issue - preview pane (at the bottom) doesn't remember on save that it was completely hidden, only when height is above zero, so it keeps resetting itself when trying to hide it. :confused:
     

    Attached Files:

  4. NOT_Lonely

    NOT_Lonely

    Joined:
    Feb 2, 2013
    Posts:
    551
    Wow, thanks, I will try it! I found some bug reports related to this issue, but it looks like Unity doesn't even touched it.
     
  5. icauroboros

    icauroboros

    Joined:
    Apr 30, 2021
    Posts:
    177
    Did you submit a bug report ?, I couldn't find any
     
  6. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    288
    icauroboros likes this.