Does anyone know if it’s possible to stop unity from serializing strings over multiple lines in .asset files?
I have the following simple script:
using UnityEngine;
namespace DefaultNamespace
{
[CreateAssetMenu(menuName = "Hello/Create")]
public class teststring : ScriptableObject
{
public string test;
}
}
And it produces output like this :
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7ec26491b0f442ff89cadf7646cb4e59, type: 3}
m_Name: New Teststring
m_EditorClassIdentifier:
test: Hello, this is a long string which will go over multiple lines I hope. I
really need to see how serialization behaves for something like this.
I’ve tried setting ‘Serialize Inline Mappings On One Line’ in the Editor Settings but it doesn’t seem to have any effect.
The main issue is that it constantly changes the wrap point - so merging files like this can be an absolute nightmare.
My workaround is to use a python wrapper around UnityYAMLMerge that would do the following:
# call UnityYAMLMerge
...
# Store the path for post-merge format cleanup
with open(
Path(__file__).parent.parent / "Assets/Scripts/Editor/DirtyMergedPaths.txt",
"a",
) as f:
f.write(f"{merged}\n")
Then I use the Editor scriptAssets\Scripts\Editor\CleanUpUnityYAMLMerge.cs to cleanup the formatting after UnityYAMLMerge:
// This is a workaround for UnityYAMLMerge's inconsistent YAML formatting.
// It forces Unity to reserialize the assets that were merged.
// https://discussions.unity.com/t/different-yaml-line-wrapping-in-unity-vs-unityyamlmerge/1511775
// https://issuetracker.unity3d.com/issues/unityyamlmerge-does-not-respect-editorsettings-dot-serializeinlinemappingsononeline
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
public class CleanUpUnityYAMLMerge : AssetPostprocessor
{
const string pathOfDirtyMergedAssets = "Assets/Scripts/Editor/DirtyMergedPaths.txt";
static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{
if (System.Array.IndexOf(importedAssets, pathOfDirtyMergedAssets) >= 0)
{
var dirtyPaths = new List<string>();
if (System.IO.File.Exists(pathOfDirtyMergedAssets))
{
foreach (var line in System.IO.File.ReadAllLines(pathOfDirtyMergedAssets))
{
var trimmed = line.Trim();
if (!string.IsNullOrEmpty(trimmed))
dirtyPaths.Add(trimmed);
}
}
if (dirtyPaths.Count > 0)
{
AssetDatabase.ForceReserializeAssets(dirtyPaths);
Debug.Log($"Reserialized {dirtyPaths.Count} assets listed in {pathOfDirtyMergedAssets}.");
}
AssetDatabase.DeleteAsset(pathOfDirtyMergedAssets);
}
}
}