Created
April 3, 2013 09:23
-
-
Save g0ody/5299715 to your computer and use it in GitHub Desktop.
Unity3D Editor Tool to save Settings of GameObjects into a file. Depends on:
MiniJSON(https://gist.github.com/darktable/1411710)
SerializeUtility(https://gist.github.com/g0ody/5299674)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEditor; | |
using UnityEngine; | |
using System.IO; | |
using System.Collections; | |
using System; | |
public class JSONTool : EditorWindow | |
{ | |
[MenuItem("Platogo/JSON Tool")] | |
public static void Init() | |
{ | |
GetWindow<JSONTool>(); | |
} | |
public const string SETTINGS_DIR = "Assets/Settings/"; | |
public const string SETTINGS_TYPE = ".txt"; | |
private Transform target; | |
private string _filename = ""; | |
private string[] _filenames; | |
private int _index; | |
void OnFocus() | |
{ | |
Repaint(); | |
_filenames = GetAchievementFileNames(); | |
} | |
void OnEnable() | |
{ | |
_filenames = GetAchievementFileNames(); | |
} | |
//*************************************** | |
// Inspector | |
//*************************************** | |
void OnGUI() | |
{ | |
EditorGUILayout.BeginVertical(); | |
EditorGUILayout.PrefixLabel("Choose Target:"); | |
target = EditorGUILayout.ObjectField(target, typeof(Transform), true) as Transform; | |
GUI.enabled = target != null; | |
EditorGUILayout.LabelField(" Clipboard:"); | |
EditorGUILayout.BeginHorizontal(); | |
if (GUILayout.Button("Save To")) | |
EditorGUIUtility.systemCopyBuffer = Save(); | |
if (GUILayout.Button("Load From")) | |
Load(EditorGUIUtility.systemCopyBuffer); | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.LabelField(" File:"); | |
EditorGUILayout.BeginHorizontal(); | |
_filename = EditorGUILayout.TextField(_filename); | |
if (GUILayout.Button("Save")) | |
SaveToFile(); | |
EditorGUILayout.EndHorizontal(); | |
if (_filenames.Length > 0) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
_index = EditorGUILayout.Popup(_index, _filenames); | |
if (GUILayout.Button("Load")) | |
LoadFromFile(_filenames[_index]); | |
EditorGUILayout.EndHorizontal(); | |
} | |
EditorGUILayout.EndVertical(); | |
} | |
private void LoadFromFile(string file) | |
{ | |
var ta = AssetDatabase.LoadAssetAtPath(SETTINGS_DIR + file + SETTINGS_TYPE, typeof(TextAsset)) as TextAsset; | |
_filename = file; | |
Load(ta.text); | |
} | |
private void Load(string json) | |
{ | |
SerializeUtility.Deserialize(target, MiniJSON.jsonDecode(json) as Hashtable); | |
} | |
private void SaveToFile() | |
{ | |
File.WriteAllText(SETTINGS_DIR + _filename + SETTINGS_TYPE, Save()); | |
AssetDatabase.ImportAsset(SETTINGS_DIR + _filename + SETTINGS_TYPE, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ImportRecursive); | |
AssetDatabase.Refresh(); | |
_filenames = GetAchievementFileNames(); | |
} | |
private string Save() | |
{ | |
return MiniJSON.jsonEncode(SerializeUtility.Serialize(target)); | |
} | |
public static string[] GetAchievementFileNames() | |
{ | |
if (Directory.Exists(SETTINGS_DIR)) | |
{ | |
var files = Directory.GetFiles(SETTINGS_DIR, "*" + SETTINGS_TYPE); | |
files = Array.ConvertAll(files, f => f.Substring(f.LastIndexOf("/") + 1)); | |
files = Array.ConvertAll(files, f => f.Remove(f.Length - SETTINGS_TYPE.Length)); | |
var fn = new string[files.Length + 1]; | |
fn[0] = "None"; | |
files.CopyTo(fn, 1); | |
return fn; | |
} | |
else | |
return new string[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment