- Home /
How can I save splatmaps (alphamaps) in a Scriptable Object?
Hello everyone! I'm trying to save the 3 dimensional float array returned by the "GetAlphamaps (...)" function of the terrain data to a Scriptable Object in a variable of the same type. When I hit the save button in the script I have the save function, apparently this 3-dimensional array is saved in the scriptableobject it creates and saved in the folder of my choice. But when I press the Play button, and I stop the execution again. The 3-dimensional array acquires the value of null. I have read some articles and posts related to this topic and the reason seems to be that multidimensional arrays (among other types of variables) cannot be serialized by Unity and therefore their value becomes null in this case. But I would like to know if there is any article, video, post, method or class that I do not know and have not found to be able to save this array in an optimal way? I am attaching the code fragments of the scripts that are related to the creation and saving of the ScriptableObject, so that you know better what I am trying to do.
I call from a editor Script:
if (GUILayout.Button("Save Splatmaps", GUILayout.Width(currentWidth / 3)))
{
SplatmapSerializedData serializedSplatmaps = myTarget.SaveSplatmaps();
if (previousPath == string.Empty)
previousPath = "Assets/";
string path = EditorUtility.SaveFilePanel("Save Splatmap Data Asset", previousPath, myTarget.name + "_SplatmapData", "asset");
if (string.IsNullOrEmpty(path)) return;
previousPath = path;
path = FileUtil.GetProjectRelativePath(path);
TYLER_SplatmapData splatmapData = ScriptableObject.CreateInstance<TYLER_SplatmapData>();
splatmapData.splatMapdata = serializedSplatmaps;
AssetDatabase.CreateAsset(splatmapData, path);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
myTarget.LoadSplatmapData = splatmapData;
}</code>
And the method SaveSpaltmaps():
public SplatmapSerializedData SaveSplatmaps()
{
if (Tiles == null || (Tiles.GetLength(0) != terrainXSize || Tiles.GetLength(1) != terrainZSize)) Tiles = GetTyles();
SplatmapSerializedData serializedSplatmaps = new SplatmapSerializedData();
serializedSplatmaps.splatmaps = new SerializedSplatmap[Tiles.Length];
int cont = 0;
for (int x = 0; x < Tiles.GetLength(0); x++)
{
for (int y = 0; y < Tiles.GetLength(1); y++)
{
Texture2D[] alphamaps = new Texture2D[Tiles[x, y].OriginalTileTerrainData.alphamapTextures.Length];
for (int i = 0; i < Tiles[x, y].OriginalTileTerrainData.alphamapTextures.Length; i++)
{
alphamaps[i] = Instantiate<Texture2D>(Tiles[x, y].OriginalTileTerrainData.alphamapTextures[i]);
alphamaps[i].name.Replace("(Clone)", "");
}
serializedSplatmaps.splatmaps[cont] = new SerializedSplatmap(new Vector2Int(x, y), Tiles[x, y].OriginalTileTerrainData.alphamapResolution, alphamaps);
cont++;
}
}
return serializedSplatmaps;
}
Your answer
Follow this Question
Related Questions
Saving players progress in ScriptableObject asset 1 Answer
Field is member of a type which is serializable but is of type which is not serializable 0 Answers
Make the same serialized class as a ScriptableObject? 1 Answer
Serializable types for ScriptableObject 2 Answers
Scriptableobject not saving if i force close the application. 1 Answer