RequireComponent的使用:
當你添加的一個用了RequireComponent組件的腳本,需要的組件將會自動被添加到game object(游戲物體)。這個可以有效的避免組裝錯誤。舉個例子一個腳本可能需要剛體總是被添加在相同的game object(游戲物體)上。用RequireComponent屬性的話,這個過程將被自動完成,因此你可以永遠不會犯組裝錯誤。
用法:在新建的類前面加 [RequireComponent(typeof(Rigidbody))]
比如有一個Cube,我們新建一個腳本PhysicCtrl,寫上
[RequireComponent(typeof (Rigidbody))]
public class PhysicCtrl : MonoBehaviour {
}
把這個腳本拖到這個Cube上,那么這個Cube將自動獲得Rigidbody組件,並且無法單獨刪除此Cube的Rigidbody,需要刪除腳本再刪除Rigidbody
AddComponentMenu的使用:
AddComponentMenu屬性允許你在"Component"菜單中放置一個無論在哪的腳本,而不是僅僅在"Component->Scripts"菜單中。
用法:[AddComponentMenu("NGUI/Examples/cubSpin")]