tags: Unity
In a 3d project, such as creating a cube, there are no two fields, sorting layer and order in layer, in the Inspector. If you need to change the sorting, we can write directly through the script; you can also write the editor script to display the two fields sorting layer and order in layer in the Inspector and set them.
1. Script writing:
using UnityEngine;
public class MeshRendererSorting : MonoBehaviour
{
private MeshRenderer meshRenderer;
void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
meshRenderer.sortingLayerName = "UI";
meshRenderer.sortingOrder = 3;
}
}
2. Write the editor script to display the sort field
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(MeshRenderer))]
public class MeshRendererEditor : Editor
{
MeshRenderer meshRenderer;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
meshRenderer = target as MeshRenderer;
string[] layerNames = new string[SortingLayer.layers.Length];
for (int i = 0; i < SortingLayer.layers.Length; i++)
layerNames[i] = SortingLayer.layers[i].name;
int layerValue = SortingLayer.GetLayerValueFromID(meshRenderer.sortingLayerID);
layerValue = EditorGUILayout.Popup("Sorting Layer", layerValue, layerNames);
SortingLayer layer = SortingLayer.layers[layerValue];
meshRenderer.sortingLayerName = layer.name;
meshRenderer.sortingLayerID = layer.id;
meshRenderer.sortingOrder = EditorGUILayout.IntField("Order in Layer", meshRenderer.sortingOrder);
}
}

Reference: http: //blog.csdn.net/u011748727/article/details/68947207 A factor affecting the rendering order are: 1.Camera Depth When rendering two Camera, Depth greater value, then the object will be ...
0x00 written in front When using unity3d to develop game projects, hierarchical problems are often accompanied by UI development, and after the introduction of 3D models and particle effects, the hier...
Unity rendering order The factors that affect the rendering order in the Unity engine are: Camera Depth Separation of transparent and opaque objects Under the same camera Sorting Layer Order In Layer ...
Camera Except for Canvas in Screen Space - Overlay (screen space coverage mode), other objects in the scene need to be rendered to the screen and all need to be drawn by the specified camera. Multiple...
1.ZTest & ZWrite ZTest: In-depth test, the test result determines whether the chip is discarded after opening, configurable ZWrite: depth write, after opening, decide whether the depth value of th...
About the camera attributes I don't have to repeat this here, I have made some simple research how to control the display of multi-camera A single camera normal mode: Camera Default Settings Default S...
1. Mesh grid Mesh concept: Mesh is a data structure in Unity, called a grid. In layman's terms, Mesh refers to the grid of the model. The 3D model is spliced from polygon, and the polygon is actuall...
table of Contents 1, light setting (1) Basic light rendering (2) Set the light "source" 1, light setting (1) Basic light rendering After the 2020 version, a lighting file is independent, 1. ...
The drop-down list is sometimes obscured by other UI due to the canvas level: I've got a Canvas which has its Render Mode set to World Space. Within this canvas, I have a Dropdown (from the new ...