Last active
April 19, 2022 16:41
-
-
Save andrew-raphael-lukasik/039f5289edc2b7fb1107690e85a32ada to your computer and use it in GitHub Desktop.
GameObject-Entity conversion examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using Unity.Entities; | |
| public class ConvertGameObjectHierarchyExample : MonoBehaviour | |
| { | |
| [SerializeField] GameObject _prefab = null; | |
| BlobAssetStore _blobAssetStore; | |
| void Awake () | |
| { | |
| var world = World.DefaultGameObjectInjectionWorld; | |
| var command = world.EntityManager; | |
| _blobAssetStore = new BlobAssetStore(); | |
| var conversionSettings = GameObjectConversionSettings.FromWorld( world , _blobAssetStore ); | |
| Entity prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy( _prefab , conversionSettings ); | |
| Entity instance = command.Instantiate( prefab ); | |
| // _blobAssetStore.Dispose();// causes exceptions @0.50 | |
| } | |
| void OnDestroy () | |
| { | |
| _blobAssetStore.Dispose();//workaround | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using Unity.Entities; | |
| public class ConvertGameObjectsToEntitiesExample : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs | |
| { | |
| [SerializeField] GameObject[] _prefabs = new GameObject[0]; | |
| void IConvertGameObjectToEntity.Convert ( Entity entity , EntityManager dstManager , GameObjectConversionSystem conversionSystem ) | |
| { | |
| var prefabs = dstManager.AddBuffer<MyPrefabs>( entity ); | |
| for( int i=0 ; i<_prefabs.Length ; i++ ) | |
| { | |
| var next = _prefabs[i]; | |
| if( next!=null ) | |
| { | |
| Entity prefab = conversionSystem.GetPrimaryEntity( next ); | |
| prefabs.Add( new MyPrefabs{ | |
| Value = prefab | |
| } ); | |
| } | |
| } | |
| } | |
| void IDeclareReferencedPrefabs.DeclareReferencedPrefabs ( List<GameObject> referencedPrefabs ) => referencedPrefabs.AddRange( _prefabs ); | |
| } | |
| [InternalBufferCapacity(0)] | |
| public struct MyPrefabs : IBufferElementData | |
| { | |
| public Entity Value; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment