AssetPreAssignAttributeDrawer.cs 833 B

12345678910111213141516171819202122
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. [CustomPropertyDrawer(typeof(AssetPreAssignAttribute))]
  7. public class AssetPreAssignAttributeDrawer : PropertyDrawer
  8. {
  9. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  10. {
  11. AssetPreAssignAttribute assetPreAssignAttribute = attribute as AssetPreAssignAttribute;
  12. string assetPath = assetPreAssignAttribute.assetPath;
  13. Type assetType = assetPreAssignAttribute.assetType;
  14. if (property.objectReferenceValue == null)
  15. {
  16. UnityEngine.Object asset = AssetDatabase.LoadAssetAtPath(assetPath, assetType);
  17. property.objectReferenceValue = asset;
  18. }
  19. EditorGUI.PropertyField(position, property, label);
  20. }
  21. }