2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【メモ】作業補助、メモエディター作ってみる

Posted at

はじめに

めんどくさがりでよくメモ帳に記録を残したりするのですが、いちいちUnityと切り替えて表示するのがめんどうなのと複数開いたりするとファイル名が省略されてどれか判断ができないのでエディターで作ってやろと思いつくります。

処理

1. フォルダ内のファイルを読み込み

内容
・Listの中をリセットします。
・フォルダのパスが空または""が入ってる場合、デフォルトのパスを用意します。
・フォルダが存在する場合は、フォルダ配下にある"_memo.txt"のファイル名をリストに入れます。
・フォルダが存在しない場合は、フォルダを生成します。

void LoadTextFile()
 fileList.Clear();
 if (string.IsNullOrEmpty(folderPath))
 {
     folderPath = Path.Combine(Application.dataPath, "Memo");
 }
 if (Directory.Exists(folderPath))
 {
     string[] files = Directory.GetFiles(folderPath, "*_memo.txt");
     foreach (string file in files)
     {
         fileList.Add(Path.GetFileName(file));
     }
     Debug.Log($"ファイル読み込み完了");
 }
 else
 {
     Directory.CreateDirectory(folderPath);
     Debug.Log($"フォルダを生成しました。({folderPath})");
 }

2. 初期処理

内容
・一度だけ処理を走らせます。
・リストを初期化します。
・フォルダ内のファイル名をリストに入れます。
・二度も処理が走らないようにtrueに変更します。

void OnGUI()
if (!isInitialized)
{
    fileList = new List<string>();
    LoadTextFile();
    isInitialized = true;
}

3. ファイルを読み込む

内容
・作業ファイルのパスが設定されていない場合、フォルダとファイル名をパスに設定します。
・ファイルが存在する場合、ファイルを読み込みます。
・ファイルが存在しない場合、ログを表示します。

void LoadFile()
if (string.IsNullOrEmpty(workfilePath))
{
    workfilePath = Path.Combine(folderPath, fileName);
}
if (File.Exists(workfilePath))
{
    fileContent = File.ReadAllText(workfilePath);
}
else
{
    Debug.Log($"ファイルが存在しません。({workfilePath})");
}

4.ファイル情報設定

内容
・フォルダパスの変更ができるようにします。
・リスト更新ボタンが押されたら、ファイル名をリストに追加します。
・リストにアイテムが存在する場合、ファイル名をポップアップで表示します。
・リストにアイテムが存在する場合に表示されるUIは横並びで表示します。
・選択が変更された場合、作業パスを変更します。
・選択を変更された時にファイルを開いていたら、現在選択しているファイルを開きます。
・ファイルを開くボタンが押されたら、ファイルを表示状態にてファイルを開きます。
・リストにアイテムが存在しない場合、ヘルプ文字を表示します。

void OnGUI()
folderPath = EditorGUILayout.TextField("フォルダパス", folderPath);
if (GUILayout.Button("リスト更新"))
{
    LoadTextFile();
}
if (fileList.Count > 0)
{
    EditorGUILayout.BeginHorizontal();
    selectedIndex = EditorGUILayout.Popup("ファイル選択", selectedIndex, fileList.ToArray());
    if(b_selectedIndex != selectedIndex)
    {
        b_selectedIndex = selectedIndex;
        workfilePath = Path.Combine(folderPath, fileList[selectedIndex]);
        if(isOpenFile)
        {
            LoadFile();
        }
    }
    if (GUILayout.Button("ファイルを開く"))
    {
        isOpenFile = true;
        LoadFile();
    }
    EditorGUILayout.EndHorizontal();
}
else
{
    EditorGUILayout.HelpBox("ファイルがありません", MessageType.Warning);
}

5.ファイル追加

内容
・UIは横並びにします。
・ファイル名を変更できるようにします。
・新規追加ボタンが押されたら、ファイル名に"_memo"と".txt"があるか確認します。ない場合は付け加えます。
・ファイルを追加する前にフォルダが存在するか確認を行い、ない場合は生成します。
・作業パスを変更します。
・指定のパスに上書き保存で文字コードUTF-8のファイルを生成し、データをファイルに書き出して、リソースを解放します。
・コンパイルを促します。

void OnGUI()
EditorGUILayout.BeginHorizontal();
fileName = EditorGUILayout.TextField("ファイル名", fileName);
if (GUILayout.Button("新規追加"))
{
    if (!fileName.EndsWith("_memo"))
    {
        fileName += "_memo";
    }
    if (!fileName.EndsWith(".txt"))
    {
        fileName += ".txt";
    }
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }
    workfilePath = Path.Combine(folderPath, fileName);
    StreamWriter sw = new StreamWriter(workfilePath, false, System.Text.Encoding.UTF8);
    sw.Flush();
    sw.Close();
    LoadTextFile();
    AssetDatabase.Refresh();
}
EditorGUILayout.EndHorizontal();

6.ファイル表示

