@@ -97,21 +109,21 @@ Adding a GameObject with Unity Sync
- Let's see if our script is working! Let's have an initial entity spawn by
- using
+ Let's see if our script is working! Using
Dynamic Entity. If everything worked correctly, when you enter playmode a gameObject
- should spawn at the position you inputted for the entity.
+ >, spawn an entity with the components we just declared. When you enter
+ playmode a gameObject will instantiate at the position you inputted for
+ the entity.
Generating Blocks
- What if we took this one step further? We've managed to use Unity Sync
- with our Dynamic Entity, and now we can can adapt to changes in entities
- during runtime. Let's go into our .ecsact
file.
+ What if we took this one step further? We used Unity Sync with our Dynamic
+ Entity, and now we can can adapt to changes in entities during runtime.
+ Let's go into our .ecsact
file.
@@ -123,7 +135,7 @@ Generating Blocks
i32 pos_y;
{{'}'}}
- // An action that will happen on an input
+ // An action that will happen on an input, generates new blocks at runtime
action PerformGenerateBlock {{'{'}}
i32 pos_x;
i32 pos_y;
@@ -150,8 +162,8 @@ Generating Blocks
Now we have the code in place to make a block spawn from an action. Any
newly generated entities will be pooled for Unity Sync. This means the
- Sync script that adds the Block GameObject will be automatically added to
- our new entities. Let's make a way to perform our action.
+ Sync script we wrote will add the Block
gameObject to all our
+ new entities. Let's make a way to perform our action.
@@ -197,6 +209,16 @@ Generating Blocks
GameObject to be present during the runtime. With Ecsact and pushing the
action accounted for, all that's left to do is implement the systems.
+
+ Note: Make sure your
+ folder hierarchy
+ is in your Assembly Definition
+
@@ -227,10 +249,10 @@ Generating Blocks
var block = context.Get<{{'example.QueueBlock'}}>();
builder
- .AddComponent<{{'example.Block'}}>(
+ .AddComponent(
new example.Block{{'{'}}{{'}'}}
)
- .AddComponent<{{'example.Position'}}>(
+ .AddComponent(
new example.Position{{'{'}}x = block.pos_x, y = block.pos_y{{'}'}}
)
.Finish();
@@ -240,12 +262,14 @@ Generating Blocks
- When the InputAction
is pressed, a new entity will be
- generated with a block the same way our Dynamic Entity
does.
- But there's a small difference. When an entity is spawned at runtime, the
- Position of the entity won't be applied to the gameobject itself. We can
- use Unity Sync to solve that
- problem.
+ To reiterate, when the InputAction
is pressed a new entity
+ will be generated with a block the same way our first
+ Dynamic Entity
does. But there's a small difference. When an
+ entity is spawned at runtime, the Position of the entity won't be applied
+ to the gameobject itself. We can use
+ Unity Sync to solve that
+ problem. Create a script called `SyncMove` that listen to callbacks from
+ the `init` and `update` component events.
@@ -269,19 +293,19 @@ Generating Blocks
- A listener for OnUpdate
was added too. This will be used a
- bit later for Movement.
+ The listener for OnUpdate
will be important when we add
+ movement.
Movement
- So we can generate entities at will during runtime and manipulate their
- data. The only problem now is we can't interact with them. Let's add
- movement to all the entity blocks that we spawn. We'll add
- Velocity
, a move action, and a corresponding system to apply
- changes.
+ We're not generating entities at will during runtime and can manipulate
+ their data. The only thing missing is we can't interact with them. Let's
+ add movement to all the entity blocks that we spawn. We'll add
+ Velocity
, a Move
action, and a corresponding
+ system to apply changes.
@@ -315,6 +339,14 @@ Movement
Ecsact
, any Dynamic Entities and
GenerateBlockSystem
to see the effects.
+
+
+
+ // Add to your builder we used for GenerateBlock
+ builder.AddComponent(new example.CanMove {{'{'}}{{'}'}})
+
+
+
Let's move to Unity. In our example, we'll add a script to the same
GameObject that holds our Dynamic Entity
called
@@ -372,11 +404,9 @@
Movement
- We've done our Ecsact declarations and added the ability to push our
- Action
. Now like before we implement our systems. This might
- start to feel familiar, the workflow of getting to and from Ecsact doesn't
- change very much. We'll make another script we'll drop into our systems
- folder called MoveSystems
.
+ Now We'll implement our newly declared systems Move
and
+ ApplyVelocity
. Drop the new script into our systems folder
+ and call it MoveSystems
.
@@ -391,10 +421,10 @@ Movement
var velocity = context.Get<{{'example.Velocity'}}>();
var move = context.Action<{{'example.Move'}}>();
- velocity.x_value = move.dir_x;
- velocity.y_value = move.dir_y;
+ velocity.x_value += move.dir_x;
+ velocity.y_value += move.dir_y;
- context.Update<{{'example.Velocity'}}>(velocity);
+ context.Update(velocity);
{{'}'}}
[Ecsact.DefaultSystemImpl(typeof(example.ApplyVelocity))]
@@ -411,22 +441,21 @@ Movement
position.x += velocity.x_value;
position.y += velocity.y_value;
- context.Update<{{'example.Position'}}>(position);
+ context.Update(position);
{{'}'}}
{{'}'}}
- Since we already added SyncMove
earlier, the gameobject
+ Since we already added SyncMove
earlier, the GameObject
should be updated whenever the position of the entity is.
Basic Collision
- We have movement, great! We can finally interact with our game in some
- way. The big problem now is we can go off the screen, and there's no way
- to
+ We have movement, great! We can finally interact with our entities. The
+ big problem now is we can go off the screen, and there's no way to
interact
with the other entities in our scene. We're going to
cover one of the ways in which you can compare entities and create
interactions with Nested Systems
.
@@ -455,11 +484,12 @@
Basic Collision
Nested System
takes each entity that qualifies for CollisionComparer
, and
compares each one against all of the qualified entities in
- CheckCollision
. You can view it as a nested for loop.
+ CheckCollision
.
Note: You need to add the Collider
component to any entities
- that you want to be considered.
+ that you want to be considered. Don't forget the
+ GenerateBlock
builder from before!
@@ -487,27 +517,19 @@ Basic Collision
position.x = position.prev_x;
position.y = position.prev_y;
- if(position.x < otherPos.x) {{'{'}}
- velocity.x_value = -0.5f;
- {{'}'}} else {{'{'}}
- velocity.x_value = 0.5f;
- {{'}'}}
+ velocity.x_value = -velocity.x_value * 0.5f;
+ velocity.y_value = -velocity.y_value * 0.5f;
- if(position.y < otherPos.y) {{'{'}}
- velocity.y_value = -0.5f;
- {{'}'}} else {{'{'}}
- velocity.y_value = 0.5f;
- {{'}'}}
-
- context.Update<{{'example.Velocity'}}>(velocity);
- context.Update<{{'example.Position'}}>(position);
+ context.Update(velocity);
+ context.Update(position);
{{'}'}}
{{'}'}}
{{'}'}}
- There's only 1 system implementation. If there's nothing for a
+ CollisionComparer
doesn't need an implementation because its
+ components are readonly
. If there's nothing for a
system
to consider then the system is considered
trivialBasic Collision
{{'}'}}
-
- If you've opened up the project you will see this. There are gameObjects
- called boundaries
in the project that will create a little
- game area. You can just as easily make your own in any way you like. The
- last thing left to do is add our new StaticCollider
script to
- these gameObjects. Now, we can move, spawn new blocks that can also move,
- and run into game boundaries!
+ The last step is to apply our static colliders to boundaries in our game
+ level. You'll see in the reference example that there are GameObjects
+ called boundaries
using this script. If you're making your
+ own, add them to your boundary so the generated blocks collide with them
Setting Up Unity
- The Ecsact Window
+ The Ecsact Window
If the Ecsact package was successfully installed, there will be a new
window under
@@ -58,7 +58,7 @@
The Ecsact Window
- Assembly
+ Assembly
For systems to be registered into Ecsact, it needs to be part of an
Assembly. In addition, it will need the following dependencies:
@@ -82,7 +82,22 @@
Assembly
- Unity Sync
+ Folder hierarchy
+
+ Everything done with Ecsact, including system implementations, has to be
+ contained in the hierarchy of the generated
+ Assembly definition. If you're
+ just learning we'd recommend putting everything in
+ Assets arrow_right Scripts
+ arrow_right Ecsact
+
+ You should have a configuration that looks something like this:
+
+
+
- Recipes
+ Recipes
Ecsact supports having multiple recipes. You can use other
implementations, or there's one available for EnTT that comes packaged in
@@ -106,7 +121,7 @@
Recipes
- Learning about the Unity Integration
+ Learning about the Unity Integration
To learn more about the Ecsact Integration for Unity, you can follow our