Skip to content

Commit

Permalink
Minor refactor changes; implement NotPrefabObjectOnlyAttribute & Drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
arimger committed Jun 13, 2024
1 parent 27fd731 commit 3b58224
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,13 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
}
}


public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.ObjectReference;
}


private NotNullAttribute Attribute => attribute as NotNullAttribute;


private static class Style
{
#if UNITY_2019_3_OR_NEWER
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Drawers
{
[CustomPropertyDrawer(typeof(NotPrefabObjectOnlyAttribute))]
public class NotPrefabObjectOnlyAttributeDrawer : ObjectValidationDrawer
{
protected override string GetWarningMessage()
{
return "Assigned object can't be a Prefab.";
}

protected override bool IsObjectValid(Object objectValue, SerializedProperty property)
{
if (objectValue == null)
{
return false;
}

var attribute = Attribute;
if (PrefabUtility.GetPrefabAssetType(objectValue) == PrefabAssetType.NotAPrefab)
{
return true;
}

if (PrefabUtility.GetPrefabInstanceStatus(objectValue) == PrefabInstanceStatus.Connected &&
attribute.AllowInstancedPrefabs)
{
return true;
}

return false;
}

private NotPrefabObjectOnlyAttribute Attribute => attribute as NotPrefabObjectOnlyAttribute;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected virtual string GetWarningMessage()

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


public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.ObjectReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ private void HandleTargetPicker(Rect position, SerializedProperty property)
}
}


protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
{
return SceneExists(property.stringValue)
Expand All @@ -88,13 +87,11 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
HandleTargetPicker(position, property);
}


public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.String;
}


private static class Style
{
internal static readonly float rowHeight = EditorGUIUtility.singleLineHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ private GameObject GetGameObject(Object reference)
return null;
}


protected override string GetWarningMessage()
{
return "Assigned object has to be instantiated in the Scene.";
Expand All @@ -28,7 +27,7 @@ protected override string GetWarningMessage()
protected override bool IsObjectValid(Object objectValue, SerializedProperty property)
{
var gameObject = GetGameObject(objectValue);
return gameObject && !string.IsNullOrEmpty(gameObject.scene.name);
return gameObject != null && !string.IsNullOrEmpty(gameObject.scene.name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
EditorGUI.EndProperty();
}


public override bool IsPropertyValid(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.Enum;
Expand Down
4 changes: 4 additions & 0 deletions Assets/Editor Toolbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ Supported types: **GameObject, Component**.

Supported types: **GameObject, Component**.

#### NotPrefabObjectOnlyAttribute

Supported types: **GameObject, Component**.

#### LeftToggleAttribute

Supported types: **bool**.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Diagnostics;

namespace UnityEngine
{
/// <summary>
/// Validates input values and accepts only objects that are not Prefabs.
///
/// <para>Supported types: <see cref="GameObject"/> and any <see cref="Component"/>.</para>
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
[Conditional("UNITY_EDITOR")]
public class NotPrefabObjectOnlyAttribute : PropertyAttribute
{
public bool AllowInstancedPrefabs { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Examples/Scripts/SampleBehaviour1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public enum FlagExample
public GameObject childReference;
[PrefabObjectOnly]
public GameObject prefabReference;
[NotPrefabObjectOnly(AllowInstancedPrefabs = false)]
public GameObject notPrefabReference;

[Label("Formatted Number", skinStyle: SkinStyle.Box)]

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ Supported types: **GameObject, Component**.

Supported types: **GameObject, Component**.

#### NotPrefabObjectOnlyAttribute

Supported types: **GameObject, Component**.

#### LeftToggleAttribute

Supported types: **bool**.
Expand Down

0 comments on commit 3b58224

Please sign in to comment.