Tuesday, 21 May 2019

Unity C# Sorting and Displaying Mesh Renderers in layers

1. Create a new C# script as "SetSortingLayer" and paste the below contents.
2. Attach the script to game objects with Mesh Renderers set the sorting layer. 0 being the lowest of the base layer and the top layer goes 0n increment



using UnityEngine;

 [ExecuteInEditMode]
 public class SetSortingLayer : MonoBehaviour {
     public Renderer MyRenderer;
     public string MySortingLayer;
     public int MySortingOrderInLayer;
   
     // Use this for initialization
     void Start () {
         if (MyRenderer == null) {
             MyRenderer = this.GetComponent<Renderer>();
         }
           

         SetLayer();
     }


     public void SetLayer() {
         if (MyRenderer == null) {
             MyRenderer = this.GetComponent<Renderer>();
         }
           
         MyRenderer.sortingLayerName = MySortingLayer;
         MyRenderer.sortingOrder = MySortingOrderInLayer;
       
         //Debug.Log(MyRenderer.sortingLayerName + " " + MyRenderer.sortingOrder);
     }
 
 }

No comments:

Post a Comment