Skip to content

Instantly share code, notes, and snippets.

@LuizMoratelli
Created August 12, 2024 13:56
Show Gist options
  • Save LuizMoratelli/0095e61a4dceb75d706bc30c29723ee9 to your computer and use it in GitHub Desktop.
Save LuizMoratelli/0095e61a4dceb75d706bc30c29723ee9 to your computer and use it in GitHub Desktop.
[Odin] Card preview

Creating a Card Preview with Odin

1. Creating a PreviewCardScene

using UnityEditor.SceneManagement;
using UnityEngine;

public class PreviewCardScene : PreviewSceneStage
{
    protected override GUIContent CreateHeaderContent()
    {
        return new GUIContent("Preview Card");
    }
}

2. Add preview feature to Card Editor

using Sirenix.OdinInspector.Editor;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using TcgEngine;
using Sirenix.Utilities.Editor;
using Sirenix.OdinInspector.Demos.RPGEditor;
using Sirenix.OdinInspector;
using System.Reflection;
using Sirenix.Utilities;
using Unity.VisualScripting;
using UnityEngine.WSA;
using UnityEditor.SceneManagement;
using TcgEngine.UI;

public class CardEditor : OdinMenuEditorWindow
{
    public UnityEngine.Object prefab;
    public UnityEngine.Object variant;
    public PreviewCardScene scene;

    private void Update()
    {
        if (scene != null && variant != null && MenuTree?.Selection?.SelectedValue != null)
        {
            var selected = MenuTree.Selection.SelectedValue as CardData;
            var card_preview = scene.scene.GetRootGameObjects()[0].GetComponent<CardUI>();
            card_preview.SetCard(selected, variant as VariantData);
        }   
    }
    
    protected override void OnBeginDrawEditors()
    {
        if (MenuTree == null) return;

        prefab = EditorGUILayout.ObjectField(prefab, typeof(GameObject), false);
        variant = EditorGUILayout.ObjectField(variant, typeof(VariantData), false);
        
        SirenixEditorGUI.BeginHorizontalToolbar(MenuTree.Config.SearchToolbarHeight);
        {
            //...
            if (SirenixEditorGUI.ToolbarButton(new GUIContent("Preview")))
            {
                scene = new PreviewCardScene();
                StageUtility.GoToStage(scene, true);
                var go = Instantiate(prefab);
                go.AddComponent<Canvas>();
                EditorSceneManager.MoveGameObjectToScene(go as GameObject, scene.scene);
            }
        }
    }
}

3. Result

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment