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

Add QuickSheet Menu #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions Assets/QuickSheet/Editor/BaseMachineEditor.cs
Expand Up @@ -42,7 +42,7 @@ public override void OnInspectorGUI()
/// <summary>
/// It should be overried and implemented in the derived class
/// </summary>
protected virtual void Import(bool reimport = false)
public virtual void Import(bool reimport = false)
{
throw new NotImplementedException();
}
Expand All @@ -66,7 +66,7 @@ protected bool IsValidHeader(string s)
/// Generate script files with the given templates.
/// Total four files are generated, two for runtime and others for editor.
/// </summary>
protected virtual ScriptPrescription Generate(BaseMachine m)
public virtual ScriptPrescription Generate(BaseMachine m)
{
if (m == null)
return null;
Expand Down Expand Up @@ -395,5 +395,11 @@ protected ColumnHeader ParseColumnHeader(string columnheader, int order)

return new ColumnHeader { name = cHeader, type = CellType.Undefined, OrderNO = order };
}

public static void CreateGenerateDirectory(BaseMachine machine)
{
Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.RuntimeClassPath);
Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.EditorClassPath);
}
}
}
156 changes: 156 additions & 0 deletions Assets/QuickSheet/Editor/QuickSheetMenu.cs
@@ -0,0 +1,156 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UnityQuickSheet
{
public static class QuickSheetMenu
{
/// <summary>
/// Select currently exist account setting asset file.
/// </summary>
[MenuItem("QuickSheet/Excel Setting")]
public static void SelectExcelSetting()
{
Selection.activeObject = ExcelSettings.Instance;
if (Selection.activeObject == null)
{
Debug.LogError(@"No ExcelSetting.asset file is found. Create setting file first. See the menu at 'Create/QuickSheet/Setting/Excel Setting'.");
}
}

/// <summary>
/// Select currently exist account setting asset file.
/// </summary>
[MenuItem("QuickSheet/Google Data Setting")]
public static void SelectGoogleDataSetting()
{
Selection.activeObject = GoogleDataSettings.Instance;
if (Selection.activeObject == null)
{
Debug.LogError("No GoogleDataSettings.asset file is found. Create setting file first.");
}
}

/// <summary>
/// Setup selected excel files
/// 1.Create Machine Asset
/// 2.Reimport Machine
/// 3.Generate Class
/// 4.Reimport Excel file
/// </summary>
[MenuItem("QuickSheet/Setup Select Excels")]
public static void SetupSelectExcels()
{
if (Selection.assetGUIDs.Length == 0)
{
EditorUtility.DisplayDialog("Error", "Select excel files to setup!", "OK");
return;
}

var selectObjs = new List<Object>();
foreach (var guid in Selection.assetGUIDs)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
if (!IsExcel(path)) continue;

ExcelSettings.Instance._waitImportPath.Add(path);

var excelQuery = new ExcelQuery(path);
var sheets = excelQuery.GetSheetNames();
for (int i = 0; i < sheets.Length; i++)
{
var machine = ExcelMachine.CreateScriptMachineAsset();
machine.excelFilePath = path;
machine.SheetNames = sheets;
machine.WorkSheetName = sheets[i];
machine.CurrentSheetIndex = i;
machine.SpreadSheetName = Path.GetFileName(path);

ReimportMachine(machine, true);
BaseMachineEditor.CreateGenerateDirectory(machine);
GenerateMachine(machine, false);
RenameMachineAsset(machine);

selectObjs.Add(machine);
Debug.LogFormat("Setup finished! file:{0}, Sheet:{1}", machine.SpreadSheetName, sheets[i]);
}
}

Selection.objects = selectObjs.ToArray();
AssetDatabase.Refresh();
}

/// <summary>
/// After generate script reload, reimport excel file to generate data asset and update it
/// </summary>
[UnityEditor.Callbacks.DidReloadScripts]
private static void OnScriptsReloaded()
{
foreach (var path in ExcelSettings.Instance._waitImportPath)
{
AssetDatabase.ImportAsset(path);
}
ExcelSettings.Instance._waitImportPath.Clear();
}

/// <summary>
/// Refresh all excel files in project
/// refresh when change excel header column
/// </summary>
[MenuItem("QuickSheet/Refresh All")]
public static void RefreshAll()
{
foreach (var machine in Util.FindAssetsByType<BaseMachine>())
{
ReimportMachine(machine, true);
GenerateMachine(machine, true);
}

var files = Directory.GetFiles(Application.dataPath, "*.xls?", SearchOption.AllDirectories);
foreach (var file in files)
{
if (!IsExcel(file) || Path.GetFileName(file).StartsWith("~$")) continue;
var relativePath = "Assets" + file.Replace(Application.dataPath, "");
Debug.Log(relativePath);
AssetDatabase.ImportAsset(relativePath);
}
AssetDatabase.Refresh();
}

private static void GenerateMachine(BaseMachine machine, bool onlyDataClass)
{
var editor = Editor.CreateEditor(machine) as BaseMachineEditor;
if (editor == null) return;
machine.onlyCreateDataClass = onlyDataClass;
editor.Generate(machine);
}

private static void ReimportMachine(BaseMachine machine, bool reimport)
{
var editor = Editor.CreateEditor(machine) as BaseMachineEditor;
if (editor == null) return;
editor.Import(reimport);
}

private static void RenameMachineAsset(ExcelMachine machine)
{
var name = new StringBuilder();
name.Append(machine.SpreadSheetName.Split('.')[0])
.Append("-")
.Append(machine.WorkSheetName)
.Append("-")
.Append("Importer");
var err = AssetDatabase.RenameAsset(AssetDatabase.GetAssetPath(machine), name.ToString());
if (!string.IsNullOrEmpty(err)) Debug.LogWarning("Rename failed " + err);
}

private static bool IsExcel(string path)
{
var ext = Path.GetExtension(path);
return ext == ".xls" || ext == ".xlsx";
}
}
}
3 changes: 3 additions & 0 deletions Assets/QuickSheet/Editor/QuickSheetMenu.cs.meta

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

12 changes: 5 additions & 7 deletions Assets/QuickSheet/Editor/Util/SingletonScriptableObject.cs
Expand Up @@ -27,16 +27,14 @@ public static T Instance
_instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
if (!_instance)
{
string filter = "t:" + typeof(T);
var guids = AssetDatabase.FindAssets(filter);
if (guids.Length > 0)
var assets = Util.FindAssetsByType<T>();
if (assets.Length > 0)
{
if (guids.Length > 1)
if (assets.Length > 1)
Debug.LogWarningFormat("Multiple {0} assets are found.", typeof(T));

string assetPath = AssetDatabase.GUIDToAssetPath(guids[0]);
Debug.LogFormat("Using {0}.", assetPath);
_instance = AssetDatabase.LoadAssetAtPath<T>(assetPath);
_instance = assets[0];
Debug.LogFormat("Using {0}.", AssetDatabase.GetAssetPath(_instance));
}
// no found any setting file.
else
Expand Down
14 changes: 14 additions & 0 deletions Assets/QuickSheet/Editor/Util/Util.cs
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections;
using System.Linq;
using UnityEditor;

namespace UnityQuickSheet
{
Expand All @@ -25,5 +26,18 @@ public static class Util
"ushort", "using", "virtual", "void", "volatile", "while",
};

public static T[] FindAssetsByType<T>() where T : UnityEngine.Object
{
string filter = "t:" + typeof(T);
var guids = AssetDatabase.FindAssets(filter);
var results = new T[guids.Length];
for (int i = 0; i < guids.Length; i++)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
results[i] = AssetDatabase.LoadAssetAtPath<T>(assetPath);
}

return results;
}
}
}
Binary file modified Assets/QuickSheet/ExcelPlugin/Editor/Excel Settings.asset
Binary file not shown.
3 changes: 2 additions & 1 deletion Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachine.cs
Expand Up @@ -50,13 +50,14 @@ public int CurrentSheetIndex
/// A menu item which create a 'ExcelMachine' asset file.
/// </summary>
[MenuItem("Assets/Create/QuickSheet/Tools/Excel")]
public static void CreateScriptMachineAsset()
public static ExcelMachine CreateScriptMachineAsset()
{
ExcelMachine inst = ScriptableObject.CreateInstance<ExcelMachine>();
string path = CustomAssetUtility.GetUniqueAssetPathNameOrFallback(ImportSettingFilename);
AssetDatabase.CreateAsset(inst, path);
AssetDatabase.SaveAssets();
Selection.activeObject = inst;
return inst;
}
}
}
7 changes: 3 additions & 4 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelMachineEditor.cs
Expand Up @@ -168,8 +168,7 @@ public override void OnInspectorGUI()
return;
}

Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.RuntimeClassPath);
Directory.CreateDirectory(Application.dataPath + Path.DirectorySeparatorChar + machine.EditorClassPath);
CreateGenerateDirectory(machine);

