-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWeakRenderedObjectAuthoring.cs
More file actions
93 lines (81 loc) · 3.35 KB
/
WeakRenderedObjectAuthoring.cs
File metadata and controls
93 lines (81 loc) · 3.35 KB
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
using Unity.Entities;
using Unity.Entities.Content;
using Unity.Entities.Serialization;
using UnityEngine;
namespace ContentManagement.Sample
{
#if UNITY_EDITOR
// This authoring component adds two components with WeakObjectReferences: one component for a mesh and one for a list of materials.
// If the UseUntypedId flag is set, it instead adds two components with UntypedWeakReferenceIds.
//
// A WeakObjectReference is basically a typed wrapper around an UntypedWeakReferenceId that is slightly more convenient to use.
// Generally, using WeakObjectReference is preferred unless you need a reference whose asset type isn't fixed at compile time,
// in which case you'll need an UntypedWeakReferenceId.
// Furthermore, the LocalContent component is assigned to the resulting entity,
// enabling the LoadingLocalCatalogSystem to load the files directly from disk.
public class WeakRenderedObjectAuthoring : MonoBehaviour
{
public bool UseUntypedId;
public Mesh Mesh;
public Material[] Materials; // an array because a single mesh can have multiple materials
class Baker : Baker<WeakRenderedObjectAuthoring>
{
public override void Bake(WeakRenderedObjectAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
var mesh = authoring.Mesh;
var materials = authoring.Materials;
// This allows the [LoadingLocalCatalogSystem] to runs and load the content,
// then the system [WeakObjectLoadingSystem] can run and connect the references.
AddComponent<LocalContent>(entity);
if (authoring.UseUntypedId)
{
AddComponent(entity, new WeakMeshUntyped
{
Value = UntypedWeakReferenceId.CreateFromObjectInstance(mesh)
});
var matsBuffer = AddBuffer<WeakMaterialUntyped>(entity);
foreach (var mat in materials)
{
matsBuffer.Add(new WeakMaterialUntyped
{
Value = UntypedWeakReferenceId.CreateFromObjectInstance(mat)
});
}
}
else
{
AddComponent(entity, new WeakMesh
{
Value = new WeakObjectReference<Mesh>(mesh),
});
var matsBuffer = AddBuffer<WeakMaterial>(entity);
foreach (var mat in materials)
{
matsBuffer.Add(new WeakMaterial
{
Value = new WeakObjectReference<Material>(mat),
});
}
}
}
}
}
#endif
public struct WeakMeshUntyped : IComponentData
{
public UntypedWeakReferenceId Value;
}
public struct WeakMaterialUntyped : IBufferElementData
{
public UntypedWeakReferenceId Value;
}
public struct WeakMesh : IComponentData
{
public WeakObjectReference<Mesh> Value;
}
public struct WeakMaterial : IBufferElementData
{
public WeakObjectReference<Material> Value;
}
}