参考:https://www.tutorialspoint.com/What-are-obsolete-attributes-in-Chash
参考:https://docs.microsoft.com/en-us/dotnet/api/system.obsoleteattribute?view=net-6.0
参考:https://stackoverflow.com/questions/31804260/do-unity-functions-that-are-obsolete-still-work

C#自身就有一个ObsoleteAttribute类,其实Unity用的就是它:

namespace System
{[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate, Inherited = false)]public sealed class ObsoleteAttribute : Attribute{public ObsoleteAttribute();public ObsoleteAttribute(string message);public ObsoleteAttribute(string message, bool error);public bool IsError { get; }public string Message { get; }}
}

只要把这个Attribute加到Method上面,编译器会去检测,当Method被调用时,就发出Warning或Error。可以看到ObsoleteAttribute的构造函数最多接受俩参数,第二个参数为true,则编译器会编译失败,生成Error,否则生成Warning,具体的信息由第一个参数message表示。

用法很简单,用于过时的Method上,Unity官方在更新它的C#代码时,经常使用这个Attribute,写法如下:

using System;public class Demo {// 保留原本的代码, 添加Obsolete属性[Obsolete("Old Method shouldn't be used! Use New Method instead", true)]static void OldMethod() {Console.WriteLine("This is the old method!");}static void NewMethod() {Console.WriteLine("This is the new method!");}public static void Main() {OldMethod();}
}

也可以啥都不写[Obsolete]:

public class TestAnimation : MonoBehaviour
{// 输出Assets\TestAnimation.cs(49,9): warning CS0618: 'TestAnimation.FFF()' is obsolete: 'FF'[Obsolete("FF")]public void FFF(){}// 输出Assets\TestAnimation.cs(56,9): warning CS0612: 'TestAnimation.SSS()' is obsolete    [Obsolete]// 其实这玩意儿对于代码执行, 没有任何影响public void SSS(){}void Start(){FFF();SSS();// 必须得用上才会有编译提示}
}

最后顺便说一句,这一部分的内容是写到C#编译器里的(ObsoleteAttribute is effectively hard-coded into the C# compiler ),所以要想实现自己版本的CustomObsoleteAttribute,几乎是不太可能的。

Unity的[Obsolete]属性相关推荐

  1. unity材质球属性无法修改

    unity材质球属性无法修改 出现的问题情况: 解决办法 出现的问题情况: 如图,unity材质球属性是灰色的无法修改 解决办法 找到材质球然后对原来的材质球进行复制(Crtl+D),再将复制后的材质 ...

  2. Unity 灯光设置 —— 灯光属性

    Unity 灯光属性 Type:灯光类型,所有类型的灯光都其实共用一个组件,本质上是一样的. Color : 灯光颜色 Mode : 灯光模型有Realtime,Baked,Mixed三种 Reati ...

  3. Unity之ASE 属性面板详解

    前言 我们详细的讲解下ASE编辑器的属性面板. 我们先来看下大致的面板信息,如果对某一条属性有疑问,可以往后查找 1.General 通用面板 Shader Name: Editable text b ...

  4. 【Unity】Inspector属性

    Attribute(属性)的作用直接体现在Inspector窗口中,通过使用Attribute,可以对Inspector窗口的内容进行灵活展现和管理,相当于一个辅助工具. RequireCompone ...

  5. Unity修改材质属性(包含自建Shader)

    Shader的属性窗口定义很多属性,例如:   而我想更改他的Lerp end值,就需要拿到这个值: GetComponent<Renderer>().material.SetFloat( ...

  6. 【Unity】编辑器属性

    using UnityEngine; using System.Collections;// 会在编辑模式下运行Update FixedUpdate OnGUI [ExecuteInEditMode] ...

  7. Unity:RectTransform面板属性获取

    如图,transform.Position和rect.transform.position是一个东西,经过检测,数值一样. anchoredPosition(或transform.Position,或 ...

  8. Unity 2017 Game Optimization 读书笔记(3)Scripting Strategies Part 3

    1.Avoid retrieving string properties from GameObjects 通常来讲,从C#的object中获取string 属性没有额外的内存开销,但是从Unity中 ...

  9. C# Obsolete

    Obsolete 属性将某个程序实体标记为一个建议不再使用的实体.每次使用被标记为已过时的实体时,随后将生成警告或错误,这取决于属性是如何配置的.例如: 上面在Main函数中调用,只产生了一个警告的信 ...

  10. Unity官方教程Ruby大冒险的自学笔记

    Unity官方教程Ruby大冒险的自学笔记 一. //正确例子: void Update(){//获取运动矢量moveX = Input.GetAxisRaw("Horizontal&quo ...

最新文章

  1. B 站的前端崩了,后端的你别慌!
  2. 前端开发需要了解的JS插件
  3. 火星今天飞抵西非国家寻找埃博拉疫情
  4. python3 open()内置函数
  5. 给Java程序员的Golang教程
  6. Future取消线程执行
  7. 新分类!全总结!最新Awesome-SLU-Survey资源库开源!
  8. Unity中Json文件编写注意
  9. SNW2010中国大会 ZDNet现场报道
  10. 架构篇--系统监控--spring-boot2.0.X 系统原生信息监控,SQL信息监控,cpu温度监控报警,cup磁盘内存使用率监控报警,自定义端点监控以及子节点获取,系统异常邮件通知
  11. protoc ——protubuf编译后的可执行文件命令usage
  12. 1.1 认识Word 2010操作界面
  13. kali linux Live Usb Encrypted Persistence配置教程
  14. component is not authorized by this account hint: [B3GVCa0189e575] 错误解决?
  15. 模糊聚类算法(FCM)
  16. 红帽企业Linux发行日期
  17. 购买计算机一定要追求独立显卡,购买电脑的常识.pptx
  18. 数字模拟电路课程设计multisim仿真源文件和设计原理
  19. ecshop2.5软件的文件架构{转}
  20. csdn博客markdown编辑器下修改图片大小及文字颜色

热门文章

  1. Exchange Rate Difference
  2. android恶意积分墙代码,传统积分墙的忧虑:微信积分墙刷榜优化分析
  3. c语言数组文曲星猜数游戏编程,关于文曲星上猜数字游戏的c编程方法
  4. 全自动加药装置详细介绍
  5. 对比只适用于有两个或多于两个层次的因子
  6. ansible常用模块
  7. 纯干货万字长文,(强烈建议收藏)一文通读Git使用教程
  8. java是要在安装的盆运行吗,Java程序员(单身30年):告诫各位,千万不要和女程序员做同事!...
  9. 经纬度的多种格式和转换方式
  10. python中类名(..)(..)的情况及_call_函数解析