Skip to content

Instantly share code, notes, and snippets.

@PhantasmicDev
Created November 9, 2021 02:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhantasmicDev/4a91afa15c5c0786ec18b46566661bfa to your computer and use it in GitHub Desktop.
Save PhantasmicDev/4a91afa15c5c0786ec18b46566661bfa to your computer and use it in GitHub Desktop.
An attribute that allows referencing only prefabs in the editor
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
[AttributeUsage(AttributeTargets.Field)]
public class PrefabOnlyAttribute : PropertyAttribute
{
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(PrefabOnlyAttribute))]
public class PrefabOnlyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
property.objectReferenceValue = EditorGUI.ObjectField(position, label, property.objectReferenceValue, fieldInfo.FieldType, false);
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment