public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Draw the property gui. EditorGUILayout.PropertyField(property, label, true); // We only apply the default if there is no assigned asset. if (property.objectReferenceValue != null) { return; } // Get the default asset. DefaultAssetAttribute defaultAssetAttribute = (DefaultAssetAttribute)attribute; Object defaultAsset = (Object)defaultAssetAttribute.GetDefaultAsset(fieldInfo.FieldType); // Assign. property.objectReferenceValue = defaultAsset; }
static public void Apply(object inObject, UnityEngine.Object inHost = null) { if (inObject == null) { return; } Type objType = inObject.GetType(); bool bChanged = false; foreach (var field in objType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (!field.IsPublic && !field.IsDefined(typeof(SerializeField))) { continue; } DefaultAssetAttribute defaultAssetAttr = Reflect.GetAttribute <DefaultAssetAttribute>(field); if (defaultAssetAttr != null) { Type type = field.FieldType; string name = defaultAssetAttr.AssetName; UnityEngine.Object asset = (UnityEngine.Object)field.GetValue(inObject); if (asset == null) { asset = AssetDBUtils.FindAsset(type, name); if (asset != null) { field.SetValue(inObject, asset); bChanged = true; } } } } if (bChanged && inHost) { EditorUtility.SetDirty(inHost); } }