How to render meshes over sprites? (C#)

I have a terrain that is built from procedural mesh generation like this:

74947-terrain.png

Behind this terrain i want to have a white background in sprite-format, but when i put the background in the scene it renders in front of the orange terrain like this:

74948-terrain2.png

My question is as follows:
how do i render the meshes above the sprite? And why are there no options for layering in the mesh renderer component?

You can use unitys ui system to do this ( the way I know how) just make a unity ui canvas and add an image as a child set the canvas to world space, then position

The following scrits and shaders are form the Tiled2Unity asset.

Put this script onto your mesh:

// Code provided by: Nick Gravelyn
// from: https://gist.github.com/nickgravelyn/7460288

using UnityEngine;

namespace Tiled2Unity
{
    // Component does nothing; editor script does all the magic
    public class SortingLayerExposed : MonoBehaviour
    {
    }
}

Put this script inside your Editor folder:

// Based on code provided by: Nick Gravelyn
// from: https://gist.github.com/nickgravelyn/7460288
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

using UnityEngine;
using UnityEditor;
using Tiled2Unity;

[CustomEditor(typeof(Tiled2Unity.SortingLayerExposed))]
public class SortingLayerExposedEditor : UnityEditor.Editor
{
    public override void OnInspectorGUI()
    {
        // Get the renderer from the target object
        var renderer = (target as SortingLayerExposed).gameObject.GetComponent<Renderer>();

        // If there is no renderer, we can't do anything
        if (!renderer)
        {
            return;
        }

        // Seanba: Use a popup that is populated with the acceptable sorting layers for the renderer
        // Also allow the player to bring up the Tag/Layers inspector if they choose so
        string[] sortLayerNames = GetSortingLayerNames();
        int sortLayerSelection = GetSortingLayerIndex(renderer, sortLayerNames);

        GUIContent[] sortingLayerContexts = GetSortingLayerContexts();
        int newSortingLayerIndex = EditorGUILayout.Popup(new GUIContent("Sorting Layer"), sortLayerSelection, sortingLayerContexts);
        if (newSortingLayerIndex == sortingLayerContexts.Length - 1)
        {
            EditorApplication.ExecuteMenuItem("Edit/Project Settings/Tags and Layers");
        }
        else if (newSortingLayerIndex != sortLayerSelection)
        {
            //int newSortingLayerId = sortLayerIds[newSortingLayerIndex];
            string newSortingLayerName = sortLayerNames[newSortingLayerIndex];

            Undo.RecordObject(renderer, "Edit Sorting Layer ID");
            renderer.sortingLayerName = newSortingLayerName;
            //renderer.sortingLayerID = newSortingLayerId;

            EditorUtility.SetDirty(renderer);
        }

        // Expose the manual sorting order within a sort layer
        int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder);
        if (newSortingLayerOrder != renderer.sortingOrder)
        {
            Undo.RecordObject(renderer, "Edit Sorting Order");
            renderer.sortingOrder = newSortingLayerOrder;
            EditorUtility.SetDirty(renderer);
        }
    }

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

    // Get the sorting layer names
    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]);
    }

    // Get the unique sorting layer IDs -- tossed this in for good measure
    public int[] GetSortingLayerUniqueIDs()
    {
        Type internalEditorUtilityType = typeof(UnityEditorInternal.InternalEditorUtility);
        PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
        return (int[])sortingLayerUniqueIDsProperty.GetValue(null, new object[0]);
    }

    public static int GetSortingLayerIndex(Renderer renderer, string[] layerNames)
    {
        for (int i = 0; i < layerNames.Length; ++i)
        {
            if (layerNames *== renderer.sortingLayerName)*

return i;

// Special case for Default, goddammit
if (layerNames == “Default” && String.IsNullOrEmpty(renderer.sortingLayerName))
return i;
}

return 0;
}

public static int GetSortingLayerIdIndex(Renderer renderer, int[] layerIds)
{
for (int i = 0; i < layerIds.Length; ++i)
{
if (layerIds == renderer.sortingLayerID)
return i;
}

return 0;
}

}
Use this shader for the mesh:
Shader “Tiled/TextureTintSnap”
{
Properties
{
[PerRendererData] _MainTex (“Tiled Texture”, 2D) = “white” {}
_Color (“Tint”, Color) = (1,1,1,1)
_AlphaColorKey (“Alpha Color Key”, Color) = (0,0,0,0)
[MaterialToggle] PixelSnap (“Pixel snap”, Float) = 1
}

SubShader
{
Tags
{
“Queue”=“Transparent”
“IgnoreProjector”=“True”
“RenderType”=“Transparent”
“PreviewType”=“Plane”
“CanUseSpriteAtlas”=“True”
}

Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include “UnityCG.cginc”

struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};

fixed4 _Color;

v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif_

return OUT;
}

sampler2D _MainTex;
float4 _AlphaColorKey;

fixed4 frag(v2f IN) : COLOR
{
half4 texcol = tex2D(_MainTex, IN.texcoord);

// The alpha color key is ‘enabled’ if it has solid alpha
if (_AlphaColorKey.a == 1 &&
_AlphaColorKey.r == texcol.r &&
_AlphaColorKey.g == texcol.g &&
_AlphaColorKey.b == texcol.b)
{
texcol.a = 0;
}
else
{
texcol = texcol * IN.color;
}

return texcol;
}
ENDCG
}
}

Fallback “Sprites/Default”
}

Make z position of the sprite background lower than the one of the terrain.