Skip to content

Instantly share code, notes, and snippets.

@MisterKidX
Last active August 2, 2024 18:47
Show Gist options
  • Save MisterKidX/cce1553fcbfd66835bebb8f3ed38ecb9 to your computer and use it in GitHub Desktop.
Save MisterKidX/cce1553fcbfd66835bebb8f3ed38ecb9 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
#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)
{
if (property.propertyType != SerializedPropertyType.ObjectReference)
{
EditorGUI.LabelField(position, label.text, "Use \"PrefabOnly\" on gameobjects and monobehaviours only.");
return;
}
var go = property.objectReferenceValue as GameObject;
if (go != null && go.scene.IsValid())
property.objectReferenceValue = null;
var comp = property.objectReferenceValue as Component;
if (comp != null && comp.gameObject.scene.IsValid())
property.objectReferenceValue = null;
var obj = EditorGUI.ObjectField(position, label.text + " (Prafab)", property.objectReferenceValue, typeof(GameObject), false);
property.objectReferenceValue = obj;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment