Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge/templatepath #55

Merged
merged 4 commits into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Assets/QuickSheet/Editor/Util/SingletonScriptableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Linq;
using UnityEngine;

namespace UnityQuickSheet
{
/// <summary>
/// Abstract class for making reload-proof singletons out of ScriptableObjects
/// Returns the asset created on editor, null if there is none
/// Based on https://www.youtube.com/watch?v=VBA1QCoEAX4
///
/// See Also:
/// blog page: http://baraujo.net/unity3d-making-singletons-from-scriptableobjects-automatically/
/// gist page: https://gist.github.com/baraujo/07bb162a1f916595cad1a2d1fee5e72d
/// </summary>
/// <typeparam name="T">Type of the singleton</typeparam>

public abstract class SingletonScriptableObject<T> : ScriptableObject where T : ScriptableObject
{
static T _instance = null;
public static T Instance
{
get
{
if (!_instance)
_instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
return _instance;
}
}
}
}
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public int CurrentSheetIndex
[SerializeField]
protected int currentSelectedSheet = 0;

// excel and google plugin have its own template files,
// so we need to set the different path when the asset file is created.
private readonly string excelTemplatePath = "QuickSheet/ExcelPlugin/Templates";

/// <summary>
/// Note: Called when the asset file is created.
/// </summary>

private void Awake() {
TemplatePath = excelTemplatePath;
// excel and google plugin have its own template files,
// so we need to set the different path when the asset file is created.
TemplatePath = ExcelSettings.Instance.TemplatePath;
}

/// <summary>
Expand Down
15 changes: 0 additions & 15 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.asset

This file was deleted.

108 changes: 7 additions & 101 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace UnityQuickSheet
/// <summary>
/// A class manages excel setting.
/// </summary>
public class ExcelSettings : ScriptableObject
[CreateAssetMenu(menuName = "QuickSheet/Setting/Excel Setting")]
public class ExcelSettings : SingletonScriptableObject<ExcelSettings>
{
// A path of default setting file is located.
public static string AssetPath = "Assets/QuickSheet/ExcelPlugin/Editor/";

// A filename of setting .asset file.
public static readonly string AssetFileName = "ExcelSettings.asset";
/// <summary>
/// A default path where .txt template files are.
/// </summary>
public string TemplatePath = "QuickSheet/ExcelPlugin/Templates";

/// <summary>
/// A path where generated ScriptableObject derived class and its data class script files are to be put.
Expand All @@ -33,20 +33,6 @@ public class ExcelSettings : ScriptableObject
/// </summary>
public string EditorPath = string.Empty;

/// <summary>
/// A singleton instance.
/// </summary>
private static ExcelSettings s_Instance = null;

/// <summary>
/// Create new account setting asset file if there is already one then select it.
/// </summary>
[MenuItem("Assets/Create/QuickSheet/Setting/Excel Setting")]
public static void CreateExcelSetting()
{
ExcelSettings.Create();
}

/// <summary>
/// Select currently exist account setting asset file.
/// </summary>
Expand All @@ -56,88 +42,8 @@ public static void Edit()
Selection.activeObject = Instance;
if (Selection.activeObject == null)
{
Debug.LogError(@"No ExcelSetting.asset file is found. Create setting file first. See the menu at 'Assets/Create/QuickSheet/Setting/Excel Setting'.");
Debug.LogError(@"No ExcelSetting.asset file is found. Create setting file first. See the menu at 'Create/QuickSheet/Setting/Excel Setting'.");
}
}

/// <summary>
/// Checks ExcelSetting.asset file exist at the specified path(AssetPath+AssetFileName).
/// </summary>
public bool CheckPath()
{
string file = AssetDatabase.GetAssetPath(Selection.activeObject);
string assetFile = AssetPath + ExcelSettings.AssetFileName;

return (file == assetFile) ? true : false;
}

/// <summary>
/// A property for a singleton instance.
/// </summary>
public static ExcelSettings Instance
{
get
{
if (s_Instance == null)
{
string path = ExcelSettings.AssetPath + ExcelSettings.AssetFileName;
s_Instance = AssetDatabase.LoadAssetAtPath(path, typeof(ExcelSettings)) as ExcelSettings;
if (s_Instance == null)
{
string title = string.Format(@"No {0} is found!", AssetFileName);
string message = string.Format(@"No {0} is found at '{1}'. \n Press 'Create' button to create a default one.", AssetFileName, AssetPath);
bool ok = EditorUtility.DisplayDialog(
title,
message,
"Create",
"Cancel"
);

// create excel setting .asset file if it does not exist under the asset path.
if (ok)
s_Instance = ExcelSettings.Create();
}
}
return s_Instance;
}
}

/// <summary>
/// Create .asset file for excel setting.
/// </summary>
public static ExcelSettings Create()
{
string filePath = CustomAssetUtility.GetUniqueAssetPathNameOrFallback(AssetFileName);
s_Instance = (ExcelSettings)AssetDatabase.LoadAssetAtPath(filePath, typeof(ExcelSettings));

if (s_Instance == null)
{
s_Instance = CreateInstance<ExcelSettings>();

string path = CustomAssetUtility.GetUniqueAssetPathNameOrFallback(AssetFileName);
AssetDatabase.CreateAsset(s_Instance, path);

ExcelSettings.AssetPath = Path.GetDirectoryName(path);
ExcelSettings.AssetPath += "/";

// saves file path of the created asset.
EditorUtility.SetDirty(s_Instance);
AssetDatabase.SaveAssets();

EditorUtility.DisplayDialog(
"Validate Settings",
"Default excel settings file has been created for accessing excel spreadsheet. Set valid runtime editor paths before proceeding.",
"OK"
);
}
else
{
Debug.LogWarning("Already exist at " + filePath);
}

Selection.activeObject = s_Instance;

return s_Instance;
}
}
}
49 changes: 13 additions & 36 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ namespace UnityQuickSheet
[CustomEditor(typeof(ExcelSettings))]
public class ExcelSettingsEditor : Editor
{
ExcelSettings setting;

public void OnEnable()
{
setting = target as ExcelSettings;

}

public override void OnInspectorGUI()
Expand All @@ -32,48 +30,27 @@ public override void OnInspectorGUI()

EditorGUILayout.Separator();

// paths for runtime and editor folder which will contain generated script files.
GUILayout.BeginHorizontal();
GUILayout.Label("Setting FilePath: ", GUILayout.Width(110));
// prevent to modify by manual
GUILayout.TextField(ExcelSettings.AssetPath, 120);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("Setting FileName: ", GUILayout.Width(110));
// read-only
GUILayout.TextField(ExcelSettings.AssetFileName, 120);
GUILayout.Label("Template Path: ", GUILayout.Width(100));
ExcelSettings.Instance.TemplatePath = GUILayout.TextField(ExcelSettings.Instance.TemplatePath);
GUILayout.EndHorizontal();

EditorGUILayout.Separator();

if (setting.CheckPath())
{
GUILayout.BeginHorizontal();
GUILayout.Label("Runtime Path: ", GUILayout.Width(100));
setting.RuntimePath = GUILayout.TextField(setting.RuntimePath);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Runtime Path: ", GUILayout.Width(100));
ExcelSettings.Instance.RuntimePath = GUILayout.TextField(ExcelSettings.Instance.RuntimePath);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
GUILayout.Label("Editor Path: ", GUILayout.Width(100));
setting.EditorPath = GUILayout.TextField(setting.EditorPath);
GUILayout.EndHorizontal();
}
else
{
GUILayout.BeginHorizontal();
GUILayout.Toggle(true, "", "CN EntryError", GUILayout.Width(20));
GUILayout.BeginVertical();
GUILayout.Label("", GUILayout.Height(12));
GUILayout.Label("Correct the path of the ExcelSetting.asset file.", GUILayout.Height(20));
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
GUILayout.BeginHorizontal();
GUILayout.Label("Editor Path: ", GUILayout.Width(100));
ExcelSettings.Instance.EditorPath = GUILayout.TextField(ExcelSettings.Instance.EditorPath);
GUILayout.EndHorizontal();

if (GUI.changed)
{
EditorUtility.SetDirty(setting);
EditorUtility.SetDirty(ExcelSettings.Instance);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public ListEntry Serialize(T e) {

public ListEntry Serialize(T e, ListEntry record) {
foreach (var p in typeof (T).GetProperties()) {
if (p.CanRead) {
record.Elements.Add(new ListEntry.Custom {
LocalName = p.Name.ToLowerInvariant(), // for some reason this HAS to be lowercase or it throws
Value = ToNullOrString(p.GetValue(e, null)),
});
}
if (p.CanRead) {
record.Elements.Add(new ListEntry.Custom {
LocalName = p.Name.ToLowerInvariant(), // for some reason this HAS to be lowercase or it throws
Value = ToNullOrString(p.GetValue(e, null)),
});
}
}
return record;
}
Expand All @@ -41,14 +41,14 @@ public object ConvertFrom(object value, Type t) {
var nc = new NullableConverter(t);
return nc.ConvertFrom(value);
}
//HACK: modified to return enum.
if (t.IsEnum)
{
return Enum.Parse(t, value.ToString(), true);
}
else
return Convert.ChangeType(value, t);
//HACK: modified to return enum.
if (t.IsEnum)
{
return Enum.Parse(t, value.ToString(), true);
}
else
return Convert.ChangeType(value, t);
}

public T Deserialize(ListEntry e) {
Expand All @@ -58,10 +58,10 @@ public T Deserialize(ListEntry e) {
var property = t.GetProperty(c.LocalName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
if (property == null)
continue;
if (property.CanWrite) {
try
{
if (property.CanWrite) {
try
{
if (property.PropertyType.IsArray)
{
const char DELIMETER = ','; // '\n'
Expand Down Expand Up @@ -123,12 +123,12 @@ public T Deserialize(ListEntry e) {

property.SetValue(r, value, null);
}
}
catch(Exception exc)
{
Debug.LogError ("GDataDB Serialization Exception: " + exc.Message + " ListEntry LocalName: " + c.LocalName);
}
}
}
catch(Exception exc)
{
Debug.LogError ("GDataDB Serialization Exception: " + exc.Message + " ListEntry LocalName: " + c.LocalName);
}
}
}
return r;
}
Expand Down
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 0 additions & 27 deletions Assets/QuickSheet/GDataPlugin/Editor/GoogleDataSettings.asset

This file was deleted.

Loading