- Home /
CreateAsset assets failing to load on Editor restart
I have created a test project where all it initially contained was an Assets/Plugin/Build/TestSettings.cs file...
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
namespace OneLoop
{
[InitializeOnLoad]
public class Test : ScriptableObject
{
[SerializeField]
public string bar;
protected const string BUILDSETTINGS_FOLDER = "Assets/Resources/";
protected const string BUILDSETTINGS_NAME = "TestSettings";
protected static Test instance = null;
public static Test inst
{
get
{
if (instance == null)
{
instance = Resources.Load(BUILDSETTINGS_NAME) as Test;
if (instance == null)
{
string path = GetProjectFolder() + "/" + BUILDSETTINGS_FOLDER;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
instance = CreateInstance<Test>();
if (instance != null)
{
Debug.LogWarning("Creating an instance of type " + "Test" + " in file " + BUILDSETTINGS_FOLDER + BUILDSETTINGS_NAME + ".asset");
AssetDatabase.CreateAsset(instance, BUILDSETTINGS_FOLDER + BUILDSETTINGS_NAME + ".asset");
EditorUtility.SetDirty(instance);
AssetDatabase.SaveAssets();
Debug.LogWarning(AssetDatabase.GetAssetPath(instance));
}
else
{
Debug.LogWarning("Failed to create instance of type " + "Test");
}
}
}
return instance;
}
}
public static string GetProjectFolder()
{
return Application.dataPath.Replace("/Assets", "");
}
static Test()
{
Debug.LogWarning(inst.ToString());
}
}
}
Upon Editor startup, its InitializeOnLoad call attempts to check to see if the "TestSettings.asset" resource already exists and if it fails calls CreateAsset to create it. This all works fine and while my Editor is running I can modify/rebuild the code (which reinitializes all classes) and see that Resources.Load now properly loads my asset. However, as soon as I shutdown and restart the Editor, Resources.Load will fail to find my TestSettings.asset.
I'm using some middleware that attempts to do the same thing that I am and their classes are working. I've looked at the difference between their asset files and mine and I see difference. In their files, their m_Script contains data (such as m_Script: {fileID: 11500000, guid: 461bfb74be57ce74eac44498472f0ad0, type: 3}) while mine is blank. Additionally, my m_EditorClassIdentifier contains data (such as m_EditorClassIdentifier: Assembly-CSharp:OneLoop:Test) while theirs is blank.
I've tried Resources.Load and .LoadAssetAtPath as well as equivalent AssetDatabase load object calls and they are all failing for me. What am I doing wrong here?
Please advise.
Thanks, Jason
Answer by Jason-King · Apr 16, 2014 at 09:29 PM
Apparently Unity requires that my class and its filename need to be the same for a proper script connection to work. I was using class Test with filename TestSettings.cs. If I rename TestSettings.cs to Test.cs then it all started to work.
Additionally placing the class within a namespace can result in the same issue. Not sure why - because I have some classes that work in a namespace and now one that doesn't. Additionally I've seen both namespace and non-namespace versions in other middleware examples.