LoginSignup
2
0

More than 3 years have passed since last update.

UnityでTOMLファイルを扱う

Last updated at Posted at 2020-11-25

概要

Unityの趣味プロジェクトでTOMLファイルの読み込みをしたくなったので、TOMLファイルを取り扱えるライブラリをインポートしたときの作業ログです。
Unityや.NET周りの開発は完全に素人で、手探りで進めているため、不必要にまわりくどい方法をとっていたりもするかと思います。
そのような点を見つけましたら、指摘して貰えると喜びます。

バージョン

  • Unity 2019.4.15f1

ログ

ライブラリのインポート

NettというTOMLファイルをパースできるライブラリがあるので、これをUnityから使えるようにしたい。
https://github.com/paiden/Nett
.NETのライブラリなので.NET用のパッケージマネージャであるNuGetでインストールする。
https://www.nuget.org/
NuGetにはUnity用の実装があるのでこれを使う。
https://github.com/GlitchEnzo/NuGetForUnity
README上のリンク
https://github.com/GlitchEnzo/NuGetForUnity/releases
から最新のリリースのunitypackageファイルをダウンロードしてきて、UnityのAssets>Import Package>Custom Package...からインポートする。
UnityのツールバーにNuGetが追加されるので、NuGet>Manage NuGet PakagesからNettを探してインストール。

Disabling WSA platform on asset settings for ThisProjectPath\Assets\packages.config

なるWarningが出たが現状問題ないのでとりあえず無視。

ファイル読み込み

NettのリポジトリのREADMEのDocumentationが工事中で情報がないので、APIの情報を探す。
ここにまとまっていたので、参考にしつつファイルを読み込む。
プロジェクトのルートディレクトリにinput/test_input.tomlを作成。中身は下記

test_input.toml
title = "Input file example"
Test.cs
using UnityEngine;
using System.Collections;
using Nett;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        // read input file
        string input_file_path = Application.dataPath + "/../input/test_input.toml";
        TomlTable root = Toml.ReadFile(input_file_path);
        string title = root.Get<string>("title");
        Debug.Log("The input file title is " + title);
    }
}

Unityで実行してコンソールに適切に出力されていることを確認。

The input file title is Input file example
UnityEngine.Debug:Log(Object)
Test:Start() (at Assets/Test.cs:8)

他に参考にした記事

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0