Version: 2021.3

RequireImplementorsAttribute

class in UnityEngine.Scripting

切换到手册

描述

When the interface type is marked, all types implementing that interface will be marked.

using System;
using UnityEngine;
using UnityEngine.Scripting;

public class NewBehaviourScript : MonoBehaviour { void Start() { IFoo ifoo = new Foo(); } }

[RequireImplementors] interface IFoo {}

class Foo : IFoo {}

// UnusedFoo is not used, however, it will survive managed code stripping due to IFoo having the [RequireImplementors] attribute. class UnusedFoo : IFoo { // Note that unused members of UnusedFoo will still be removed by managed code stripping public static void UnusedMethod() {} }

// IBar is not used so it will be removed by managed code stripping [RequireImplementors] interface IBar {}

// Because IBar is not used, the [RequireImplementors] attribute on IBar has no impact. UnusedBar will also be removed. class UnusedBar : IBar {}