[DB-AL]Easy Editor Windows in Unity with Serialized Properties

Pour les scripts écrits en C#
Règles du forum
Merci de respecter la NOMENCLATURE suivante pour vos TITRES de messages :

Commencez par le niveau de vos scripts
DB = Débutant
MY = Moyen
CF = Confirmé

Puis le domaine d'application
-RS = Réseau
-AL = Algorithmie

Exemple :

[DB-RS] Mouvement perso multijoueur
Malavia
Messages : 22
Inscription : 09 Juil 2019 17:06

[DB-AL]Easy Editor Windows in Unity with Serialized Properties

Message par Malavia » 29 Mars 2021 15:48

Bonjour,

j'essaye de refaire le code de cette vidéo https://www.youtube.com/watch?v=c_3DXBrH-Is qui génère dynamiquement toute la partie GUI à partir d'un script.

à 6:25, il affiche tout un tas d'information dans la fenêtre alors que la mienne reste vide.
On ne voit jamais dans la vidéo ce qui est lu par le programme. Mon scriptableObject ressemble à

Code : Tout sélectionner

[CreateAssetMenu(fileName = "GameDataObject", menuName = "test/GameDataObject", order = 1)]

[System.Serializable]
public class GameDataObject : ScriptableObject
{
	public List<Item> items = new List<Item>();
}

[System.Serializable]
public class Item : ScriptableObject
{
    [SerializeField]
    private string m_Id;

    public string Id
    {
        get { return this.m_Id; }
        set { this.m_Id = value; }
    }

    [SerializeField]
    private string m_ItemName = "New Item";

    public string Name
    {
        get { return this.m_ItemName; }
        set { this.m_ItemName = value; }
    }
}

quand au code de la vidéo, le voici :

Code : Tout sélectionner

using UnityEditor;

public class GameDataObjectEditorWindow : ExtendedEditorWindow
{
    public static void Open(GameDataObject dataObject)
    {
        GameDataObjectEditorWindow window = GetWindow<GameDataObjectEditorWindow>("Game Data Editor");
        window.serializedObject = new SerializedObject(dataObject);
    }

    private void OnGUI()
    {
        currentProperty = serializedObject.FindProperty("items");
        DrawProperties(currentProperty, true);
    }
}
J'ai juste changé ce qu'il y avait dans FindProperty par items

Code : Tout sélectionner

using UnityEditor;
using UnityEditor.Callbacks;

public class AssetHandler
{
    [OnOpenAsset()]
    public static bool OpenEditor(int instanceId, int line)
    {
        GameDataObject obj = EditorUtility.InstanceIDToObject(instanceId) as GameDataObject;
        if (obj != null)
        {
            GameDataObjectEditorWindow.Open(obj);
            return true;
        }
        return false;
    }
}
[CustomEditor(typeof(GameDataObject))]
public class GameDataObjectCustomEditor : Editor
{
    public override void OnInspectorGUI()
    {
        if (GUILayout.Button("Open Editor"))
        {
            GameDataObjectEditorWindow.Open((GameDataObject)target);
        }
    }
}

Code : Tout sélectionner

using UnityEditor;

public class ExtendedEditorWindow : EditorWindow
{
    protected SerializedObject serializedObject;
    protected SerializedProperty currentProperty;

    protected void DrawProperties(SerializedProperty prop, bool drawChildren)
    {
        string lastPropPath = string.Empty;
        foreach(SerializedProperty p in prop)
        {
            if (p.isArray && p.propertyType == SerializedPropertyType.Generic)
            {
                EditorGUILayout.BeginHorizontal();
                p.isExpanded = EditorGUILayout.Foldout(p.isExpanded, p.displayName);
                EditorGUILayout.EndHorizontal();

                if (p.isExpanded)
                {
                    EditorGUI.indentLevel++;
                    DrawProperties(p, drawChildren);
                    EditorGUI.indentLevel--;
                }
            }
            else
            {
                if(!string.IsNullOrEmpty(lastPropPath) && p.propertyPath.Contains(lastPropPath)) { continue; }
                lastPropPath = p.propertyPath;
                EditorGUILayout.PropertyField(p, drawChildren);
            }
        }
    }
}
ps ; Mes connaissances dans Unity et c# sont parsemés d’incompréhension..
Merci pour votre aide.

Répondre

Revenir vers « (C#) CSharp »