Last active
August 21, 2024 10:41
-
-
Save tomkail/1cb4e700e328675f2c54ea56e75b2cd0 to your computer and use it in GitHub Desktop.
Draws inspectors for any file type Unity doesn't draw by default
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Used to draw custom inspectors for unrecognised file types, which Unity imports as "DefaultAsset" | |
/// To do this, create a new editor class extending DefaultAssetInspector | |
/// Return true in the IsValid function if the file extension of the file matches the type you'd like to draw. | |
/// The DefaultAssetEditor class will then hold a reference to the new instance of your editor class and call the appropriate methods for drawing. | |
/// An example can be found at the bottom of the file. | |
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.IO; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Reflection; | |
/// <summary> | |
/// Used to draw custom inspectors for unrecognised file types, which Unity imports as "DefaultAsset" | |
/// </summary> | |
[CustomEditor(typeof(DefaultAsset), true)] | |
public class DefaultAssetEditor : Editor { | |
private DefaultAssetInspector inspector; | |
private void OnEnable () { | |
inspector = FindObjectInspector (); | |
if(inspector != null) { | |
inspector.editor = this; | |
inspector.serializedObject = serializedObject; | |
inspector.target = target; | |
inspector.OnEnable(); | |
} | |
} | |
private void OnDisable () { | |
if(inspector != null) | |
inspector.OnDisable(); | |
} | |
protected override void OnHeaderGUI () { | |
if(inspector != null) { | |
inspector.OnHeaderGUI(); | |
} | |
else if (target.GetType() != typeof(UnityEditor.DefaultAsset)) | |
base.OnHeaderGUI(); | |
} | |
public void DrawDefaultHeaderGUI () { | |
base.OnHeaderGUI(); | |
} | |
public override void OnInspectorGUI () { | |
if(inspector != null) { | |
GUI.enabled = true; | |
inspector.OnInspectorGUI(); | |
} | |
else if (target.GetType() != typeof(UnityEditor.DefaultAsset)) | |
base.OnInspectorGUI(); | |
} | |
private DefaultAssetInspector FindObjectInspector () { | |
List<string> assembliesToCheck = new List<string>{"Assembly-CSharp-Editor", "Assembly-CSharp-Editor-firstpass", "Assembly-UnityScript-Editor", "Assembly-UnityScript-Editor-firstpass"}; | |
string assetPath = AssetDatabase.GetAssetPath(target); | |
Assembly[] referencedAssemblies = System.AppDomain.CurrentDomain.GetAssemblies(); | |
for(int i = 0; i < referencedAssemblies.Length; ++i) { | |
if(!assembliesToCheck.Contains(referencedAssemblies[i].GetName().Name)) | |
continue; | |
foreach(var type in referencedAssemblies[i].GetTypes()) { | |
if(!type.IsSubclassOf(typeof(DefaultAssetInspector))) | |
continue; | |
DefaultAssetInspector objectInspector = (DefaultAssetInspector)Activator.CreateInstance(type); | |
if(objectInspector.IsValid(assetPath)) { | |
objectInspector.target = target; | |
return objectInspector; | |
} | |
} | |
} | |
return null; | |
} | |
} | |
/// <summary> | |
/// Default asset inspector. Used by DefaultAssetEditor | |
/// </summary> | |
public abstract class DefaultAssetInspector { | |
// Reference to the actual editor we draw to | |
public DefaultAssetEditor editor; | |
// Shortcut to the target object | |
public UnityEngine.Object target; | |
// Shortcut to the serializedObject | |
public SerializedObject serializedObject; | |
public abstract bool IsValid(string assetPath); | |
public virtual void OnEnable () {} | |
public virtual void OnDisable () {} | |
// An example of how Unity draws headers can be found at https://github.com/MattRix/UnityDecompiled/blob/master/UnityEditor/UnityEditor/Editor.cs | |
public virtual void OnHeaderGUI () { | |
editor.DrawDefaultHeaderGUI(); | |
} | |
public virtual void OnInspectorGUI() {} | |
} | |
// EXAMPLE FOR A .SAVE FILE | |
// public class SaveFileInspector : DefaultAssetInspector { | |
// public override bool IsValid(string assetPath) { | |
// return Path.GetExtension(assetPath) == ".save"; | |
// } | |
// public override void OnInspectorGUI () { | |
// // Call to redraw every frame | |
// editor.Repaint(); | |
// serializedObject.Update(); | |
// // Use System.IO.File to read the file contents and draw it here if you wish! | |
// var assetPath = AssetDatabase.GetAssetPath(editor.target); | |
// var absolutePath = System.IO.Path.Combine(Application.dataPath.Substring(0, Application.dataPath.Length-7), assetPath); | |
// var text = System.IO.File.ReadAllText(absolutePath); | |
// EditorGUILayout.TextArea(text); | |
// serializedObject.ApplyModifiedProperties(); | |
// } | |
// } |
Yeah that’s right - I don’t think this code is what you're looking for. I
actually don’t know how to override the behaviour of the inspector window!
Perhaps extend it and build your own inspector window code? Sounds awkward
though.
…On Wed, 21 Aug 2024 at 11:40, MewPipe ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Ok, I tried. In fact what I need is that :
public override bool IsValid(string assetPath) {
return string.IsNullOrEmpty(assetPath); // Nothing selected
}
But your code only execute when something is selected in project window.
My condition is always "false".
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/tomkail/1cb4e700e328675f2c54ea56e75b2cd0#gistcomment-5162122>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJR3UDXBZ53DQ5IKRJ6OX3ZSRVBJBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4DEOBQHA4DONVHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, I tried. In fact what I need is that :
But your code only execute when something is selected in project window.
My condition is always "false".