Ok I have finally managed to come up with a solution for this one, Ill post what I did here as I could not find the information anywhere else online so hopefully it will help someone else in future.
I created a CustomTiledImporter that will look at the sorting layer name on the tiled layer and set the z position of the layer based on the number of layers in the map. This will set all layers that target the same sorting layer to the same value so the map can be created with multiple layers, but they will all share the same sorting layer and z position in unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Tiled2Unity.CustomTiledImporter]
public class WraithNathCustomTiledImporter : Tiled2Unity.ICustomTiledImporter
{
public void HandleCustomProperties(UnityEngine.GameObject gameObject, IDictionary<string, string> props)
{
}
public void CustomizePrefab(GameObject prefab)
{
Tiled2Unity.TiledMap map = prefab.GetComponent<Tiled2Unity.TiledMap>();
Rect mapRect = map.GetMapRect();
float depthPerLayer = -map.TileHeight / mapRect.height;
var renderers = prefab.GetComponentsInChildren<Renderer>();
foreach (Renderer renderer in renderers)
{
//Get the transform for the parent layer
Transform parentTransform = renderer.transform.parent.transform;
float z = (depthPerLayer * SortingLayer.GetLayerValueFromID(renderer.sortingLayerID));
//Set the z position for the layer
parentTransform.position = new Vector3(parentTransform.position.x, parentTransform.position.y, z); ;
}
}
}
I then copied the Tiled2Unity SpriteDepthInMap script and created a new version of it called SpriteDepthInSortingLayer. This is essentially exactly the same but with a property called InteractWithSortingLayer.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpriteDepthInSortingLayer : MonoBehaviour {
[Tooltip("The TiledMap instance our sprite is interacting with.")]
public Tiled2Unity.TiledMap AttachedMap = null;
[HideInInspector]
[Tooltip("Which Sorting layer our sprite is interacting with. Will render above lower layers and below higher layers. Render order of Tiles on same layer will depend on location.")]
public int InteractWithSortingLayer = 0;
[Tooltip("For maps where tileset heights are different than map tile heights. Enter the tileset height here. Useful/crucial for isometric maps. Leave at default (0) if you don't care.")]
public int TilesetHeight = 0;
private void Start()
{
if (this.AttachedMap == null)
{
Debug.LogError(String.Format("Sprite must be attached to a TiledMap instance in order to calucluate the 'z-depth' on that map. Check the SpriteDepthInMap component in the Inspector."));
return;
}
}
private void Update()
{
UpdateSpriteDepth();
}
public void UpdateSpriteDepth()
{
// Put position into map space
Vector3 spritePosition = this.gameObject.transform.position;
spritePosition -= this.AttachedMap.gameObject.transform.position;
// Some maps (like isometric) have a tileset height that is larger than the map tile height in order to get the isometric illusion. We need to know that difference in caluclating depth.
if (TilesetHeight != 0)
{
int delta_y = this.AttachedMap.TileHeight - this.TilesetHeight;
spritePosition.y += delta_y;
}
Rect mapRect = this.AttachedMap.GetMapRect();
float depthPerLayer = -this.AttachedMap.TileHeight / mapRect.height;
float depth_z = (spritePosition.y / this.AttachedMap.ExportScale / mapRect.height) + (depthPerLayer * this.InteractWithSortingLayer);
// Assign our depth value in the z component.
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, depth_z);
}
}
You don't need to create the new script above as the old one will work, just remember you are now referencing a sorting layer rather than a map layer. I just wanted to make a new script so I could create a better editor for it as per below;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
[CustomEditor(typeof(SpriteDepthInSortingLayer))]
public class SpriteDepthInSortingLayerEditor : UnityEditor.Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
// Get the renderer from the target object
var script = (target as SpriteDepthInSortingLayer);
// If there is no script we cant do
if (script==null)
return;
//Get the current sort layer selection
int sortLayerSelection = script.InteractWithSortingLayer;
GUIContent[] sortingLayerContexts = GetSortingLayerContexts();
int newSortingLayerIndex = EditorGUILayout.Popup(new GUIContent("Sorting Layer"), sortLayerSelection, sortingLayerContexts);
//Change layer or show layers menu
if (newSortingLayerIndex == sortingLayerContexts.Length - 1)
{
EditorApplication.ExecuteMenuItem("Edit/Project Settings/Tags and Layers");
}
else if (newSortingLayerIndex != sortLayerSelection)
{
Undo.RecordObject(script, "Edit Sorting Layer ID");
script.InteractWithSortingLayer = newSortingLayerIndex;
EditorUtility.SetDirty(script);
}
//Add button to set the depth now
if (GUILayout.Button("Set Depth (Changes Transform Z Position)"))
{
if (script.AttachedMap == null)
{
Debug.LogError("Cannot set sprite depth without an Attached Map");
}
else
{
script.UpdateSpriteDepth();
}
}
}
public static string[] GetSortingLayerNames()
{
Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
return (string[])sortingLayersProperty.GetValue(null, new object[0]);
}
public static GUIContent[] GetSortingLayerContexts()
{
List<GUIContent> contexts = new List<GUIContent>();
foreach (string layerName in GetSortingLayerNames())
{
contexts.Add(new GUIContent(layerName));
}
contexts.Add(GUIContent.none);
contexts.Add(new GUIContent("Edit Layers..."));
return contexts.ToArray();
}
}

Now the character can go in front and behind of any sprite I put on sorting layer level 1 (or different if selected in the editor) regardless of which map level they are on.
Note: Its Important to still export the Tiled map into unity with the Use Depth buffer option ticked as this uses a depth shader on the layers.
Note 2: I also set the order in layer of my player sprite to 999 so it was on top of everything else, I added some more layers to the map later and it started appearing behind sprites with higher order in layers. Works perfectly now.