A class attribute that allows you to define dynamic constraint on a MonoBehavior or ScriptableObject's field for the object selector.
This attribute identifies methods as custom object selector handlers. Handlers require editor-specific objects to work. If you want to constrain a UnityEngine script's field, you must define a custom attribute, decorate the field with it, and use this attribute in your editor code when you declare your custom handler.
Here is an example of how to setup your component:
// UnityEngine only script. using System; using UnityEngine; using UnityEngine.SearchService; using Object = UnityEngine.Object;
// Declare a custom attribute that you use to decorate your fields and declare your handlers. public class ObjectSelectorRedMaterialAttribute : Attribute {}
// This example demonstrates how to add a constraint on a field so // that an object selector only displays objects that satisfy the constraint. public class MyComponent : MonoBehaviour { // Declare a field with an object selector constraint on it. // This material only accepts materials that satisfy the conditions implemented by the handler written for ObjectSelectorRedMaterialAttribute. [ObjectSelectorRedMaterialAttribute] public Material redMat;
// Start is called before the first frame update. void Start() { }
// Update is called once per frame. void Update() { } }
This example demonstrates how to implement the Editor script for the handlers:
using System; using System.Linq; using UnityEditor; using UnityEditor.SearchService; using UnityEngine; using Object = UnityEngine.Object;
public class ExternalHandlers { // Declare the object selector constraint handler. // This function act as the constraint for red materials. // It only returns true for materials that are red. [ObjectSelectorHandler(typeof(ObjectSelectorRedMaterialAttribute))] static bool HandleRedMaterial(ObjectSelectorTargetInfo targetInfo, Object[] editedObjects, ObjectSelectorSearchContext context) { var target = targetInfo.LoadObject(); var mat = target as Material; if (mat == null) return false;
return mat.color == Color.red; } }
An object selector handler requires the following signature:
static bool Handler(ObjectSelectorTargetInfo targetInfo, Object[] editedObjects, SearchService.ObjectSelectorSearchContext context);
The first parameter is information about the target. It contains the target object's global identifier. If the target object is already loaded, the first parameter might also contain its type and instance. The second parameter is the array of currently selected objects that are edited in the Inspector. The last parameter is the SearchService.ObjectSelectorHandlerContext passed to the function ISelectorEngine.SelectObject.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.