Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andrew-raphael-lukasik/039f5289edc2b7fb1107690e85a32ada to your computer and use it in GitHub Desktop.

Select an option

Save andrew-raphael-lukasik/039f5289edc2b7fb1107690e85a32ada to your computer and use it in GitHub Desktop.
GameObject-Entity conversion examples
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
}
}
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;
}
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
[RequireComponent( typeof(ConvertToEntity) )]
public class MyComponentAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
[SerializeField] string _value = "hello dots";
void IConvertGameObjectToEntity.Convert ( Entity entity , EntityManager dstManager , GameObjectConversionSystem conversionSystem )
{
dstManager.AddComponentData( entity , new MyComponent{
Value = _value
} );
}
}
public struct MyComponent : IComponentData
{
public FixedString32Bytes Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment