Different YAML Line wrapping in Unity vs UnityYAMLMerge

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.

After a bit more investigation, it seems that after a YAMLMerge, it changes the word wrapping on the files.

So something to do with that, I can only assume.

So as an example, this file is what unity will serialize:

%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: 61002b575d8440828fe4e0936c1d3850, type: 3}
  m_Name: en_StringLibrary
  m_EditorClassIdentifier: 
  GUID:
    m_data: f4963499c84f22b405c4ee219ae44e19
  Version: 0
  Entries:
    - UID:
        m_data: 4f15f1141e3676765d095c02046e5095
      Key: Content/Story/Townies/Generic/JoinTown01_02
      Value: But as this is a demo I'll just make it easy for you and start living
        here!
      Comment: null
      State: 0

But UnityYAMLMerge will output 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: 61002b575d8440828fe4e0936c1d3850, type: 3}
  m_Name: en_StringLibrary
  m_EditorClassIdentifier: 
  GUID:
    m_data: f4963499c84f22b405c4ee219ae44e19
  Version: 0
  Entries:
    - UID:
        m_data: 4f15f1141e3676765d095c02046e5095
      Key: Content/Story/Townies/Generic/JoinTown01_02
      Value: But as this is a demo I'll just make it easy for you and start living here!
      Comment: null
      State: 0

Note the word ‘here’ in the ‘value’ field being pushed to the next line.

good coding nice

Make bug report on this :slight_smile:

The bug was reported here: Unity Issue Tracker - UnityYAMLMerge does not respect EditorSettings.serializeInlineMappingsOnOneLine. It was marked as Won’t Fix.

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);
        }
    }
}

now fixed it : Unity Issue Tracker -在与 UnityYAMLMerge 合并时,将额外的换行符添加到资产文件中 — Unity Issue Tracker - Additional line breaks are added to asset files when merging with UnityYAMLMerge
can use unity 6000.1 UnityYAMLMerge file replace

The link says it’s fixed in 6000.0.33f1. But I’m on 6000.0.58f2 and the line wrappings of the Editor and of UnityYAMLMerge are still different for me.

I recreated unityYamlMerge in rust and fixed it ^^

Basically had to reverse-engineer the unity serialization code, but hey, it works