排序图层中的渲染器顺序。
您可以将 GameObject 分组到 SpriteRenderer 组件中的层中。这称为 SortingLayer。
排序顺序决定每个 GameObject 对于每个排序图层中渲染器的优先级。
提供的数字越小,GameObject 显示的距离越远。数字越大,GameObject 越靠近摄像机。这在创建 2D 卷轴游戏时非常有效,因为您可能需要某些 GameObject 位于同一层上,但某些部分显示在其他部分前方。例如,对云进行分层处理,使其显示在天空前方。
**注意**:该值必须介于 -32768 与 32767 之间。
//Attach a script like this to a Sprite GameObject (Create>2D Object>Sprite). Assign a Sprite to it in the Sprite field. //Repeat the first step for another two Sprites and make them overlap each other slightly. This shows how the order number changes the view of the Sprites.
using UnityEngine; public class MyScript : MonoBehaviour { public int MyOrder; public string MyName; }
//Create a folder named “Editor” (Right click in your Assets folder, Create>Folder) //Put this script in the folder. //This script adds fields to the Inspector of your GameObjects with the MyScript script attached. Edit the fields to change the layer and order number each Sprite belongs to.
using UnityEngine; using UnityEditor;
// Custom Editor using SerializedProperties.
[CustomEditor(typeof(MyScript))] public class MeshSortingOrderExample : Editor { SerializedProperty m_Name; SerializedProperty m_Order;
private SpriteRenderer rend;
void OnEnable() { // Fetch the properties from the MyScript script and set up the SerializedProperties. m_Name = serializedObject.FindProperty("MyName"); m_Order = serializedObject.FindProperty("MyOrder"); }
void CheckRenderer() { //Check that the GameObject you select in the hierarchy has a SpriteRenderer component if (Selection.activeGameObject.GetComponent<SpriteRenderer>()) { //Fetch the SpriteRenderer from the selected GameObject rend = Selection.activeGameObject.GetComponent<SpriteRenderer>(); //Change the sorting layer to the name you specify in the TextField //Changes to Default if the name you enter doesn't exist rend.sortingLayerName = m_Name.stringValue; //Change the order (or priority) of the layer rend.sortingOrder = m_Order.intValue; } }
public override void OnInspectorGUI() { // Update the serializedProperty - always do this in the beginning of OnInspectorGUI. serializedObject.Update(); //Create fields for each SerializedProperty EditorGUILayout.PropertyField(m_Name, new GUIContent("Name")); EditorGUILayout.PropertyField(m_Order, new GUIContent("Order")); //Update the name and order of the Renderer properties CheckRenderer();
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI. serializedObject.ApplyModifiedProperties(); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.