Kodlanabilir Nesnelerde Koşullu Değişkenler


10

Kullanırken ScriptableObjects, bazı değişkenleri nasıl koşullu hale getirebilirim?

Örnek Kod:

[System.Serializable]
public class Test : ScriptableObject
{
      public bool testbool;
      public string teststring;
      public int testint;
}

Hedef: Ne zaman testbool == truesonra teststringdüzenlemeye kullanılabilir, ne zaman testbool == falsesonra testintdiğeri "ise düzenlemeye kullanılabilir gri ".

Yanıtlar:


7

Editör dostu yol bir "özel denetçi" dir. Unity API terimlerinde, bu, Editor sınıfını genişletmek anlamına gelir .

İşte çalışan bir örnek, ancak yukarıdaki doc bağlantısı size birçok ayrıntı ve ek seçenekte yol gösterecektir:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test))]
public class TestEditor : Editor
{
    private Test targetObject;

    void OnEnable()
    {
        targetObject = (Test) this.target;
    }

    // Implement this function to make a custom inspector.
    public override void OnInspectorGUI()
    {
        // Using Begin/End ChangeCheck is a good practice to avoid changing assets on disk that weren't edited.
        EditorGUI.BeginChangeCheck();

        // Use the editor auto-layout system to make your life easy
        EditorGUILayout.BeginVertical();
        targetObject.testBool = EditorGUILayout.Toggle("Bool", targetObject.testBool);

        // GUI.enabled enables or disables all controls until it is called again
        GUI.enabled = targetObject.testBool;
        targetObject.testString = EditorGUILayout.TextField("String", targetObject.testString);

        // Re-enable further controls
        GUI.enabled = true;

        targetObject.testInt = EditorGUILayout.IntField("Int", targetObject.testInt);

        EditorGUILayout.EndVertical();

        // If anything has changed, mark the object dirty so it's saved to disk
        if(EditorGUI.EndChangeCheck())
            EditorUtility.SetDirty(target);
    }
}

Bu komut dosyasının Yalnızca Editör API'larını kullandığını ve bu nedenle Editör adlı bir klasöre yerleştirilmesi gerektiğini unutmayın. Yukarıdaki kod, müfettişinizi aşağıdakilere dönüştürecektir:

resim açıklamasını buraya girin

Editör komut dosyası yazma konusunda daha rahat olana kadar bu sizi harekete geçirir.


4
[System.Serializable]
public class Test : ScriptableObject
{
    private bool testbool;
    public string teststring;
    public int testint;

    public string TestString 
    {
        get 
        {    
            return teststring; 
        }
        set 
        {
            if (testbool)
                teststring = value; 
        }
    }
}

Kesin görünüyor! Test edip rapor vereceğim!
— Valamorde

Bunun yalnızca yanlış bir değeri önleyeceği ve bir koşul olduğunda düzenleme yapılmasını sağlamayacağı anlaşılıyor true.
— Valamorde
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.