Skip to content

Instantly share code, notes, and snippets.

@zeroZshadow
Last active October 24, 2018 10:26
Show Gist options
  • Save zeroZshadow/af3f96c923515f230f9490f0b4fd5032 to your computer and use it in GitHub Desktop.
Save zeroZshadow/af3f96c923515f230f9490f0b4fd5032 to your computer and use it in GitHub Desktop.
BasicTemplateProvider loading fix
// Copyright (c) Improbable Worlds Ltd, All Rights Reserved
using System;
using System.Linq;
using Improbable.Assets;
using Improbable.Unity;
using Improbable.Unity.Assets;
using Improbable.Unity.Entity;
using UnityEngine;
namespace Improbable.MinimalBuildSystem.Prefabs
{
public class BasicTemplateProvider : MonoBehaviour, IEntityTemplateProvider
{
[SerializeField]
private WorkerPlatform platform;
// The template provider can't be instantiated during construction as Application.isEditor doesn't work.
private IEntityTemplateProvider templateProvider;
private IEntityTemplateProvider TemplateProvider
{
get
{
if (templateProvider == null)
{
var gameObjectLoader = InitializeAssetLoader();
templateProvider = InitializeTemplateProvider(gameObjectLoader);
}
return templateProvider;
}
}
private IAssetLoader<GameObject> InitializeAssetLoader()
{
return new ResourceGameObjectLoader();
}
private IEntityTemplateProvider InitializeTemplateProvider(IAssetLoader<GameObject> gameObjectLoader)
{
return new AssetDatabaseTemplateProvider(new CachingAssetDatabase(gameObjectLoader));
}
public void PrepareTemplate(string prefabName, Action<string> onSuccess, Action<Exception> onError)
{
TemplateProvider.PrepareTemplate(prefabName, onSuccess, onError);
}
public GameObject GetEntityTemplate(string prefabName)
{
return TemplateProvider.GetEntityTemplate(prefabName);
}
public void CancelAllTemplatePreparations()
{
TemplateProvider.CancelAllTemplatePreparations();
}
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
static void SetupPlaymodeCallbacks()
{
UnityEditor.EditorApplication.playModeStateChanged += PrepareEntityPrefabs;
// Safety check
UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += (scene, mode) =>
{
var provider = FindObjectOfType<BasicTemplateProvider>();
if (provider != null) {
var workerPlatform = provider.platform;
if (workerPlatform == 0)
{
Debug.LogWarning("The BasicTemplateProvider instance has an invalid WorkerPlatform set.", provider);
}
}
};
}
private static void PrepareEntityPrefabs(UnityEditor.PlayModeStateChange state)
{
// Only do this when we go from edit mode to play mode
if (state != UnityEditor.PlayModeStateChange.ExitingEditMode)
{
return;
}
// Get worker platform
var workerPlatform = FindObjectOfType<BasicTemplateProvider>().platform;
Debug.LogFormat("Preparing EntityPrefabs for {0}", workerPlatform.ToString());
// Find editor method to start export
const string entityPrefabsTypeName = "Improbable.Unity.MinimalBuildSystem.EntityPrefabs";
var entityPrefabsType = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.FullName == entityPrefabsTypeName
select type).First();
// Start prefab export
var method = entityPrefabsType.GetMethod("Export");
method.Invoke(null, new object[] { workerPlatform });
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment