2016-11-11 52 views
1

腳本通常是這樣編寫的,以便在檢查器中公開字段;有沒有辦法使用屬性來代替?在Unity中,我可以在檢查器窗口中公開C#*屬性*嗎?

// 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; 
} 
+0

有點令人難以置信的Unity如何在這麼多版本之後仍然不支持這個。 –

+0

@RayKoopa問題是序列化。在字段上進行序列化比在沒有任何問題出現的情況下更容易。 –

回答

0

整理。

不能直接使用屬性檢查器,但你可以支持字段創建屬性:

public GameObject WrongBallProperty { 
    get { return this.wrongBallProperty; } 
    set { //do whatever } 
} 

[SerializeField] 
private gameObject wrongBallProperty; 

這將在檢查顯示wrongBallProperty,並允許你做任何邏輯你需要在getset。有關更多信息,請參閱SerializeField reference

相關問題