Skip to content

Commit

Permalink
Merge pull request #129 from Algoryx/fix/inspector-editor-hideflags
Browse files Browse the repository at this point in the history
Added a workaround for Unity not handling setting hideFlags in custom inspector
  • Loading branch information
FilipAlg authored Oct 3, 2023
2 parents 79fb95b + b865f97 commit 22f570e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
36 changes: 31 additions & 5 deletions Editor/AGXUnityEditor/AssetPostprocessorHandler.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
using System.Collections.Generic;
using AGXUnity;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Reflection;
using UnityEditor;
using AGXUnity;
using UnityEngine;

namespace AGXUnityEditor
{
public class AssetPostprocessorHandler : AssetPostprocessor
{
static AssetPostprocessorHandler()
{
// Cache types which have the HideInInspector attribute for use in OnAGXPrefabAdddedToScene
TypesHiddenInInspector = new List<System.Type>();
foreach ( var a in System.AppDomain.CurrentDomain.GetAssemblies() )
if ( !a.GetName().Name.StartsWith( "Unity" ) && !a.GetName().Name.StartsWith( "System" ) )
foreach ( var t in a.DefinedTypes )
if ( t.IsSubclassOf( typeof( MonoBehaviour ) ) && t.GetCustomAttribute<HideInInspector>() != null )
TypesHiddenInInspector.Add( t );
}

static List<System.Type> TypesHiddenInInspector = new List<System.Type>();

private class CollisionGroupEntryEqualityComparer : IEqualityComparer<CollisionGroupEntry>
{
public bool Equals( CollisionGroupEntry cg1, CollisionGroupEntry cg2 )
Expand Down Expand Up @@ -108,10 +122,10 @@ public static void OnPrefabAddedToScene( GameObject instance )

var fileInfo = new IO.AGXFileInfo( instance );
if ( fileInfo.IsValid && fileInfo.Type == IO.AGXFileInfo.FileType.AGXPrefab )
OnAGXPrefabAdddedToScene( instance, fileInfo );
OnAGXPrefabAddedToScene( instance, fileInfo );
}

private static void OnAGXPrefabAdddedToScene( GameObject instance, IO.AGXFileInfo fileInfo )
private static void OnAGXPrefabAddedToScene( GameObject instance, IO.AGXFileInfo fileInfo )
{
if ( fileInfo.ExistingPrefab == null ) {
Debug.LogWarning( "Unable to load parent prefab from file: " + fileInfo.NameWithExtension );
Expand All @@ -121,6 +135,18 @@ private static void OnAGXPrefabAdddedToScene( GameObject instance, IO.AGXFileInf
Undo.SetCurrentGroupName( "Adding: " + instance.name + " to scene." );
var grouId = Undo.GetCurrentGroup();

// As of Unity 2022.1 the inspector breaks when setting hideflags while rendering a custom inspector
// To circumvent this the hideflags are also set in the Reset method of the affected classes.
// This however is not sufficient when importing .agx files as the agx file importer saves a prefab
// which removes hideflags set on the object. (See https://forum.unity.com/threads/is-it-impossible-to-save-component-hideflags-in-a-prefab.976974/)
// As an additional workaround we set the hideflags on the affected componentes when adding a prefab
// to the scene instead.
foreach ( var t in TypesHiddenInInspector ) {
var components = instance.GetComponentsInChildren(t);
foreach ( var comp in components )
comp.hideFlags |= HideFlags.HideInInspector;
}

var contactMaterialManager = TopMenu.GetOrCreateUniqueGameObject<ContactMaterialManager>();
Undo.RecordObject( contactMaterialManager, "Adding contact materials" );
foreach ( var cm in IO.Utils.FindAssetsOfType<ContactMaterial>( fileInfo.DataDirectory ) )
Expand Down
4 changes: 4 additions & 0 deletions Editor/AGXUnityEditor/InspectorEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ private void OnEnable()
// should be visible are enabled again but never rendered by Unity.
// SOLUTION: Add hideFlags |= HideFlags.HideInInspector in Reset method of the class
// that shouldn't be rendered in the Inspector.
// NOTE 2: The above solution does not work when importing prefabs as the act of
// saving a GameObject to a prefab clears the hideFlags.
// SOLUTION: Set the hideFlags on affected components when adding a prefab to the scene
// in AGXUnityEditor.AssetPostprocessorHandler.OnAGXPrefabAdddedToScene
if ( this.targets.Any( t => !t.hideFlags.HasFlag( HideFlags.HideInInspector ) ) && m_targetType.GetCustomAttribute<HideInInspector>( false ) != null ) {
foreach ( var t in this.targets )
t.hideFlags |= HideFlags.HideInInspector;
Expand Down

0 comments on commit 22f570e

Please sign in to comment.