forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Baking.cs
99 lines (86 loc) · 3.66 KB
/
Baking.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Unity.Entities;
using UnityEngine;
namespace ExampleCode.Bakers
{
// An example component for which we want to
// define an authoring component and a baker.
public struct EnergyShield : IComponentData
{
public int HitPoints;
public int MaxHitPoints;
public float RechargeDelay;
public float RechargeRate;
}
// An authoring component for EnergyShield.
// By itself, an authoring component is just an ordinary MonoBehavior.
public class EnergyShieldAuthoring : MonoBehaviour
{
// Notice the authoring component has no HitPoints field.
// This is fine as long as we don't need to set the HitPoints
// value in the editor.
// (The fact that these names mirror the fields
// of EnergyShield is not a requirement.)
public int MaxHitPoints;
public float RechargeDelay;
public float RechargeRate;
// The baker for our EnergyShield authoring component.
// For every GameObject in an entity subscene, baking creates a
// corresponding entity. This baker is run once for every
// EnergyShieldAuthoring instance that's attached to any GameObject in
// the entity subscene.
public class Baker : Baker<EnergyShieldAuthoring>
{
public override void Bake(EnergyShieldAuthoring authoring)
{
// The TransformUsageFlags specifies which transform components the
// entity should have. 'None' means that it doesn't need any.
var entity = GetEntity(TransformUsageFlags.None);
// This simple baker adds just one component to the entity.
AddComponent(entity, new EnergyShield
{
HitPoints = authoring.MaxHitPoints,
MaxHitPoints = authoring.MaxHitPoints,
RechargeDelay = authoring.RechargeDelay,
RechargeRate = authoring.RechargeRate,
});
}
}
}
/*
* A Baker must register the data it accesses. For the authoring component, this is automatic, but for other
* data (assets, prefabs, and other GameObject components), you must access them through
* Baker methods to ensure the Baker is aware of them:
*/
public struct MyComponent : IComponentData
{
public int A;
public float B;
public Entity Prefab;
}
public class MyAuthoring : MonoBehaviour
{
public GameObject prefab;
public GameObject otherGO;
public Mesh mesh;
public class Baker : Baker<MyAuthoring>
{
public override void Bake(MyAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
// To do this, use the functions that the Baker provides to access other components
// instead of the ones provided by GameObject. In the same way, if you access data from an asset,
// you need to create a dependency for it, so the Baker reruns if the asset changes.
// We want to re-bake if anything changes in the mesh itself.
var transform = GetComponent<Transform>(authoring.otherGO);
DependsOn(authoring.mesh);
AddComponent(entity, new MyComponent
{
A = authoring.mesh.vertexCount,
B = transform.localPosition.x,
// to register and convert Prefabs, call `GetEntity` in the baker:
Prefab = GetEntity(authoring.prefab, TransformUsageFlags.Dynamic)
});
}
}
}
}