Unity MeshRenderer rendering sorting problem (setting of SortingLayer and order in layer)

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

Intelligent Recommendation

Unity rendering order

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 ...

Levels and rendering order in Unity

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 analysis

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 ...

Unity Rendering Order (2)

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...

Understanding the rendering order in Unity and some pits and rendering sorting solutions in 2.5D games

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...

More Recommendation

Unity multi-camera rendering UI 3D object display order problem

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...

Unity Mesh, Meshfilter, Meshrenderer Light Scanning

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...

Unity light setting, select GPU rendering

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. ...

Unity Dropdown drop-down list is blocked by Canvas SortingLayer

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 ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top