Skip to content

Instantly share code, notes, and snippets.

@vysinsky
Last active December 3, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vysinsky/ad12f187775ae2236f8ab2b4a4b56aa9 to your computer and use it in GitHub Desktop.
Save vysinsky/ad12f187775ae2236f8ab2b4a4b56aa9 to your computer and use it in GitHub Desktop.
Custom entity authoring behaviour for Camera object.
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;
namespace Acme.Authoring
{
/// <summary>
/// Custom authoring way must be used because converting Camera object to Entity in traditional way (ConvertToEntity nor SubScene) does not work.
/// </summary>
[DisallowMultipleComponent]
public class CameraAuthoring : MonoBehaviour
{
private void Start()
{
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
var entity = em.CreateEntity();
em.SetName(entity, "Main Camera");
em.AddComponent<CopyTransformToGameObject>(entity);
em.AddComponentData(entity, new Rotation()
{
Value = transform.rotation
});
em.AddComponentData(entity, new Translation()
{
Value = transform.position
});
em.AddComponent<LocalToWorld>(entity);
// Taken from ConvertToEntity.InjectOriginalComponents
foreach (var com in transform.GetComponents<Component>())
{
if (com is GameObjectEntity || com is ConvertToEntity ||
com is ComponentDataProxyBase || com is StopConvertToEntity)
continue;
em.AddComponentObject(entity, com);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment