Add SceneObjectsOnly attriute

This commit is contained in:
VladV 2022-07-12 13:27:04 +03:00
parent a56ae84197
commit b21f687394
7 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,36 @@
using TriInspector;
using TriInspector.Validators;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriAttributeValidator(typeof(SceneObjectsOnlyValidator))]
namespace TriInspector.Validators
{
public class SceneObjectsOnlyValidator : TriAttributeValidator<SceneObjectsOnlyAttribute>
{
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
if (!typeof(Object).IsAssignableFrom(propertyDefinition.FieldType))
{
return "AssetsOnly attribute can be used only on Object fields";
}
return TriExtensionInitializationResult.Ok;
}
public override TriValidationResult Validate(TriProperty property)
{
var obj = property.TryGetSerializedProperty(out var serializedProperty)
? serializedProperty.objectReferenceValue
: (Object) property.Value;
if (obj == null || !AssetDatabase.Contains(obj))
{
return TriValidationResult.Valid;
}
return TriValidationResult.Error($"{obj} cannot be an asset.");
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 98948bb92a504f87abeda6e500dc22b2
timeCreated: 1657621306

View File

@ -0,0 +1,8 @@
using TriInspector;
using UnityEngine;
public class Validators_SceneObjectsOnlySample : ScriptableObject
{
[SceneObjectsOnly]
public GameObject obj;
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7bd709dcdda44927b14c8a77ffebf843
timeCreated: 1657621668

View File

@ -171,6 +171,15 @@ private bool VisibleInEditMode => !Application.isPlaying;
public GameObject obj;
```
#### SceneObjectsOnly
![SceneObjectsOnly](https://user-images.githubusercontent.com/26966368/178470605-618c9796-054f-40bb-9c09-2d9c6f342faf.png)
```csharp
[SceneObjectsOnly]
public GameObject obj;
```
### Decorators
#### Dropdown

View File

@ -0,0 +1,11 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage((AttributeTargets.Field | AttributeTargets.Property))]
[Conditional("UNITY_EDITOR")]
public sealed class SceneObjectsOnlyAttribute : Attribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 908b02bc0b5d4cdd88e952625ce0cbc7
timeCreated: 1657621281