public class Test : MonoBehaviour
{
[SearchContext("test:")] // works
public MonoBehaviour test1;
[MySearchContext] //doesn't work
public MonoBehaviour test2;
}
public class MySearchContextAttribute : SearchContextAttribute
{
public MySearchContextAttribute() : base("test:")
{
}
}
When reusing both the query and the search provider together, it may be necessary to define a custom attribute that inherits from SearchContextAttribute.
Ok this is a good one. I will log this bug officially (UUM-91984).
Thanks for reporting.
Hi @santutu2,
WE have taken the decision to make the class SearchContextAttribute sealed. It was never meant to be derived from in the first place. I will close the bug.
Thanks for understanding,
Sebastien
Hello @sebastienp_unity, thank you for your support and response.
I have a question.
The reason I wanted to use inheritance was to implement the code below.
The following is an attribute that searches for MonoBehaviours implementing a specific interface.
using System;
using System.Reflection;
using UnityEngine.Search;
using UnityEngine;
public class Test : MonoBehaviour
{
[SearchInterface(typeof(ITest))]
public MonoBehaviour aa;
}
[AttributeUsage(AttributeTargets.Field)]
public class SearchInterfaceAttribute : SearchContextAttribute
{
public SearchInterfaceAttribute(Type interfaceType) : base(
$"ic:{new AssemblyName(interfaceType.Assembly.FullName).Name},{interfaceType.FullName}",
typeof(InterfaceComponentSearchProvider)
)
{ }
}
If it is not possible to inherit from SearchContextAttribute, is there another way to achieve this kind of implementation?
Hi @santutu2,
after reviewing your feature request, we have decided to keep the [SearchContextAttribute] sealed. Our rationale are as follow:
- Deriving from an attribute might have unforeseen consequences at the call site where we gather all attributes
- PropertyDrawer used to draw the SearchContextAttribute might also have issues
Sorry mate
But keep up giving us feedback,
Sebastien
This is dissapointing.
Is there not a way to only allow child classes that only provide new parameters for the base [SearchContextAttribute] constructors? This wouldn’t allow you to do anything that you can’t already do, but saves you from having to remember and type complicated search queries.
santutu2’s problem above is a good example. We don’t actually need to change or add behaviour in child attributes. Just want to make shortcuts so the attribute is useful.