我對統一的多型性有疑問。我想創建一個抽象的基礎“槍”類來代表我的厄運克隆游戲中的槍。接下來我想創建一些子類,如“shotgun”、“pistol”等,然后在我存盤每個孩子時制作例如“槍”串列。但是當我想這樣做時,我需要從我的基本“槍”類派生,而不是從單聲道行為。這樣我就無法在我的子腳本(如 Shotgun)中存盤對影片師或音頻源的參考。我希望霰彈槍的聲音存盤在霰彈槍腳本中,所以當我想播放它時,我只需呼叫特定的物件方法。但如果沒有單聲道行為,這似乎是不可能的。我認為可撰寫腳本的物件也不能解決我的問題。那我該怎么辦?
uj5u.com熱心網友回復:
只要有
public abstract class Gun : MonoBehaviour
{
// common members like e.g.
// either private -> only this class has access
// or protected -> only this class and inherited classes have accss
// or public -> every other type has access
[SerializeField] protected AudioSource audio;
[SerializeField] protected Animator animator;
// abstract methods all inheritors HAVE TO implement
// maybe virtual methods inheritors CAN overrule or extend but don't have to
// other common private/protected/public methods etc
public virtual void Shoot()
{
// reduce one bullet
// play your sound file etc
}
}
然后每個在自己的檔案中
public class Pistol : Gun
{
}
和
public class Shutgun : Gun
{
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/408548.html
標籤:
上一篇:addforce對角線移動更快
