Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Sirenix.OdinInspector;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
namespace AiGame
{
public class PrefabConvertToEntity : MonoBehaviour
{
private const string AssetPath = @"Assets/AiGame/GameData/Resources/EcsPrefabData";
private const string ResourcePath = @"EcsPrefabData/PrefabDb";
private static bool Loaded;
[SerializeField]
protected List<GameObject> Prefabs = new List<GameObject>();
[SerializeField]
private Unity.Scenes.ReferencedUnityObjects ObjectRefs;
private BlobAssetStore ConversionStore;
private World ConversionWorld;
private GameObjectConversionSettings ConversionSettings;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Initialize()
{
Loaded = false;
}
public static void LoadAllPrefabs()
{
if (Loaded)
{
return;
}
Loaded = true;
var prefabDb = Resources.Load(ResourcePath, typeof(Unity.Scenes.ReferencedUnityObjects)) as Unity.Scenes.ReferencedUnityObjects;
foreach (var obj in prefabDb.Array)
{
var prefabConvert = (PrefabConvertToEntity)obj;
prefabConvert.Load();
}
}
protected virtual void OnEntityConverted(Entity entity, GameObject gameObject, EntityManager manager)
{
}
protected virtual void OnBeforeConversion(EntityManager manager)
{
}
protected virtual void OnAfterConversion()
{
}
[Button("Load")]
public void Load()
{
DefaultWorldInitialization.DefaultLazyEditModeInitialize();
World targetWorld = World.DefaultGameObjectInjectionWorld;
targetWorld.EntityManager.CompleteAllJobs();
World sourceWorld = new World(name);
EntitySerializer.Deserialize(sourceWorld.EntityManager, name, ObjectRefs);
targetWorld.EntityManager.MoveEntitiesFrom(sourceWorld.EntityManager);
sourceWorld.Dispose();
}
#if UNITY_EDITOR
[Button("ConvertAll")]
public void ConvertAll()
{
var prefabDb = Resources.Load(ResourcePath, typeof(Unity.Scenes.ReferencedUnityObjects)) as Unity.Scenes.ReferencedUnityObjects;
foreach (var obj in prefabDb.Array)
{
var prefabConvert = (PrefabConvertToEntity)obj;
prefabConvert.Convert();
}
}
public World StartConversion()
{
EnsurePrefabDb();
ResetObjectRefs();
DefaultWorldInitialization.DefaultLazyEditModeInitialize();
ConversionWorld = new World(name);
ConversionStore = new BlobAssetStore();
ConversionStore.ResetCache(true);
ConversionSettings = new GameObjectConversionSettings(ConversionWorld, GameObjectConversionUtility.ConversionFlags.AssignName, ConversionStore);
OnBeforeConversion(ConversionWorld.EntityManager);
foreach (var prefab in Prefabs)
{
Entity entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(prefab, ConversionSettings);
ConversionWorld.EntityManager.SetName(entity, prefab.name);
OnEntityConverted(entity, prefab, ConversionWorld.EntityManager);
}
return ConversionWorld;
}
public void EndConversion()
{
EntitySerializer.Serialize(ConversionWorld.EntityManager, name, out Unity.Scenes.ReferencedUnityObjects refs);
if (refs != null && refs.Array.Length > 0)
{
ObjectRefs.Array = new Object[refs.Array.Length];
for (int i = 0; i < refs.Array.Length; i++)
{
ObjectRefs.Array[i] = refs.Array[i];
}
}
ConversionWorld.Dispose();
ConversionStore.Dispose();
OnAfterConversion();
}
[Button("Convert")]
protected void Convert()
{
StartConversion();
EndConversion();
}
[Button("Unload")]
private void Unload()
{
World world = World.DefaultGameObjectInjectionWorld;
if (world == null) return;
world.EntityManager.CompleteAllJobs();
var entities = world.EntityManager.GetAllEntities();
world.EntityManager.DestroyEntity(entities);
}
private void EnsurePrefabDb()
{
string path = string.Format("{0}/PrefabDb.asset", AssetPath);
var prefabs = UnityEditor.AssetDatabase.LoadAssetAtPath<Unity.Scenes.ReferencedUnityObjects>(path);
if (prefabs == null)
{
prefabs = ScriptableObject.CreateInstance<Unity.Scenes.ReferencedUnityObjects>();
UnityEditor.AssetDatabase.CreateAsset(prefabs, path);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.EditorUtility.SetDirty(prefabs);
}
List<Object> converters = new List<Object>();
if (prefabs.Array != null)
{
converters.AddRange(prefabs.Array);
}
foreach(var prefab in converters)
{
if (prefab == this)
{
return;
}
}
converters.Add(this);
prefabs.Array = converters.ToArray();
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.EditorUtility.SetDirty(prefabs);
}
private void ResetObjectRefs()
{
if (ObjectRefs != null)
{
ObjectRefs.Array = null;
}
ObjectRefs = ScriptableObject.CreateInstance<Unity.Scenes.ReferencedUnityObjects>();
string path = string.Format("{0}/{1}.asset", AssetPath, name);
UnityEditor.AssetDatabase.CreateAsset(ObjectRefs, path);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.EditorUtility.SetDirty(ObjectRefs);
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment