Created
January 31, 2026 23:04
-
-
Save Fizzyhex/f5c8f406e9652667063d70b369c6a0c2 to your computer and use it in GitHub Desktop.
Unity Editor Object Picker
This file contains hidden or 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
| // i struggled to figure this out, so i'm making this gist as a future reference. | |
| using System; | |
| using UnityEditor; | |
| using UnityEngine; | |
| namespace VoxelLab.Editor | |
| { | |
| [CustomPropertyDrawer(typeof(DirectoryLink))] | |
| public class DirectoryLinkEditor : PropertyDrawer | |
| { | |
| private bool _isShown; | |
| public int _controlId; | |
| public override void OnGUI(Rect position, SerializedProperty property, | |
| GUIContent label) | |
| { | |
| var evt = Event.current; | |
| if (evt.type == EventType.ExecuteCommand) | |
| { | |
| if (evt.commandName == "ObjectSelectorUpdated" && | |
| EditorGUIUtility.GetObjectPickerControlID() == _controlId) | |
| { | |
| property.objectReferenceValue = EditorGUIUtility.GetObjectPickerObject(); | |
| } | |
| } | |
| EditorGUI.LabelField(position, label); | |
| _isShown = EditorGUI.Foldout(new Rect(position.position + position.size / 2, position.size / 2), _isShown, "ho"); | |
| if (_isShown) | |
| { | |
| _isShown = false; | |
| _controlId += 1; | |
| EditorGUIUtility.ShowObjectPicker<DirectoryLink>(property.objectReferenceValue, false, "", _controlId); | |
| } | |
| } | |
| public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
| { | |
| return _isShown ? base.GetPropertyHeight(property, label) + 24 : base.GetPropertyHeight(property, label); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment