Version: 2021.2
LanguageEnglish
  • C#
Method group is Obsolete

ObjectSelectorHandlerAttribute

class in UnityEditor.SearchService

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Obsolete ObjectSelectorHandlerAttribute has been deprecated. Use SearchContextAttribute instead.

Description

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.