如何将嵌套的 类 组织为 Unity 中的引用?
How do I organize nested classes to be references in Unity?
所以我正在开发一款游戏,我想要一些有关如何对项目进行排序的建议,以便在 Unity 中轻松、清晰地进行编码参考。我知道我目前的方法有缺陷,真的我只是想要一些指导。
所以我正在做的是使用 classes 来分隔项目类别及其项目。
例如,这里有一个脚本设置的想法:
public class Items {
public class Equipment {
public class Weapons {
public Item Sword = new Item();
Sword.name = "Sword";
Sword.cost = 1;
}
}
}
然后一个基本项目 class 例如
public class Item {
public string name;
public int cost;
}
我可以看出这是一种糟糕的做法,尤其是基于我一直遇到的问题,但我喜欢使用像 Items.Equipment.Weapons.Sword 这样的参考的想法,而且我已经习惯了使用它API我以前用过
我愿意彻底改变一切,我只想要一些提示。谢谢
我想我的主要问题是(曾经)组织嵌套 class 的最佳方式是什么,以便它们可以轻松地从其他脚本中引用?
我找到的答案是,在我的例子中,与其嵌套 classes,不如使用名称空间将项目分成类别。百万感谢。
我建议使用 ScriptableObjects 来创建您的物品、盔甲和武器。 You'll have to spend an hour or two learning them, 但我认为如果您走那条路,您会对自己的设计感到满意。
将 ScriptableObject 视为一组属性(物品名称、成本、攻击力、防御力等)。对于游戏中的每个项目,您都创建一个 ScriptableObject 实例。这些 ScriptableObject 实例随后成为您的 Unity 项目中的资产,就像预制件或精灵一样。这意味着您可以在项目中四处拖动它们,并将它们分配给 MonoBehaviours 上的字段。这将使您能够通过将设备从项目视图拖到检查器中来将设备分配给角色。
这是一个外观示例
Item.cs
public class Item : ScriptableObject
{
public string name;
public int cost;
public Sprite image;
}
Equipment.cs
public class Equipment : Item
{
public Slots slot;
}
public enum Slots
{
Body,
DoubleHanded,
Hands,
Head,
Feet,
Legs,
LeftHand,
RightHand
}
Weapon.cs
// CreateAssetMenu is what lets you create an instance of a Weapon in your Project
// view. Make a folder for your weapons, then right click inside that folder (in the
// Unity project view) and there should be a menu option for Equipment -> Create Weapon
[CreateAssetMenu(menuName = "Equipment/Create Weapon")]
public class Weapon : Equipment
{
public int attackPower;
public int attackSpeed;
public WeaponTypes weaponType;
}
public enum WeaponTypes
{
Axe,
Bow,
Sword
}
Armor.cs
[CreateAssetMenu(menuName = "Equipment/Create Armor")]
public class Armor : Equipment
{
public int defensePower;
}
现在在你的项目中创建一堆武器和盔甲。
ScriptableObjects 的一大优点是您可以在 Inspector 中编辑它们,而不必通过代码来完成(尽管您也可以这样做)。
现在在您的 "character" MonoBehaviour 上,为该角色的设备添加一些属性。
public class Character : MonoBehaviour
{
public Armor bodyArmor;
public Armor headArmor;
public Weapon weapon;
}
现在您可以在检查器中将您的武器和盔甲分配给您的角色
您可能需要比我的示例更符合您需求的内容,但这些是基础知识。我建议花一些时间查看 ScriptableObjects。阅读我之前链接的 Unity 文档,或在 YouTube 上观看一些视频。
Unity 的优势之一是它允许您通过编辑器而不是通过代码进行大量设计和配置,而 ScriptableObjects 加强了这一点。
所以我正在开发一款游戏,我想要一些有关如何对项目进行排序的建议,以便在 Unity 中轻松、清晰地进行编码参考。我知道我目前的方法有缺陷,真的我只是想要一些指导。
所以我正在做的是使用 classes 来分隔项目类别及其项目。 例如,这里有一个脚本设置的想法:
public class Items {
public class Equipment {
public class Weapons {
public Item Sword = new Item();
Sword.name = "Sword";
Sword.cost = 1;
}
}
}
然后一个基本项目 class 例如
public class Item {
public string name;
public int cost;
}
我可以看出这是一种糟糕的做法,尤其是基于我一直遇到的问题,但我喜欢使用像 Items.Equipment.Weapons.Sword 这样的参考的想法,而且我已经习惯了使用它API我以前用过
我愿意彻底改变一切,我只想要一些提示。谢谢
我想我的主要问题是(曾经)组织嵌套 class 的最佳方式是什么,以便它们可以轻松地从其他脚本中引用?
我找到的答案是,在我的例子中,与其嵌套 classes,不如使用名称空间将项目分成类别。百万感谢。
我建议使用 ScriptableObjects 来创建您的物品、盔甲和武器。 You'll have to spend an hour or two learning them, 但我认为如果您走那条路,您会对自己的设计感到满意。
将 ScriptableObject 视为一组属性(物品名称、成本、攻击力、防御力等)。对于游戏中的每个项目,您都创建一个 ScriptableObject 实例。这些 ScriptableObject 实例随后成为您的 Unity 项目中的资产,就像预制件或精灵一样。这意味着您可以在项目中四处拖动它们,并将它们分配给 MonoBehaviours 上的字段。这将使您能够通过将设备从项目视图拖到检查器中来将设备分配给角色。
这是一个外观示例
Item.cs
public class Item : ScriptableObject
{
public string name;
public int cost;
public Sprite image;
}
Equipment.cs
public class Equipment : Item
{
public Slots slot;
}
public enum Slots
{
Body,
DoubleHanded,
Hands,
Head,
Feet,
Legs,
LeftHand,
RightHand
}
Weapon.cs
// CreateAssetMenu is what lets you create an instance of a Weapon in your Project
// view. Make a folder for your weapons, then right click inside that folder (in the
// Unity project view) and there should be a menu option for Equipment -> Create Weapon
[CreateAssetMenu(menuName = "Equipment/Create Weapon")]
public class Weapon : Equipment
{
public int attackPower;
public int attackSpeed;
public WeaponTypes weaponType;
}
public enum WeaponTypes
{
Axe,
Bow,
Sword
}
Armor.cs
[CreateAssetMenu(menuName = "Equipment/Create Armor")]
public class Armor : Equipment
{
public int defensePower;
}
现在在你的项目中创建一堆武器和盔甲。
ScriptableObjects 的一大优点是您可以在 Inspector 中编辑它们,而不必通过代码来完成(尽管您也可以这样做)。
现在在您的 "character" MonoBehaviour 上,为该角色的设备添加一些属性。
public class Character : MonoBehaviour
{
public Armor bodyArmor;
public Armor headArmor;
public Weapon weapon;
}
现在您可以在检查器中将您的武器和盔甲分配给您的角色
您可能需要比我的示例更符合您需求的内容,但这些是基础知识。我建议花一些时间查看 ScriptableObjects。阅读我之前链接的 Unity 文档,或在 YouTube 上观看一些视频。
Unity 的优势之一是它允许您通过编辑器而不是通过代码进行大量设计和配置,而 ScriptableObjects 加强了这一点。