Created
March 7, 2016 17:02
-
-
Save sugi-cho/febc795775681aae09fc to your computer and use it in GitHub Desktop.
設定をプレイ中に変更したり、jsonに書き込んだり、読み込んだり
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
static class Extensions | |
{ | |
public static void LoadJsonFile<T> (T overwriteTarget, string filePath = "appData.json") | |
{ | |
var path = Path.Combine (Application.streamingAssetsPath, filePath); | |
if (File.Exists (path)) | |
JsonUtility.FromJsonOverwrite (File.ReadAllText (path), overwriteTarget); | |
else | |
SaveJsonFile (overwriteTarget, filePath); | |
} | |
public static void SaveJsonFile<T> (T obj, string filePath = "appData.json") | |
{ | |
var json = JsonUtility.ToJson (obj); | |
var path = Path.Combine (Application.streamingAssetsPath, filePath); | |
var dPath = Path.GetDirectoryName (path); | |
if (!Directory.Exists (dPath)) | |
Directory.CreateDirectory (dPath); | |
using (var writer = new StreamWriter (path)) | |
writer.Write (json); | |
#if UNITY_EDITOR | |
UnityEditor.AssetDatabase.Refresh (); | |
#endif | |
} | |
} |
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System; | |
using System.Linq; | |
public class MySettingManager : MonoBehaviour | |
{ | |
public static void AddSettingMenu (Setting setting, string filePath) | |
{ | |
setting.LoadSettingFromFile (filePath); | |
if (!Instance.settings.Contains (setting)) { | |
Instance.settings.Add (setting); | |
Instance.settings = Instance.settings.OrderBy (b => b.filePath).ToList (); | |
} | |
} | |
#region instance | |
public static MySettingManager Instance { | |
get { | |
if (_Instance == null) | |
_Instance = new GameObject ("SettingManager").AddComponent<MySettingManager> (); | |
return _Instance; | |
} | |
} | |
static MySettingManager _Instance; | |
#endregion | |
public static KeyCode EditKey = KeyCode.E; | |
List<Setting> settings = new List<Setting> (); | |
Setting currentSetting; | |
bool edit; | |
Rect windowRect = Rect.MinMaxRect (0, 0, Math.Min (Screen.width, 1024f), Math.Min (Screen.height, 768f)); | |
Vector2 scroll; | |
// Update is called once per frame | |
void Update () | |
{ | |
if (Input.GetKeyDown (EditKey)) { | |
edit = !edit; | |
Cursor.visible = edit; | |
} | |
} | |
void OnGUI () | |
{ | |
if (!edit) | |
return; | |
windowRect = GUI.Window (0, windowRect, OnWindow, "Settings"); | |
} | |
void OnWindow (int id) | |
{ | |
scroll = GUILayout.BeginScrollView (scroll); | |
foreach (var setting in settings) { | |
if (setting.edit) { | |
GUILayout.BeginHorizontal (); | |
GUILayout.Space (16f); | |
GUILayout.BeginVertical (); | |
GUILayout.Label (setting.filePath); | |
setting.OnGUIFunc (); | |
GUILayout.BeginHorizontal (); | |
if (GUILayout.Button ("Save and Close")) | |
setting.SaveAndClose (); | |
if (GUILayout.Button ("Cancel")) | |
setting.edit = false; | |
GUILayout.EndHorizontal (); | |
GUILayout.EndVertical (); | |
GUILayout.EndHorizontal (); | |
} else if (GUILayout.Button (setting.filePath)) | |
setting.edit = true; | |
} | |
GUILayout.EndScrollView (); | |
GUI.DragWindow (); | |
} | |
[System.Serializable] | |
public abstract class Setting | |
{ | |
public string filePath{ get; set; } | |
public bool edit{ get; set; } | |
public abstract void OnGUIFunc (); | |
public void LoadSettingFromFile (string path) | |
{ | |
filePath = path; | |
Extensions.LoadJsonFile (this, filePath); | |
OnLoad (); | |
} | |
public void SaveAndClose () | |
{ | |
Extensions.SaveJsonFile (this, filePath); | |
edit = false; | |
OnClose (); | |
} | |
public void CancelAndClose () | |
{ | |
Extensions.LoadJsonFile (this, filePath); | |
edit = false; | |
OnClose (); | |
} | |
protected virtual void OnLoad () | |
{ | |
} | |
protected virtual void OnClose () | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment