Jump to content
Unity Insider Forum

Editor


Kojote

Recommended Posts

Grüße!

Mal eine Frage zum Editor bzw. zum Inspector. Ich hatte mal in einem Script gesehen, dass, wenn man bei einem Script im Inspector ein Häkchen setzt, weitere Felder im Script angezeigt wurden. Häkchen entfernt und sie wurden wieder versteckt.

Wie funktioniert das?

EDIT:

Hab jetzt gerade folgendes durch Zufall gefunden:

https://answers.unity.com/questions/192895/hideshow-properties-dynamically-in-inspector.html

Habs nun mal genau abgeschrieben und funktioniert nicht.^^ Editor-Script liegt im Editor Ordner unter Assets.

[CustomEditor(typeof(LD_SP_Food))]
public class HideFieldEditor : Editor {
    void OnInspectorGUI() {
        var script = target as LD_SP_Food;

        script.isCadaver = GUILayout.Toggle(script.isCadaver, "Flag");

        if (script.isCadaver)
            script.ID = EditorGUILayout.IntField("I field:", script.ID);

    }
}

EDIT 2:

Ich bekomm das Script oben nicht zum laufen, keine Ahnung an was es liegt. Hab aber was anderes vom User "DMGregory" gefunden:

https://gamedev.stackexchange.com/questions/157127/how-to-make-a-property-visible-in-the-inspector-window-if-another-property-is-tr

public bool isLightOn = false;

[ConditionalProperty("isLightOn")]
public float brightness;
public class ConditionalPropertyAttribute : PropertyAttribute {

    public string condition;

    public ConditionalPropertyAttribute(string condition) {
        this.condition = condition;
    }
}
using UnityEditor;

[CustomPropertyDrawer(typeof(ConditionalPropertyAttribute))]
public class ConditionalPropertyDrawer : PropertyDrawer {

    // Determine whether this field should be visible.
    // (We could probably do some caching here...)
    bool ShouldShow(SerializedProperty property) {
        var conditionAttribute = (ConditionalPropertyAttribute)attribute;
        string conditionPath = conditionAttribute.condition;

        // If this property is defined inside a nested type 
        // (like a struct inside a MonoBehaviour), look for
        // our condition field inside the same nested instance.
        string thisPropertyPath = property.propertyPath;
        int last = thisPropertyPath.LastIndexOf('.');
        if (last > 0) {            
            string containerPath = thisPropertyPath.Substring(0, last + 1);
            conditionPath = containerPath + conditionPath;
        }

        // Get the SerializedProperty representing the field that is our criterion.
        var conditionProperty = property.serializedObject.FindProperty(conditionPath);

        // For now, we'll only support bool criteria, and default to visible if there's a problem.
        if (conditionProperty == null || conditionProperty.type != "bool")
            return true;

        // Use the condition property's boolean value to drive visibility.
        return conditionProperty.boolValue;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
        if(ShouldShow(property))
            EditorGUI.PropertyField(position, property, label, true);
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
        if (ShouldShow(property)) {            
            // Provision the normal vertical spacing for this control.
            return EditorGUI.GetPropertyHeight(property, label, true);
        } else {
            // Collapse the unseen derived property.
            return -EditorGUIUtility.standardVerticalSpacing;            
        }
    }
}

Grüße von Kojote

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...