Windows Server
Editor kustom

Harta site

Drawer properti dapat digunakan untuk menyesuaikan tampilan kontrol tertentu di Inspector window dengan menggunakan atribut pada scriptsSepotong kode yang memungkinkan Anda untuk membuat Komponen Anda sendiri, memicu peristiwa permainan, memodifikasi sifat komponen dari waktu ke waktu dan menanggapi input pengguna dengan cara apa pun yang Anda sukai. More info
Lihat di Glossary
Anda, atau dengan mengendalikan bagaimana kelas Serializable tertentu harus terlihat.

Drawer properti memiliki dua kegunaan:

  • Sesuaikan GUI setiap contoh kelas Serializable.
  • Sesuaikan GUI anggota skrip dengan Property Attributes kustom.

Sesuaikan GUI kelas Serializable

Jika Anda memiliki kelas Serializable kustom, Anda dapat menggunakan Property Drawer untuk mengontrol bagaimana terlihat di InspectorJendela Unity yang menampilkan informasi tentang Pengaturan GameObject yang dipilih saat ini, aset atau proyek, memungkinkan Anda untuk memeriksa dan mengedit nilai. More info
Lihat di Glossary
. Pertimbangkan Ingredient kelas Serializable dalam contoh skrip di bawah (Note: Ini bukan script editor. Kelas atribut properti harus ditempatkan dalam file skrip biasa):

C# (example):

using System;
using UnityEngine;

enum IngredientUnit { Spoon, Cup, Bowl, Piece }

// Custom serializable class
[Serializable]
public class Ingredient
{
    public string name;
    public int amount = 1;
    public IngredientUnit unit;
}

public class Recipe : MonoBehaviour
{
    public Ingredient potionResult;
    public Ingredient[] potionIngredients;
}

Menggunakan Drawer Properti kustom, setiap penampilan kelas Bahan dalam Inspektur dapat diubah. Bandingkan tampilan sifat Bahan dalam Inspektur tanpa dan dengan Drawer Properti kustom:

Class in the Inspector without (left) and with (right) custom Property Drawer.
Kelas di Inspektur tanpa (kiri) dan dengan (kanan) Drawer Properti kustom.

Anda dapat melampirkan Drawer Properti ke kelas Serializable dengan menggunakan atribut CustomPropertyDrawer dan lulus dalam jenis kelas Serializable yang merupakan laci untuk.

C# (example):

using UnityEditor;
using UnityEngine;

// IngredientDrawer
[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer
{
    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        var amountRect = new Rect(position.x, position.y, 30, position.height);
        var unitRect = new Rect(position.x + 35, position.y, 50, position.height);
        var nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
        EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
        EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
}

Sesuaikan GUI anggota skrip menggunakan Attribut Properti

Penggunaan lain dari Property Drawer adalah mengubah penampilan anggota dalam skrip yang memiliki Property Attributes kustom. Katakan Anda ingin membatasi float atau integer di skrip Anda ke kisaran tertentu dan tunjukkan sebagai slider di Inspector. Menggunakan built-in PropertyAttribute yang disebut RangeAttribute, Anda dapat melakukannya:

C# (example):

// Show this float in the Inspector as a slider between 0 and 10
[Range(0f, 10f)]
float myFloat = 0f;

Anda dapat membuat PropertyAttribute Anda sendiri juga. Kita akan menggunakan kode untuk RangeAttribute sebagai contoh. Atribut harus memperpanjang kelas PropertyAttribute. Jika Anda ingin, properti Anda dapat mengambil parameter dan menyimpannya sebagai variabel anggota publik.

C# (example):

using UnityEngine;

public class MyRangeAttribute : PropertyAttribute 
{
        readonly float min;
        readonly float max;
        
        void MyRangeAttribute(float min, float max)
        {
            this.min = min;
            this.max = max;
        }
}

Sekarang Anda memiliki atribut, Anda perlu membuat Property Drawer yang menarik properti yang memiliki atribut itu. laci harus memperpanjang kelas PropertyDrawer, dan harus memiliki atribut CustomPropertyDrawer untuk memberitahunya yang atribut itu adalah laci untuk.

Kelas laci properti harus ditempatkan di script editor, di dalam folder yang disebut Editor.

C# (example):

using UnityEditor;
using UnityEngine;

// Tell the MyRangeDrawer that it is a drawer for properties with the MyRangeAttribute.
[CustomPropertyDrawer(typeof(MyRangeAttribute))]
public class RangeDrawer : PropertyDrawer
{
    // Draw the property inside the given rect
    void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // First get the attribute since it contains the range for the slider
        MyRangeAttribute range = (MyRangeAttribute)attribute;

        // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
        if (property.propertyType == SerializedPropertyType.Float)
            EditorGUI.Slider(position, property, range.min, range.max, label);
        else if (property.propertyType == SerializedPropertyType.Integer)
            EditorGUI.IntSlider(position, property, (int) range.min, (int) range.max, label);
        else
            EditorGUI.LabelField(position, label.text, "Use MyRange with float or int.");
    }
}

Perhatikan bahwa untuk alasan kinerja, fungsi EditorGUILayout tidak dapat digunakan dengan Drawer Properti.

Windows Server
Editor kustom