内容
・表示モード且つリストにアイテムが存在する場合、処理を行います。
・UIは縦に並べて表示します。
・現在開いているファイルパウを表示します。
・ファイルの中身を表示します。
・保存ボタんが押されたら、作業パスのファイルにファイルの中身を上書きします。

void OnGUI()
if (isOpenFile && fileList.Count > 0)
{
    EditorGUILayout.BeginVertical();

    EditorGUILayout.LabelField("表示中のファイル: ", Path.GetFileName(workfilePath));
    fileContent = EditorGUILayout.TextArea(fileContent, GUILayout.Height(180));
    if (GUILayout.Button("保存"))
    {
        File.WriteAllText(workfilePath, fileContent);
        Debug.Log($"ファイルを保存しました。({workfilePath})");
    }

    EditorGUILayout.EndVertical();
}

おわり

修正できるところはいくつかあるので修正して使いやすくしたいです。

最後に全コードです。

MemoEditor
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class MemoEditor : EditorWindow
{
    private string folderPath;
    private string fileName = "default_memo";
    private string fileContent;
    private string workfilePath;

    private int selectedIndex = 0;
    private int b_selectedIndex = 0;
    private List<string> fileList = new List<string>();

    private bool isInitialized = false;
    private bool isOpenFile = false;

    [MenuItem("MyTool/Memo")]
    private static void ShowWindow()
    {
        var window = GetWindow<MemoEditor>("MemoEditor");
        window.minSize = new Vector2(600, 400);
        window.Show();
    }

    private void OnGUI()
    {
        // 初期処理
        if (!isInitialized)
        {
            fileList = new List<string>();
            LoadTextFile();
            isInitialized = true;
        }

        // ファイル読み込み
        SetthingFileData();

        // ファイル生成
        NewFile();

        // ファイル表示
        OpenFile();
    }

    // フォルダ内のファイル読み込み
    void LoadTextFile()
    {
        fileList.Clear();
        if (string.IsNullOrEmpty(folderPath))
        {
            folderPath = Path.Combine(Application.dataPath, "Memo");
        }
        if (Directory.Exists(folderPath))
        {
            string[] files = Directory.GetFiles(folderPath, "*_memo.txt");
            foreach (string file in files)
            {
                fileList.Add(Path.GetFileName(file));
            }
            Debug.Log($"ファイル読み込み完了");
        }
        else
        {
            Directory.CreateDirectory(folderPath);
            Debug.Log($"フォルダを生成しました。({folderPath})");
        }
    }

    // ファイル読み込み
    void LoadFile()
    {
        if (string.IsNullOrEmpty(workfilePath))
        {
            workfilePath = Path.Combine(folderPath, fileName);
        }
        if (File.Exists(workfilePath))
        {
            fileContent = File.ReadAllText(workfilePath);
        }
        else
        {
            Debug.Log($"ファイルが存在しません。({workfilePath})");
        }
    }

    // ファイル情報設定
    void SetthingFileData()
    {
        folderPath = EditorGUILayout.TextField("フォルダパス", folderPath);
        if (GUILayout.Button("リスト更新"))
        {
            LoadTextFile();
        }
        if (fileList.Count > 0)
        {
            EditorGUILayout.BeginHorizontal();
            selectedIndex = EditorGUILayout.Popup("ファイル選択", selectedIndex, fileList.ToArray());
            if(b_selectedIndex != selectedIndex)
            {
                b_selectedIndex = selectedIndex;
                workfilePath = Path.Combine(folderPath, fileList[selectedIndex]);
                if (isOpenFile)
                {
                    LoadFile();
                }
            }
            if (GUILayout.Button("ファイルを開く"))
            {
                isOpenFile = true;
                LoadFile();
            }
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.HelpBox("ファイルがありません", MessageType.Warning);
        }
    }

    // ファイル追加
    void NewFile()
    {
        EditorGUILayout.BeginHorizontal();
        fileName = EditorGUILayout.TextField("ファイル名", fileName);
        if (GUILayout.Button("新規追加"))
        {
            if (!fileName.EndsWith("_memo"))
            {
                fileName += "_memo";
            }
            if (!fileName.EndsWith(".txt"))
            {
                fileName += ".txt";
            }
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            workfilePath = Path.Combine(folderPath, fileName);
            StreamWriter sw = new StreamWriter(workfilePath, false, System.Text.Encoding.UTF8);
            sw.Flush();
            sw.Close();
            LoadTextFile();
            AssetDatabase.Refresh();
        }
        EditorGUILayout.EndHorizontal();
    }

    // テキスト表示
    void OpenFile()
    {
        if (isOpenFile && fileList.Count > 0)
        {
            EditorGUILayout.BeginVertical();

            EditorGUILayout.LabelField("表示中のファイル: ", Path.GetFileName(workfilePath));
            fileContent = EditorGUILayout.TextArea(fileContent, GUILayout.Height(180));
            if (GUILayout.Button("保存"))
            {
                File.WriteAllText(workfilePath, fileContent);
                Debug.Log($"ファイルを保存しました。({workfilePath})");
            }

            EditorGUILayout.EndVertical();
        }
    }
}
2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?