स्क्रिप्ट करने योग्य वस्तुओं में सशर्त चर


10

उपयोग करते समय ScriptableObjects, मैं कुछ चर को सशर्त कैसे बना सकता हूं?

उदाहरण कोड:

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

लक्ष्य: जब testbool == trueतब teststringसंपादित करने के लिए उपलब्ध होता है, जब testbool == falseतब testintसंपादित करने के लिए उपलब्ध होता है जबकि दूसरा " ग्रे आउट " होता है।

जवाबों:


7

संपादक के अनुकूल मार्ग एक "कस्टम निरीक्षक" है। यूनिटी एपीआई के संदर्भ में, इसका मतलब है संपादक वर्ग का विस्तार ।

यहाँ एक कार्यशील उदाहरण दिया गया है, लेकिन ऊपर दिया गया डॉक लिंक आपको बहुत सारे विवरणों और अतिरिक्त विकल्पों के माध्यम से आगे बढ़ाएगा:

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

ध्यान रखें कि यह स्क्रिप्ट संपादक-केवल API का उपयोग करती है, इसलिए इसे संपादक नाम के फ़ोल्डर में रखा जाना चाहिए। उपरोक्त कोड आपके निरीक्षक को निम्नलिखित में बदल देगा:

यहाँ छवि विवरण दर्ज करें

जब तक आप संपादक पटकथा के साथ अधिक सहज नहीं हो जाते तब तक आपको रोल करना चाहिए।


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

यह सटीक लग रहा है! मैं परीक्षण करूंगा और वापस रिपोर्ट करूंगा!
— वैलमॉर्डे

ऐसा प्रतीत होता है कि यह केवल एक गलत मान को रोकेगा और एक शर्त के दौरान इसे संपादित करने के लिए उपलब्ध नहीं है true।
— वेलमॉर्डे
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.