讓我們直接看一個例子。假設我們有:從Update方法移出比較:使用委託或其他方法?
Update(){
if (value.Equals("circular")) moveGameObjectInACircularWay();
else if (value.Equals("linear")) moveGameObjectInALinearWay();
}
我認爲這不是很優雅的解決方案。 Unity需要對每一幀進行比較。這對我來說聽起來不太合適。我只是猜測它應該是一些其他的方式來實現相同的:
Start() {
if (value.Equals("circular")) movement += moveGameObjectInACircularWay;
else if (value.Equals("linear")) movement += moveGameObjectInALinearWay;
}
Update() {
movement();
}
我想解決方案是與代表有關。這就是爲什麼我建議的解決方案看起來像代表。我不明白什麼代表都很好。
你需要委託和事件來做到這一點。我建議你看下面的兩個視頻,因爲你提到你不明白代表是什麼:https://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates https://unity3d.com/learn/tutorials/modules/intermediate/scripting/events – Programmer
[Unity中的簡單事件系統]的可能重複(http://stackoverflow.com/questions/36244660/simple-event-system-in-unity) – Fattie
@JoeBlow您的評論聲音對我來說,比起IlonaHari提出的更好的解決方案。我們能做什麼?編輯包含你的解決方案?如果你願意,我可以爲你做。謝謝! – chelder