- Home /
How to merge Sorting Layers ?
I have two unity3d projects, both are using sprite renderers and sorting layers. Project 1 has Following Sorting Layers:
Default
Tile Map
Items
Animals
UI
Particles
Project 2 has Following Sorting Layers:
Default
Background
Slots
Chips
Foreground
Map
UI
Project 1 is my main project. I need to merge these both projects. I created new layers in project 1 and then imported project 2 but sorting layers of project 2 are broken .Setting each layer again in project 2 is very difficult task and rework. How can i merge these two project without breaking any sorting layer or tags ?
Answer by Bunny83 · Jul 02, 2018 at 01:34 PM
You have to fix the layers before the import. The layer for each object is just the numeric value of the layer. Since your two projects have conflicting layers:
// layer Project1 Project2
// ---------------------------------
// 2 | Tile Map | Background
// 3 | Items | Slots
// 4 | Animals | Chips
// 5 | UI | Foreground
// 6 | Particles | Map
When you just import project 2 into 1 then all what happens is that you basically renamed all the layers from project 2. Once merged you can't differentiate which object comes from which project.
What you have to do are those steps inside project 2 before you merge them:
Keep an eye on the target layers from project 1. If the layer names match, just move to the next layer. In your case only the first layer is the same. Since layer 2 should become "tilemap" you have to move your "background" layer to a free layer in both projects. So just use layer 8 for Background, layer 9 for Slots, layer 10 for Chips, layer 11 for Foreground etc. If "Tile Map" and "Map" should be considered the same you may want to move the objects from layer 6 to the now empty layer 2 in project 2. However if they are not the same, just use a new layer (12). Finally you probably want to move the objects from the UI layer 7 to the new UI layer 5.
Now here comes the major problem: There is no built-in way to change the layer of all objects on a certain layer. The layer may be set on scene objects (maybe in multiple scenes) or in prefabs stored in the project. Furthermore scripts may use already either hardcoded layers or LayerMasks which also need to be adjusted.
Though the main issue is the actual layer of the individual objects in scenes / prefabs. For this problem there are two approaches possible. First with the asset serialization mode set to "force text" you could create a script to iterate through all assets files in the project, search for the "m_Layer" field and apply the desired change. This should automatically cover all objects in all scenes as well as all prefabs.
The second way would do the changes with an editor script inside the editor. However that means you have to open, change and save each scene as well as iterating through all prefabs in the AssetDatabase. Also keep in mind that you have to search through the child objects as well.
Both ways may have the potential to fail or to corrupt your project if something goes wrong. Also keep in mind that the changes i've mentioned above may need to be carried out in a very specific order to not loose any information
Usually i perfer to apply changes inside Unity and go through the assetdatabase and Unity's serialization system. However i think in this case the manual route through the YAML text files may be faster. If you have a YAML parser you could make the process more safe / fail proof.
As far as i know there is no ready to use solution to this problem. Keep in mind that the layer names are just there for convenience. The only thing that matters is the layer index which is stored inside each object. Have a look at the example YAML file over here or at one of your own scene files. Find a "GameObject" and look for the "m_Layer" field.
Once you managed to move all objects to their new layers you should check your code, settings and LayerMasks where you may use layers and get your project2 working again with the new layers before you do the merge ^^