Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

To Merge or not to Merge that is the question...

Discussion in 'Works In Progress - Archive' started by PrefabEvolution, Jun 30, 2014.

  1. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    Hi! Surely every Unity developer faced with the asset merging problem. And almost all have failed. Everyone wondered: "Why is this happening? Why merging source code does not cause such serious problems?" This is because the structure of the code is very different from the serialized assets.
    Assets in unity serialized to YAML, this language is simple, but it's enough to store data necessary for us. But merging Unity YAML files are not trivial in text mode without any knowing about the context of the object. So after many failed attempts to create a tool thats can proper merge serialized assets, i finally came closer to solve this problem. The biggest problem that I encountered during the development: "How to find matching object in foreign object hierarchy?" For example you have 3 files "BaseScene" "MineScene" and "TheirsScene". So for every object inside BaseScene you should find matching object in all others. In serialized files every object have a local unique ID. If you open any scene or prefab with any text editor you will see something like this:
    "--- !u!1 &6002778" first number is type of the object, and the second is ID. BUT... this ids is incremental for objects with the same type. So there is no any guaranty that two or more people can create new Objects inside the same scene and they will not have id collision. And this is a biggest unity FAIL! And the only way to match objects is to add to all objects some extra unique id. But how? Where we can store some extra data about every Scene or Prefab object? Its simple, just create hidden GameObject with Component for scene and ScriptableObject for prefabs. This Component/ScriptableObject should find new objects inside parent hierarchy and generate unique ids for them. How to create unique id? GUID! Chance of GUID collision tends to zero.
    So now we can match objects, and this is a biggest step to make a useful 3-way asset merge tool. So i try to implement it and this is simple result:

    Test Prefab Hierarchy
    : 2014-07-01 00-01-31 1.unity - PrefabEvolution_TEST - PC, Mac & Linux Standalone.png

    And this is Expected result:
    2014-07-01 00-04-48 1.unity - PrefabEvolution_TEST - PC, Mac & Linux Standalone.png

    GUI of the Merge Tool:
    2014-07-01 00-07-52 1.unity - PrefabEvolution_TEST - PC, Mac & Linux Standalone.png

    This is result of AutoMerge:
    2014-07-01 00-09-54 1.unity - PrefabEvolution_TEST - PC, Mac & Linux Standalone.png

    This is just a simple test case that this tool successful passed. Its was tested on more complicated objects hierarchy with successful. But the most complicated thing that i can't solve, its merging array properties with concurrent changes. How to determine what user wants to? The only way i see is to mark array properties with attributes that will determine merging behavior. But i don't think that this is a good idea. Any ideas?

    Wait for your comments, questions, ideas, test cases and so on))
     
  2. vidjo

    vidjo

    Joined:
    Sep 8, 2013
    Posts:
    97
    Seems cool. Keep up the good work!
     
  3. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    So finally i found the way, how to merge arrays properties. This is very simple simple way. I just add to every array element property some child pseudo property "Index" that contains index inside parent array. Moreover in property matching functions i add code that find most matching property inside "Theirs" and "Mine" properties. In this case i can identify reordering elements of the array and merge array properties automatically :)