Skip to content

Instantly share code, notes, and snippets.

@urahimono
Last active October 17, 2017 23:03
Show Gist options
  • Save urahimono/66d290f63ff5c0c45beb9239b2147eb3 to your computer and use it in GitHub Desktop.
Save urahimono/66d290f63ff5c0c45beb9239b2147eb3 to your computer and use it in GitHub Desktop.
【Unity】JsonEditorの作成
//------------------------------------------------------------------------
//
// (C) Copyright 2017 Urahimono Project Inc.
//
//------------------------------------------------------------------------
using UnityEngine;
[System.Serializable]
public class JsonData
{
[SerializeField]
public PersonalData[] party = null;
}
[System.Serializable]
public class PersonalData
{
[SerializeField]
public string name = string.Empty;
[SerializeField, Range(1, 100)]
public int life = 1;
[SerializeField, Range(0, 50)]
public int attack = 0;
[SerializeField]
public AccessoryData[] accessories = null;
} // class PersonalData
[System.Serializable]
public struct AccessoryData
{
[SerializeField]
public string name;
[SerializeField, Range(0, 10)]
public int defense;
} // AccessoryData
//------------------------------------------------------------------------
//
// (C) Copyright 2017 Urahimono Project Inc.
//
//------------------------------------------------------------------------
using UnityEngine;
using UnityEditor;
public class JsonEditorWindow : ScriptableWizard
{
[SerializeField]
private JsonData m_jsonData = null;
[SerializeField]
private JsonScriptableObject m_jsonScriptableObject = null;
private static readonly string SAVE_ASSET_PATH = "Assets/Editor/JsonScriptableObject.asset";
[MenuItem( "Tool/JsonEditor" )]
public static void Open()
{
var window = DisplayWizard<JsonEditorWindow>( "JsonEditor", "Save", "Load" );
var saveAsset = AssetDatabase.LoadAssetAtPath<JsonScriptableObject>( SAVE_ASSET_PATH );
// ファイルが存在しないときには作成しよう。
if( saveAsset == null )
{
saveAsset = CreateInstance<JsonScriptableObject>();
AssetDatabase.CreateAsset( saveAsset, SAVE_ASSET_PATH );
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
window.m_jsonScriptableObject = saveAsset;
window.m_jsonData = saveAsset.Json;
}
/// <summary>
/// パラメータを更新した時に呼ばれるよ!
/// </summary>
private void OnWizardUpdate()
{
if( m_jsonScriptableObject != null )
{
m_jsonScriptableObject.Json = m_jsonData;
EditorUtility.SetDirty( m_jsonScriptableObject );
}
}
/// <summary>
/// ScriptableWizardのメインとなるボタンが押された際に呼ばれるよ!
/// 今回の場合はDisplayWizardの第二引数で指定した"Save"ボタンが押されたとき。
/// </summary>
private void OnWizardCreate()
{
string json = JsonUtility.ToJson( m_jsonData );
json = JsonPrettyPrint( json );
string path = EditorUtility.SaveFilePanel( "名前を付けてJsonを保存しよう", "", "Setting", "json" );
System.IO.File.WriteAllText( path, json );
// プロジェクトフォルダ内に保存された際の対応.
AssetDatabase.Refresh();
}
/// <summary>
/// ScriptableWizardのサブとなるボタンが押された際に呼ばれるよ!
/// 今回の場合はDisplayWizardの第三引数で指定した"Load"ボタンが押されたとき。
/// </summary>
private void OnWizardOtherButton()
{
string path = EditorUtility.OpenFilePanel( "Jsonファイルを開く", "", "json" );
string json = System.IO.File.ReadAllText( path );
JsonData loadedJsonData = null;
// Jsonとは異なるファイルを読んだとき用の対策。
try
{
loadedJsonData = JsonUtility.FromJson<JsonData>( json );
}
catch( System.Exception i_exception )
{
Debug.LogError( i_exception );
loadedJsonData = null;
}
if( loadedJsonData != null )
{
m_jsonData = loadedJsonData;
OnWizardUpdate();
}
}
private string JsonPrettyPrint( string i_json )
{
if( string.IsNullOrEmpty( i_json ) )
{
return string.Empty;
}
i_json = i_json.Replace( System.Environment.NewLine, "" ).Replace( "\t", "" );
System.Text.StringBuilder sb = new System.Text.StringBuilder();
bool quote = false;
bool ignore = false;
int offset = 0;
int indentLength = 3;
foreach( char ch in i_json )
{
switch( ch )
{
case '"':
if( !ignore )
{
quote = !quote;
}
break;
case '\'':
if( quote )
{
ignore = !ignore;
}
break;
}
if( quote )
{
sb.Append( ch );
}
else
{
switch( ch )
{
case '{':
case '[':
sb.Append( ch );
sb.Append( System.Environment.NewLine );
sb.Append( new string( ' ', ++offset * indentLength ) );
break;
case '}':
case ']':
sb.Append( System.Environment.NewLine );
sb.Append( new string( ' ', --offset * indentLength ) );
sb.Append( ch );
break;
case ',':
sb.Append( ch );
sb.Append( System.Environment.NewLine );
sb.Append( new string( ' ', offset * indentLength ) );
break;
case ':':
sb.Append( ch );
sb.Append( ' ' );
break;
default:
if( ch != ' ' )
{
sb.Append( ch );
}
break;
}
}
}
return sb.ToString().Trim();
}
} // class JsonEditorWindow
//------------------------------------------------------------------------
//
// (C) Copyright 2017 Urahimono Project Inc.
//
//------------------------------------------------------------------------
using UnityEngine;
public class JsonScriptableObject : ScriptableObject
{
[SerializeField]
private JsonData m_jsonData = null;
public JsonData Json
{
get { return m_jsonData; }
set { m_jsonData = value; }
}
} // class JsonScriptableObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment