RequiredInterfaceAttribute

Unity · Версия 2020.3 LTS · Scripting

RequiredInterfaceAttribute

Описание

Когда отмечен тип, все реализации интерфейса указанных типов будут отмечены.

using System;
using UnityEngine;
using UnityEngine.Scripting;

public class NewBehaviourScript : MonoBehaviour
{
    void Start()
    {
        new Foo();
        new Bar();
        new Jar();
        new GenericFoo();
    }
}

interface IUnused {}

interface IFoo {}

interface IGeneric<T> {}

// Foo сохранит IFoo. IUnused будет удален 
[RequiredInterface(typeof(IFoo))]
class Foo : IFoo, IUnused {}

// Bar сохранит IGeneric<int> и IGeneric<double>. IGeneric<string> будет удален 
[RequiredInterface(typeof(IGeneric<int>))]
[RequiredInterface(typeof(IGeneric<double>))]
class Bar : IGeneric<int>, IGeneric<string>, IGeneric<double> {}

// Jar сохранит IGeneric<int>, IGeneric<string> и IGeneric<double> 
[RequiredInterface(typeof(IGeneric<>))]
class Jar : IGeneric<int>, IGeneric<string>, IGeneric<double> {}

// GenericFoo<T> сохранит IGeneric<T>
[RequiredInterface(typeof(IGeneric<>))]
class GenericFoo<T> : IGeneric<T> {}

Обновлено 17 ноября 2026 г.