3 回答

TA貢獻2011條經(jīng)驗 獲得超2個贊
如果我正確理解你的意思,你正在嘗試擁有一個例如List<Move>,并且你想“注冊”Move這里創(chuàng)建的每個實例。那么為什么不簡單地像
// This is a static class so it does not have to be instantiated
// but rather simply "lives" in the assets
public static class MoveManager
{
public static List<Move> Moves = new List<Move>();
}
那么你可以這樣
public class Move
{
private int power;
private float accuracy;
private string type;
private string style;
public Move(int p, float a, string t, string s, float sh)
{
power = p;
accuracy = a;
type = t;
style = s;
// a static class member is simply accessed via the type itself
MoveManager.Moves.Add(this);
}
}
問題:什么時候?qū)⑺鼈儚牧斜碇袆h除?
通常你想在解構(gòu)函數(shù)中執(zhí)行此操作,例如
~Move()
{
MoveManager.Moves.Remove(this);
}
但在 Unity 中,解構(gòu)函數(shù)不會自動調(diào)用 - 特別是當任何東西仍在引用此Move實例時......劇透警報:Moves列表總是可以的!Moves因此,如果需要,每次您確實想要銷毀實例時,都必須“手動”清理列表Move。

TA貢獻1824條經(jīng)驗 獲得超8個贊
我正在使用建議的方法,但發(fā)現(xiàn)為了在運行時“導入”[RuntimeInitializeOnLoadMethod]必須設置使用的類型。可能還有其他方法,但這是我最終得到的用于完成答案的 3 個移動文件。
MoveManager.cs
?public static class MoveManager
?{
? ? public static List<Move> Moves = new List<Move>();
?}
移動.cs
public class Move
{
? ? public string name;
? ? public string description;
? ? public Target target;
? ? public int power;
? ? public float accuracy;
? ? public int cost;
? ? public game_Type type;
? ? public Style style;
? ? public Move(
? ? ? ? string name
? ? ? ? , string description
? ? ? ? , int power
? ? ? ? , float accuracy
? ? ? ? , int cost
? ? ? ? , string typeName
? ? ? ? , string styleName
? ? ? ? , string targetType
? ? )
? ? {
? ? ? ? this.name = name;
? ? ? ? this.power = power;
? ? ? ? this.accuracy = accuracy;
? ? ? ? this.description = description;
? ? ? ? this.type = game_Types.getByName(typeName);
? ? ? ? this.style = MasterStyles.getByName(styleName);
? ? ? ? this.target = new Target(targetType);
? ? ? ? // a static class member is simply accessed via the type itself
? ? ? ? Debug.Log("Registering " + name + " type!");
? ? ? ? MoveManager.Moves.Add(this);
? ? }
}
move_basic.cs
class move_basic
{
? ? [RuntimeInitializeOnLoadMethod]
? ? static void OnRuntimeMethodLoad()
? ? {
? ? ? ? // Name this move
? ? ? ? string name = "Attack!";
? ? ? ? // Describe this move
? ? ? ? string description = "Strike out at the enemy with tooth, claw, weapon, or whatever else is available!";
? ? ? ? // Choose the type of target this move is for
? ? ? ? // self/ally/selfAlly/enemy/enemies
? ? ? ? string target = "enemy";
? ? ? ? // The move typing - This determines cost reduction as well as potential attack bonuses
? ? ? ? string typeName = "physical";
? ? ? ? game_Type type = game_Types.Types.Find(x => x.name == typeName);
? ? ? ? // The move style - This determines additional bonuses and modifiers
? ? ? ? string styleName = "martial";
? ? ? ? // Style style = ??
? ? ? ? // Power of the move, base/'normal' power is 50.
? ? ? ? int power = 50;
? ? ? ? // Accuracy of the move, base/normal is 90
? ? ? ? int accuracy = 90;
? ? ? ? // MP Cost of the move
? ? ? ? int cost = 0;
? ? ? ? Move mv = new Move(name, description, power, accuracy, cost, typeName, styleName, target);
? ? }
}
既然我已經(jīng)確認這項工作有效,下一步就是向注冊的動作添加方法,這才是真正的魔力所在。
當您需要創(chuàng)建一個易于擴展的對象庫(可能需要包含可調(diào)用方法)時,這似乎是一個非常理想的解決方案。如果不是出于這種需要,從 csv 或其他東西導入屬性可能會更好。
一般來說,我對統(tǒng)一和游戲設計還很陌生,所以我非常高興編輯/更正/改進這篇文章以幫助未來的搜索者。

TA貢獻1818條經(jīng)驗 獲得超11個贊
沒有(好的)方法可以做你想做的事。
選項1:
自己親手寫出來。例如,就像我在這里所做的那樣。這總是有效的,并且是您要努力避免的事情。
選項2:
反思性地檢查程序集并加載它包含的所有類,如下所示。但這很容易出現(xiàn)意想不到的事情。例如,當您創(chuàng)建獨立構(gòu)建時,Unity 會剔除這些類,以免它們被編譯,因為它們在任何地方都沒有被引用?;蛘邿o意中實例化了一個不應該實例化的對象?;蛘邲]有記住“哦,是的,MoveXYZ 有一個獨特的構(gòu)造函數(shù),需要一個額外的參數(shù)。”
- 3 回答
- 0 關注
- 239 瀏覽
添加回答
舉報