もしタム~もしもタムロチェリーが牝馬三冠をとっていたら~

スマホアプリゲーム「リアタイ競馬道」の開発者ブログです

老いてはGPTに従え

  • アプリ再開時に再ログインさせるよう修正
  • ガチャ結果に各馬の生年を表示するように修正
  • 日付が変わるタイミングでサーバーとデータを同期するように実装
  • 任意の時間にしてデバッグができるように修正
  • 出馬表のデザインを修正(途中)

DateTime型はOdinアセットを使用しても、そのままではUnityのインスペクターに展開できない。
これまでは別に文字列型のプロパティを作成してDateTime型を変換して表示させていたのだけど、Chat-GPTに聞いてみたらもっとスマートな方法を教えてくれた。

#if UNITY_EDITOR
using System;
using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEngine;

public class DateTimeDrawer : OdinValueDrawer<DateTime>
{
    protected override void DrawPropertyLayout(GUIContent label)
    {
        DateTime dateTime = this.ValueEntry.SmartValue;
        string dateTimeString = dateTime.ToString("yyyy-MM-dd HH:mm:ss");

        EditorGUI.BeginChangeCheck();
        dateTimeString = EditorGUILayout.TextField(label, dateTimeString);
        if (EditorGUI.EndChangeCheck())
        {
            if (DateTime.TryParse(dateTimeString, out DateTime parsedDateTime))
            {
                this.ValueEntry.SmartValue = parsedDateTime;
            }
        }
    }
}
#endif

List<DateTime>を展開するには以下。

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEngine;

public class DateTimeListDrawer : OdinValueDrawer<List<DateTime>>
{
    protected override void DrawPropertyLayout(GUIContent label)
    {
        var list = this.ValueEntry.SmartValue;

        EditorGUI.BeginChangeCheck();
        for (int i = 0; i < list.Count; i++)
        {
            string dateTimeString = list[i].ToString("yyyy-MM-dd HH:mm:ss");
            dateTimeString = EditorGUILayout.TextField($"Element {i}", dateTimeString);
            if (DateTime.TryParse(dateTimeString, out DateTime parsedDateTime))
            {
                list[i] = parsedDateTime;
            }
        }
        if (EditorGUI.EndChangeCheck())
        {
            this.ValueEntry.SmartValue = list;
        }

        if (GUILayout.Button("Add Element"))
        {
            list.Add(DateTime.Now);
            this.ValueEntry.SmartValue = list;
        }
    }
}
#endif

こんな感じのソースコード

Projectの適当なフォルダにぶちこんでやるだけ。


有能!