Code Generator - OneYoungMean/Entitas-CSharp-OYM GitHub Wiki
Entitas 代码生成器
代码生成器为您生成类和方法,因此您可以专注于完成工作。它从根本上减少了您必须编写的代码量,并大大提高了可读性。它使您的代码不易出错,同时确保最佳性能。我强烈推荐使用它!
代码生成器非常灵活,可以根据您的需求进行定制。请参阅安装指南以了解如何在纯C#或Unity项目中使用代码生成器。
自定义生成和代码生成器扩展
您可以通过实现以下方法之一轻松实现自己的生成器 ICodeGeneratorInterfaces
:
- ICodeGeneratorDataProvider: Returns
CodeGeneratorData[]
to provide information for CodeGenerators - ICodeGenerator: Takes
CodeGeneratorData
and returnsCodeGenFile[]
that contain all generated file contents - ICodeGenFilePostProcessor: Takes
CodeGenFile[]
to add modifications to any generated files
但对于大多数项目,提供的发电机将满足所有需求。
可用的代码生成器
Entitas已经附带了所有生成器,这些生成器对于使用Entitas框架进行简洁编码至关重要。您可以使用Unity中的Entitas> Preferences Editor窗口配置它们并打开/关闭它们,也可以手动编辑Entitas.properties根目录中的文件。
ComponentEntityCreator
这个生成器为您提供了一个用于访问和操作实体上的组件的简单界面。结果取决于您的组件是否为:
- 携带标签的组件 (e.g. MovableComponent)
- 带有公共字段的标准组件 (with public fields (e.g. PositionComponent) 下面举例说明一下这两种情况:
标签属性组件 (e.g. MovableComponent)
[Game]
public class MovableComponent : IComponent {}
[Game, FlagPrefix("flag")]
public class DestroyComponent : IComponent {}
You get
GameEntity e;
var movable = e.isMovable;
e.isMovable = true;
e.isMovable = false;
e.flagDestroy = true;
标准组件 (e.g. PositionComponent)
[Game]
public class PositionComponent : IComponent {
public int x;
public int y;
public int z;
}
You get
GameEntity e;
if(e.hasPosition)
var position = e.position;
else
e.AddPosition(x, y, z);
e.ReplacePosition(x, y, z);
e.RemovePosition();
ComponentContextCreator
此Generator可帮助您管理每个上下文存在一次的组件。想想Singleton-Component只存在于Context中而不是静态。您可以使用[Unique]-Attribute 标记单实例组件。输出取决于您的Component是:
- 不带公共字段的唯一标志组件 (e.g. AnimatingComponent)
- U具有公共字段的唯一标准组件 (e.g. UserComponent) 下面举例说明一下这两种情况:
单标志组件 (e.g. AnimatingComponent)
[Game, Unique]
public class AnimatingComponent : IComponent {}
You get
// extensions from `ComponentEntityCreator` are also available
GmeContext context;
GameEntity e = context.animatingEntity;
var isAnimating = context.isAnimating;
context.isAnimating = true;
context.isAnimating = false;
单一标准组件 (e.g. UserComponent)
[Game, Unique]
public class UserComponent : IComponent {
public string name;
public int age;
}
你将得到
// extensions from `ComponentEntityCreator` are also available
GameContext context;
GameEntity e = context.userEntity;
if(context.hasUser)
var name = context.user.name;
else
context.SetUser("John", 42);
context.ReplaceUser("Max", 24);
context.RemoveUser();
ComponentGenerator
[todo]
ComponentLookupGenerator
[todo]
ContextAttributeGenerator
[todo]
ContextGenerator
[todo]
ContextsGenerator
[todo]
EntityGenerator
[todo]
MatcherGenerator
[TODO]
如何添加自己的生成器
- 创建一个新的类,比如
ICodeGenerator
并保存到 Editor 文件夹。 - 您的 Assembly-CSharp-Editor.dll 现在可以被视为插件,因为它包含了您新的生成器。
- 添加
Assembly-CSharp-Editor
到Jenny.Plugins
文件中的 Preferences.properties字段。 - 添加Assembly-CSharp-Editor.dll的文件夹路径到
Jenny.SearchPaths
.这通常是Library/ScriptAssemblies
- Entitas 首选项窗口将在其中一个生成器下拉列表中包含自定义代码生成器的名称。启用它并生成。
一些Debugging相关经验
代码生成器基于运行时的反馈。项目必须先编译才能生成。创建新组件时这不是问题,但是当涉及更改或删除组件时,您的代码可能会停止编译。这是一个相关经验的介绍,以便您在更改和删除组件时避免更大的麻烦。
重命名组件字段
使用重命名重构IDE并生成。这件事情不应该发生,因为字段只影响生成的方法中的方法参数名称。
重命名组件
使用重命名重构IDE,并重命名生成的类中的现有方法,在生成的类中setter与getter并重新生成。
向组件添加新字段
添加新字段并生成。这将导致编译错误,因为某些方法现在需要更多参数,比如e.AddXyz()和e.ReplaceXyz()。您必须更新调用这些方法的所有位置。
从组件中删除字段
这将直接导致编译错误,因为它正在使用中,至少生成的类正在使用它们。在这种情况下,您只需注释掉受影响方法的实现,例如e.AddXyz()和e.ReplaceXyz()。之后,再次生成。
删除组件
完全删除组件和生成的类,然后删除ComponentIds中componentTypes下的组件(如果更改了上下文名称,则可能有前缀)。之后,删除/更改组件的用法并再次生成。您可能需要重启unity。还要确保删除生成器生成的相关代码,例如删除对它的所有引用。
重命名上下文名称 使用IDE的重命名重构来首先重命名ContextAttribute类名,然后生成。