I’m trying to load some scriptableobjects using Addressables.LoadAssetsAsync() in the System but I’m not sure its even viable or I maybe doing something wrong. Are the systems not run in editor mode so that’s why it wouldn’t work. If not what’s the best way to convert scriptableobjects into entity’s.
For Subscenes, the GOConversionSystem runs in edit-mode, which Addressables doesn’t have support currently. Instead, you can use AssetDatabase for that.
For ConvertAndDestroy/ConvertAndInject inside normal scenes, they should run at play-mode as usual (AFAIK) so Addressables should work there. Just remember that Addressables is async, which may cause you GOCS to finish before Addressables finishes loading.
That’s what I figured, thanks for the help.
So I’m still unsure what would be the best way to load scriptableobjects, just use an ordinary SystemBase and destroy it afterwards?
For that, I usually use RuntimeInitializeOnLoadMethod to call Addressables API and then I create entities to store my IComponentData representation of the SO (it usually requires some boilerplate, but until Addressables get some official support for DOTS I think it is a fine way to go).
That’s great, thanks again for the help.
AssetDatabase do not work in a GameObjectConversionSystems, (not for me )
calling this function insidie GameObjectConversionSystems.Onpdate
public void CreateMaterial()
{
GeneratedMaterial = new Material(Shader.Find("Universal Render Pipeline/Simple Lit"));
GeneratedMaterial.SetTexture("_MainTex", TextureAtlas);
var materialPath = FoldersPaths.RenderingAssetsHoldersPath + $"\\{SceneName}_Material.mat";
AssetDatabase.CreateAsset(GeneratedMaterial, materialPath);
// Print the path of the created asset
Debug.Log(AssetDatabase.GetAssetPath(GeneratedMaterial));
}
Error:
UnityException: Creating asset at path Assets\Scripts\ScriptableObjects\Map Asset Holders\Instances\Map5_Material.mat failed.
Working fine for me, maybe it is an issue with CreateAsset
Actually, just tested here, CreateAsset works fine too:
that’s very weird! that’s not working for me.
is it a bug or something ?
That’s the code im using
using UnityEditor;
using UnityEngine;
public class TestConversionsystem : GameObjectConversionSystem
{
protected override void OnUpdate()
{
Entities.ForEach((MaterialHolder materialHolder) => {
Debug.Log("Start Material Creation");
AssetDatabase.CreateAsset(new Material(Shader.Find("Universal Render Pipeline/Simple Lit")), "Assets/TestMaterial.mat");
Debug.Log("Material Created");
});
}
}
Subscene Debugs
Hmm, interesting, found out that it only work if the SubScene is open for edit, when closed it does indeed give that error
Still, LoadAsset works fine, it is an issue with CreateAsset only
Thanks for all the help this is what I’m using to load the assets if anyone else is curious.
public static int TryGetUnityObjectsofTypefromPath<T>(string path, List<T> assetsFound) where T : UnityEngine.Object
{
string[] filepaths = System.IO.Directory.GetFiles(path);
int countfound = 0;
Debug.Log("Filepaths length " + filepaths.Length);
if(filepaths != null && filepaths.Length > 0)
{
for(int i = 0; i < filepaths.Length; i++)
{
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(filepaths[i], typeof(T));
if(obj is T asset)
{
countfound++;
if(!assetsFound.Contains(asset))
{
assetsFound.Add(asset);
}
}
}
}
return countfound;
}