ScriptPrescription sp = Generate(machine);
if (sp != null)
Expand All @@ -189,7 +188,7 @@ public override void OnInspectorGUI()
/// <summary>
/// Import the specified excel file and prepare to set type of each cell.
/// </summary>
protected override void Import(bool reimport = false)
public override void Import(bool reimport = false)
{
ExcelMachine machine = target as ExcelMachine;

Expand Down Expand Up @@ -291,7 +290,7 @@ protected override void CreateAssetCreationScript(BaseMachine m, ScriptPrescript
// path where the .asset file will be created.
string path = Path.GetDirectoryName(machine.excelFilePath);
path += "/" + machine.WorkSheetName + ".asset";
sp.assetFilepath = path;
sp.assetFilepath = path.Replace('\\', '/');
sp.assetPostprocessorClass = machine.WorkSheetName + "AssetPostprocessor";
sp.template = GetTemplate("PostProcessor");

Expand Down
15 changes: 4 additions & 11 deletions Assets/QuickSheet/ExcelPlugin/Editor/ExcelSettings.cs
Expand Up @@ -8,6 +8,7 @@
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace UnityQuickSheet
Expand All @@ -32,18 +33,10 @@ public class ExcelSettings : SingletonScriptableObject<ExcelSettings>
/// A path where generated editor script files are to be put.
/// </summary>
public string EditorPath = string.Empty;

/// <summary>
/// Select currently exist account setting asset file.
/// record import path for QuickSheetMenu
/// </summary>
[MenuItem("Edit/Project Settings/QuickSheet/Select Excel Setting")]
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 'Create/QuickSheet/Setting/Excel Setting'.");
}
}
internal List<string> _waitImportPath = new List<string>();
}
}
13 changes: 0 additions & 13 deletions Assets/QuickSheet/GDataPlugin/Editor/GoogleDataSettings.cs
Expand Up @@ -68,18 +68,5 @@ public struct OAuth2JsonData
public string _RefreshToken = "";

public string _AccessToken = "";

/// <summary>
/// Select currently exist account setting asset file.
/// </summary>
[MenuItem("Edit/Project Settings/QuickSheet/Select Google Data Setting")]
public static void Edit()
{
Selection.activeObject = Instance;
if (Selection.activeObject == null)
{
Debug.LogError("No GoogleDataSettings.asset file is found. Create setting file first.");
}
}
}
}
Expand Up @@ -190,7 +190,7 @@ private void DoCellQuery(OnEachCell onCell)
/// <summary>
/// Connect to the google spreadsheet and retrieves its header columns.
/// </summary>
protected override void Import(bool reimport = false)
public override void Import(bool reimport = false)
{
Regex re = new Regex(@"\d+");

Expand Down