Skip to content

Commit 3b58224

Browse files
committed
Minor refactor changes; implement NotPrefabObjectOnlyAttribute & Drawer
1 parent 27fd731 commit 3b58224

12 files changed

+88
-10
lines changed

Assets/Editor Toolbox/Editor/Drawers/Regular/NotNullAttributeDrawer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,13 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
4141
}
4242
}
4343

44-
4544
public override bool IsPropertyValid(SerializedProperty property)
4645
{
4746
return property.propertyType == SerializedPropertyType.ObjectReference;
4847
}
4948

50-
5149
private NotNullAttribute Attribute => attribute as NotNullAttribute;
5250

53-
5451
private static class Style
5552
{
5653
#if UNITY_2019_3_OR_NEWER
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Drawers
5+
{
6+
[CustomPropertyDrawer(typeof(NotPrefabObjectOnlyAttribute))]
7+
public class NotPrefabObjectOnlyAttributeDrawer : ObjectValidationDrawer
8+
{
9+
protected override string GetWarningMessage()
10+
{
11+
return "Assigned object can't be a Prefab.";
12+
}
13+
14+
protected override bool IsObjectValid(Object objectValue, SerializedProperty property)
15+
{
16+
if (objectValue == null)
17+
{
18+
return false;
19+
}
20+
21+
var attribute = Attribute;
22+
if (PrefabUtility.GetPrefabAssetType(objectValue) == PrefabAssetType.NotAPrefab)
23+
{
24+
return true;
25+
}
26+
27+
if (PrefabUtility.GetPrefabInstanceStatus(objectValue) == PrefabInstanceStatus.Connected &&
28+
attribute.AllowInstancedPrefabs)
29+
{
30+
return true;
31+
}
32+
33+
return false;
34+
}
35+
36+
private NotPrefabObjectOnlyAttribute Attribute => attribute as NotPrefabObjectOnlyAttribute;
37+
}
38+
}

Assets/Editor Toolbox/Editor/Drawers/Regular/NotPrefabObjectOnlyAttributeDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Editor/Drawers/Regular/ObjectValidationDrawer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected virtual string GetWarningMessage()
3434

3535
protected abstract bool IsObjectValid(Object objectValue, SerializedProperty property);
3636

37-
3837
public override bool IsPropertyValid(SerializedProperty property)
3938
{
4039
return property.propertyType == SerializedPropertyType.ObjectReference;

Assets/Editor Toolbox/Editor/Drawers/Regular/SceneNameAttributeDrawer.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ private void HandleTargetPicker(Rect position, SerializedProperty property)
6363
}
6464
}
6565

66-
6766
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
6867
{
6968
return SceneExists(property.stringValue)
@@ -88,13 +87,11 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
8887
HandleTargetPicker(position, property);
8988
}
9089

91-
9290
public override bool IsPropertyValid(SerializedProperty property)
9391
{
9492
return property.propertyType == SerializedPropertyType.String;
9593
}
9694

97-
9895
private static class Style
9996
{
10097
internal static readonly float rowHeight = EditorGUIUtility.singleLineHeight;

Assets/Editor Toolbox/Editor/Drawers/Regular/SceneObjectOnlyAttributeDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ private GameObject GetGameObject(Object reference)
1919
return null;
2020
}
2121

22-
2322
protected override string GetWarningMessage()
2423
{
2524
return "Assigned object has to be instantiated in the Scene.";
@@ -28,7 +27,7 @@ protected override string GetWarningMessage()
2827
protected override bool IsObjectValid(Object objectValue, SerializedProperty property)
2928
{
3029
var gameObject = GetGameObject(objectValue);
31-
return gameObject && !string.IsNullOrEmpty(gameObject.scene.name);
30+
return gameObject != null && !string.IsNullOrEmpty(gameObject.scene.name);
3231
}
3332
}
3433
}

Assets/Editor Toolbox/Editor/Drawers/Regular/SearchableEnumAttributeDrawer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
3434
EditorGUI.EndProperty();
3535
}
3636

37-
3837
public override bool IsPropertyValid(SerializedProperty property)
3938
{
4039
return property.propertyType == SerializedPropertyType.Enum;

Assets/Editor Toolbox/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ Supported types: **GameObject, Component**.
251251

252252
Supported types: **GameObject, Component**.
253253

254+
#### NotPrefabObjectOnlyAttribute
255+
256+
Supported types: **GameObject, Component**.
257+
254258
#### LeftToggleAttribute
255259

256260
Supported types: **bool**.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace UnityEngine
5+
{
6+
/// <summary>
7+
/// Validates input values and accepts only objects that are not Prefabs.
8+
///
9+
/// <para>Supported types: <see cref="GameObject"/> and any <see cref="Component"/>.</para>
10+
/// </summary>
11+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
12+
[Conditional("UNITY_EDITOR")]
13+
public class NotPrefabObjectOnlyAttribute : PropertyAttribute
14+
{
15+
public bool AllowInstancedPrefabs { get; set; }
16+
}
17+
}

Assets/Editor Toolbox/Runtime/Attributes/Property/Regular/NotPrefabObjectOnlyAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)