22using System . Collections ;
33using System . Collections . Generic ;
44using System . Reflection ;
5+ using Unity . Collections ;
56using Unity . Entities ;
67using Unity . Mathematics ;
78using Unity . Physics ;
8- using Unity . Physics . Authoring ;
9- using Unity . Physics . Extensions ;
10- using Unity . Physics . Systems ;
119using Unity . Rendering ;
1210using Unity . Transforms ;
1311using UnityEngine ;
1412using Collider = Unity . Physics . Collider ;
1513using Material = UnityEngine . Material ;
1614using Mesh = UnityEngine . Mesh ;
1715
18- /// <summary>
19- /// Helper for demos set up in C# rather than in the editor
20- /// </summary>
21- public class BasePhysicsDemo : MonoBehaviour
16+ public abstract class SceneCreationSettings : IComponentData
2217{
23- public static World DefaultWorld => World . DefaultGameObjectInjectionWorld ;
18+ public Material DynamicMaterial ;
19+ public Material StaticMaterial ;
20+ }
2421
25- public static void ResetDefaultWorld ( )
26- {
27- DefaultWorld . EntityManager . CompleteAllJobs ( ) ;
28- foreach ( var system in DefaultWorld . Systems )
29- {
30- system . Enabled = false ;
31- }
22+ public class SceneCreatedTag : IComponentData { } ;
3223
33- DefaultWorld . Dispose ( ) ;
34- DefaultWorldInitialization . Initialize ( "Default World" , false ) ;
35- }
3624
37- public Material dynamicMaterial ;
38- public Material staticMaterial ;
25+ // Base class of authoring components that create scene from code, using SceneCreationSystem
26+ public abstract class SceneCreationAuthoring < T > : MonoBehaviour , IConvertGameObjectToEntity
27+ where T : SceneCreationSettings , new ( )
28+ {
29+ public Material DynamicMaterial ;
30+ public Material StaticMaterial ;
3931
40- protected void init ( )
32+ public virtual void Convert ( Entity entity , EntityManager dstManager , GameObjectConversionSystem conversionSystem )
4133 {
42- // Load assets
43- //dynamicMaterial = (Material)Resources.Load("Materials/PhysicsDynamicMaterial");
44- //staticMaterial = (Material)Resources.Load("Materials/PhysicsStaticMaterial");
34+ T sceneSettings = new T
35+ {
36+ DynamicMaterial = DynamicMaterial ,
37+ StaticMaterial = StaticMaterial
38+ } ;
39+ dstManager . AddComponentData ( entity , sceneSettings ) ;
4540 }
41+ }
42+
43+ [ UpdateInGroup ( typeof ( InitializationSystemGroup ) ) ]
44+ public abstract class SceneCreationSystem < T > : SystemBase
45+ where T : SceneCreationSettings
46+ {
47+ private EntityQuery m_ScenesToCreateQuery ;
48+
49+ protected Material DynamicMaterial ;
50+ protected Material StaticMaterial ;
51+
52+ public NativeList < BlobAssetReference < Collider > > CreatedColliders ;
4653
47- #if ! UNITY_EDITOR
48- void OnEnable ( )
54+ protected override void OnCreate ( )
4955 {
50- Application . logMessageReceivedThreaded += HandleLogEntry ;
56+ CreatedColliders = new NativeList < BlobAssetReference < Collider > > ( Allocator . Persistent ) ;
57+
58+ m_ScenesToCreateQuery = GetEntityQuery ( new EntityQueryDesc
59+ {
60+ All = new ComponentType [ ] { typeof ( T ) } ,
61+ None = new ComponentType [ ] { typeof ( SceneCreatedTag ) } ,
62+ } ) ;
63+ RequireForUpdate ( GetEntityQuery ( new ComponentType [ ] { typeof ( T ) } ) ) ;
5164 }
5265
53- void HandleLogEntry ( string logEntry , string stackTrace , LogType logType )
66+ protected override void OnUpdate ( )
5467 {
55- if ( logType == LogType . Exception )
68+ if ( m_ScenesToCreateQuery . CalculateEntityCount ( ) == 0 ) return ;
69+
70+ using ( var entities = m_ScenesToCreateQuery . ToEntityArray ( Allocator . TempJob ) )
5671 {
57- // Log exception and exit with non-zero error code
58- UnityEngine . Debug . Log ( $ "Caught an exception, exiting... \n { logEntry } \n { stackTrace } ") ;
59- Application . Quit ( 1 ) ;
72+ foreach ( Entity entity in entities )
73+ {
74+ T settings = EntityManager . GetComponentObject < T > ( entity ) ;
75+ DynamicMaterial = settings . DynamicMaterial ;
76+ StaticMaterial = settings . StaticMaterial ;
77+
78+ CreateScene ( settings ) ;
79+ EntityManager . AddComponentData ( entity , new SceneCreatedTag ( ) ) ;
80+ }
6081 }
6182 }
6283
63- void OnDisable ( )
84+ protected override void OnDestroy ( )
6485 {
65- Application . logMessageReceivedThreaded -= HandleLogEntry ;
66- }
67-
68- #endif
86+ foreach ( var collider in CreatedColliders )
87+ {
88+ if ( collider . IsCreated )
89+ collider . Dispose ( ) ;
90+ }
6991
70- protected virtual void Start ( )
71- {
72- init ( ) ;
92+ CreatedColliders . Dispose ( ) ;
7393 }
7494
75- //
76- // Object creation
77- //
95+ public abstract void CreateScene ( T sceneSettings ) ;
96+
97+ #region Utilities
7898
79- // TODO: add proper utility APIs for converting Collider into buffers usable for UnityEngine.Mesh and for drawing lines
8099 static readonly Type k_DrawComponent = typeof ( Unity . Physics . Authoring . DisplayBodyColliders )
81100 . GetNestedType ( "DrawComponent" , BindingFlags . NonPublic ) ;
82101
@@ -88,7 +107,7 @@ protected virtual void Start()
88107 static readonly FieldInfo k_DisplayResultsMesh = k_DisplayResult . GetField ( "Mesh" ) ;
89108 static readonly PropertyInfo k_DisplayResultsTransform = k_DisplayResult . GetProperty ( "Transform" ) ;
90109
91- internal static void CreateRenderMeshForCollider (
110+ public static void CreateRenderMeshForCollider (
92111 EntityManager entityManager , Entity entity , BlobAssetReference < Collider > collider , Material material
93112 )
94113 {
@@ -117,10 +136,10 @@ internal static void CreateRenderMeshForCollider(
117136 entityManager . AddComponentData ( entity , new RenderBounds { Value = mesh . bounds . ToAABB ( ) } ) ;
118137 }
119138
120- Entity CreateBody ( float3 position , quaternion orientation , BlobAssetReference < Collider > collider ,
139+ public Entity CreateBody ( float3 position , quaternion orientation , BlobAssetReference < Collider > collider ,
121140 float3 linearVelocity , float3 angularVelocity , float mass , bool isDynamic )
122141 {
123- var entityManager = DefaultWorld . EntityManager ;
142+ var entityManager = World . DefaultGameObjectInjectionWorld . EntityManager ;
124143
125144 Entity entity = entityManager . CreateEntity ( new ComponentType [ ] { } ) ;
126145
@@ -131,7 +150,7 @@ Entity CreateBody(float3 position, quaternion orientation, BlobAssetReference<Co
131150 var colliderComponent = new PhysicsCollider { Value = collider } ;
132151 entityManager . AddComponentData ( entity , colliderComponent ) ;
133152
134- CreateRenderMeshForCollider ( entityManager , entity , collider , isDynamic ? dynamicMaterial : staticMaterial ) ;
153+ CreateRenderMeshForCollider ( entityManager , entity , collider , isDynamic ? DynamicMaterial : StaticMaterial ) ;
135154
136155 if ( isDynamic )
137156 {
@@ -153,20 +172,20 @@ Entity CreateBody(float3 position, quaternion orientation, BlobAssetReference<Co
153172 return entity ;
154173 }
155174
156- protected Entity CreateStaticBody ( float3 position , quaternion orientation , BlobAssetReference < Collider > collider )
175+ public Entity CreateStaticBody ( float3 position , quaternion orientation , BlobAssetReference < Collider > collider )
157176 {
158177 return CreateBody ( position , orientation , collider , float3 . zero , float3 . zero , 0.0f , false ) ;
159178 }
160179
161- protected Entity CreateDynamicBody ( float3 position , quaternion orientation , BlobAssetReference < Collider > collider ,
180+ public Entity CreateDynamicBody ( float3 position , quaternion orientation , BlobAssetReference < Collider > collider ,
162181 float3 linearVelocity , float3 angularVelocity , float mass )
163182 {
164183 return CreateBody ( position , orientation , collider , linearVelocity , angularVelocity , mass , true ) ;
165184 }
166185
167- protected Entity CreateJoint ( PhysicsJoint joint , Entity entityA , Entity entityB , bool enableCollision = false )
186+ public Entity CreateJoint ( PhysicsJoint joint , Entity entityA , Entity entityB , bool enableCollision = false )
168187 {
169- var entityManager = DefaultWorld . EntityManager ;
188+ var entityManager = World . DefaultGameObjectInjectionWorld . EntityManager ;
170189 ComponentType [ ] componentTypes =
171190 {
172191 typeof ( PhysicsConstrainedBodyPair ) ,
@@ -180,11 +199,13 @@ protected Entity CreateJoint(PhysicsJoint joint, Entity entityA, Entity entityB,
180199 return jointEntity ;
181200 }
182201
183- protected RigidTransform GetBodyTransform ( Entity entity )
202+ public static RigidTransform GetBodyTransform ( Entity entity )
184203 {
185- var entityManager = DefaultWorld . EntityManager ;
204+ var entityManager = World . DefaultGameObjectInjectionWorld . EntityManager ;
186205 return new RigidTransform (
187206 entityManager . GetComponentData < Rotation > ( entity ) . Value ,
188207 entityManager . GetComponentData < Translation > ( entity ) . Value ) ;
189208 }
209+
210+ #endregion
190211}
0 commit comments