Sco*_*ker 4 c# unity-game-engine
编写脚本时通常会在 Inspector中公开公共字段;有什么方法可以代替使用属性吗?
// instead of this
public GameObject wrongBall;
// is there some way to do this (to trigger an event in the setter, for instance)
// and have it show up in the inspector window?
public GameObject WrongBallProperty
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
澄清:通过较新版本的 Unity,我的意思是任何支持 .NET 4.6 运行时的 Unity 版本(我认为)。并非所有版本都存在该文档,但至少从 Unity 2018.4 [来源]开始就支持它。
不是 Unity 5,而是供参考,如果其他人来这里寻找 Unity 较新版本的答案。
您现在可以使用Auto-Implemented Property Field-Targeted Attributes作为较新版本的 Unity 支持 C# 7.3 功能。
[field: SerializeField]
public GameObject WrongBallProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
有点。
您不能直接在检查器中使用属性,但可以使用支持字段创建属性:
public GameObject WrongBallProperty {
get { return this.wrongBallProperty; }
set { //do whatever }
}
[SerializeField]
private gameObject wrongBallProperty;
Run Code Online (Sandbox Code Playgroud)
这将显示在检查器中,并允许您在和wrongBallProperty中执行所需的任何逻辑。有关详细信息,请参阅SerializeField 参考。getset
| 归档时间: |
|
| 查看次数: |
2697 次 |
| 最近记录: |