Описание
Порядок визуализации в слое сортировки.
Вы можете группировать игровые объекты по слоям в их компоненте SpriteRenderer. Это называется Слой сортировки. Порядок сортировки определяет приоритет каждого игрового объекта для средства визуализации в каждом слое сортировки. Чем меньше число, которое вы ему даете, тем дальше появляется GameObject. Чем выше число, тем ближе GameObject смотрит к камере. Это очень эффективно при создании игр с 2D-скроллером, так как вы можете захотеть, чтобы определенные игровые объекты находились на одном слое, но некоторые части отображались поверх других, например, слои облаков и их отображение на фоне неба.
Примечание. Значение должно быть в диапазоне от -32768 до 32767.
//Attach a script like this to a SpriteGameObject (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 SerializedPropertyEditorGUILayout.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();
}
}