Last active
August 2, 2024 18:47
-
-
Save MisterKidX/cce1553fcbfd66835bebb8f3ed38ecb9 to your computer and use it in GitHub Desktop.
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
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