Instantly share code, notes, and snippets.

Embed
What would you like to do?
日本語から変数や関数名を生成するエディタ拡張【Unity】【エディタ拡張】
// NameGenerationWindow.cs
// http://kan-kikuchi.hatenablog.com/entry/NameGenerationWindow
//
// Created by kan.kikuchi on 2017.12.10.
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using System;
using System.Collections;
/// <summary>
/// 日本語から変数や関数名を生成するウィンドウ
/// </summary>
public class NameGenerationWindow : EditorWindow {
//変換する日本語と変換後の名前
private string _fromText = "", _toText = "";
//アクセストークンとそれを保存しておくKey
private string _accessToken = "";
private const string ACCESS_TOKEN_SAVE_KEY = "ACCESS_TOKEN_SAVE_KEY";
//翻訳後のJsonを変換するためのクラス
[Serializable]
private class TranslatedInfo{
[SerializeField]
private string translated_text = "";
public string TranslatedText{get{return translated_text;}}
}
//コルーチン実行用用(StartCoroutineが使えないので)
private IEnumerator _enumerator = null;
//=================================================================================
//初期化
//=================================================================================
//メニューからウィンドウを表示
[MenuItem("Window/Name Generation Window")]
public static void Open (){
NameGenerationWindow.GetWindow (typeof(NameGenerationWindow));
}
private void OnEnable (){
//保存してあるアクセストークンを取得
_accessToken = EditorUserSettings.GetConfigValue(ACCESS_TOKEN_SAVE_KEY);
}
//=================================================================================
//表示するGUIの設定
//=================================================================================
private void OnGUI(){
EditorGUILayout.Space();
EditorGUILayout.BeginVertical(GUI.skin.box);
EditorGUILayout.BeginVertical(GUI.skin.box);
ShowTitle("アクセストークン");
//アクセストークンの入力欄を作成
string beforeAccessToken = _accessToken;
_accessToken = EditorGUILayout.TextField(_accessToken);
//アクセストークンを変更したら保存
if(_accessToken != beforeAccessToken){
EditorUserSettings.SetConfigValue(ACCESS_TOKEN_SAVE_KEY, _accessToken);
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
EditorGUILayout.BeginVertical(GUI.skin.box);
ShowTitle("日本語から変数や関数名を生成");
//変換する日本語の入力欄を作成
_fromText = EditorGUILayout.TextField(_fromText);
//日本語が入力されていれば変換ボタン表示
if(!string.IsNullOrEmpty(_fromText)){
//ボタンが押されたら変換
if (GUILayout.Button("変換")){
Convert();
}
}
EditorGUILayout.Space();
//変換された日本語を表示
EditorGUILayout.SelectableLabel(_toText);
//コルーチン実行中
if(_enumerator != null){
while (_enumerator.MoveNext()) {}
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndVertical();
}
//中心にタイトルをラベルで表示
private void ShowTitle(string text){
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label(text);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
//=================================================================================
//変換
//=================================================================================
//変換
private void Convert(){
//リクエスト作成
UnityWebRequest request = UnityWebRequest.Get("https://api.codic.jp/v1/engine/translate.json?text=" + _fromText + "&casing=pascal");
//HTTPヘッダにトークンを設定
request.SetRequestHeader("Authorization", "Bearer " + _accessToken);
//リクエスト送信
_enumerator = SendWebRequest(request, FinishConvert);
}
//変換リクエスト送信
private IEnumerator SendWebRequest(UnityWebRequest request, Action<bool, string> callback) {
//リクエスト送信
yield return request.SendWebRequest();
//リクエストが完了するまで待機
while(!request.isDone){
yield return 0;
}
//エラー判定
bool isError = request.isNetworkError || request.isHttpError;
//エラー時は内容をログで表示
if(isError) {
Debug.LogWarning(request.url + " : " + request.responseCode + " : " + request.error + " : " + request.downloadHandler.text);
}
//コールバック実行
callback(!isError, isError ? request.error : request.downloadHandler.text);
}
//変換完了
private void FinishConvert(bool isSuccess, string json){
_enumerator = null;
//変換失敗時は空文字を設定
if(!isSuccess){
_toText = "";
return;
}
//jsonの両サイドの[]を削除
json = json.Substring(1, json.Length - 2);
//Jsonを変換し、翻訳後のテキストを取得
_toText = JsonUtility.FromJson<TranslatedInfo>(json).TranslatedText;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment