AssetDatabase.LoadAssetAtPath() seems to only work after AssetDatabase.CreateAsset() is first called. But doing so would destroy the Asset that is already there. If I call AssetDatabase.LoadAssetAtPath() immediately after loading the Unity editor, it does not return the full data. Can anyone see what I’m doing wrong in the code sample below?
using UnityEngine;
using UnityEditor;
[System.Serializable]
public class AssetTest : ScriptableObject
{
public UIObjectSerializedData m_UIObjectSerializedData;
public string m_OtherString;
[System.Serializable]
public class UIObjectSerializedData
{
public string m_NameString;
public int m_IntTest;
}
public static void CreateAsset(AssetTest asset)
{
Debug.Log("CreateAsset: " + asset.name + "
");
AssetDatabase.CreateAsset(asset, "Assets/AssetTest.asset");
}
public static AssetTest LoadAsset(string assetPath)
{
var asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(AssetTest));
Debug.Log("LoadAsset: " + assetPath + "=" + asset.name + "
");
return asset as AssetTest;
}
public string ShowData()
{
string outString = m_OtherString + ", " + m_UIObjectSerializedData.m_NameString + ", " + m_UIObjectSerializedData.m_IntTest.ToString();
return outString;
}
}
public class AssetTestMenu
{
static string AssetTestPathName = "Assets/AssetTest.asset";
[MenuItem("AssetTest/Test Create")]
static void TestCreateMenu()
{
AssetTest asset = ScriptableObject.CreateInstance<AssetTest>();
asset.name = "MytestAsset";
AssetTest.CreateAsset(asset);
asset.m_OtherString = "OtherString";
asset.m_UIObjectSerializedData.m_NameString = "This space intentionally left blank.";
asset.m_UIObjectSerializedData.m_IntTest = 12345;
AssetDatabase.SaveAssets();
// try loading it right away to see if we got the right data
Debug.Log("Test Create: NameString=" + asset.ShowData() + "
");
TestLoadMenu();
}
[MenuItem("AssetTest/Test Load")]
static void TestLoadMenu()
{
var loadedObj = AssetTest.LoadAsset(AssetTestPathName);
AssetTest asset = loadedObj as AssetTest;
Debug.Log("Test Load: NameString=" + asset.ShowData() + "
");
}
}
Output
If AssetTest/Test Create is selected:
CreateAsset: MytestAsset
Test Create: NameString=OtherString, This space intentionally left blank., 12345
LoadAsset: Assets/AssetTest.asset=AssetTest
Test Load: NameString=OtherString, This space intentionally left blank., 12345
If AssetTest/Test Load is selected without first choosing AssetTest/Test Load:
LoadAsset: Assets/AssetTest.asset=AssetTest
Test Load: NameString=, , 0
,I am also having this problem. Have you come up with a solution to it? In my case the object which has been created comes back out, and some of the fields are populated and others are null. In terms of your class structure, the m_NameString variable is populated but the m_IntTest is null.
– anon70817236