1 工具简介
将策划配置的excel转换成程序使用json文件。方便策划是excel的功能。
2文件的放置和生成
需要将excle文件放置到项目中的Excel文件夹下面。
生成的文件会在Res/Config文件夹下面
生成json文件只需要点击Tools下的ExcelTools就可以了
3 excel文件的格式
第一行是说明,不具有实际意义
第二行为属性的名字,json文件的key值。不能使用数字开头,特殊符号开头,不能使用程序内的关键字,如:string,int。
第三行是程序内使用的类型,支持的类型有 string int float double bool。
bool中fasle,0,空白 都代表否的意思,填写其他值代表为真
生成的接json文件的名字是以表命中(#文件名#) #号包裹的名字位主的,如果表名不存在#号,则该表不导出。示例如下图:
4 注意事项
1. excel文件名中不能存在空格或特殊符号
2. excel后缀只支持xlsx,并且需要确保后缀小写,不能使用大写
3. 导出的文件名确定后不要轻易修改,修改需要和程序确认(#文件名#)
4. 添加。删除。修改字段时,请和程序确定,防止游戏内逻辑出错
5. json中需要转换的数据结构由程序自行创建
6. 每次生成会清理config文件,重新从excel生成json文件。请不要手动修改生成后的json文件。确保excel文件的正确定。不要config文件夹下面放置其他文件
7. 使用工具的时候需要关闭excel文件文章来源:https://www.toymoban.com/news/detail-425307.html
using UnityEngine;
using System.IO;
using System.Linq;
/// <summary>
/// @ 2017.12.25
/// 功能:通用静态方法
/// </summary>
public class GameUtility
{
public const string AssetsFolderName = "Assets";
public static string FormatToUnityPath(string path)
{
return path.Replace("\\", "/");
}
public static string FormatToSysFilePath(string path)
{
return path.Replace("/", "\\");
}
public static string FullPathToAssetPath(string full_path)
{
full_path = FormatToUnityPath(full_path);
if (!full_path.StartsWith(Application.dataPath))
{
return null;
}
string ret_path = full_path.Replace(Application.dataPath, "");
return AssetsFolderName + ret_path;
}
public static string GetFileExtension(string path)
{
return Path.GetExtension(path).ToLower();
}
public static string[] GetSpecifyFilesInFolder(string path, string[] extensions = null, bool exclude = false)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
if (extensions == null)
{
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
}
else if (exclude)
{
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
.Where(f => !extensions.Contains(GetFileExtension(f))).ToArray();
}
else
{
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
.Where(f => extensions.Contains(GetFileExtension(f))).ToArray();
}
}
public static string[] GetSpecifyFilesInFolder(string path, string pattern)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
return Directory.GetFiles(path, pattern, SearchOption.AllDirectories);
}
public static string[] GetAllFilesInFolder(string path)
{
return GetSpecifyFilesInFolder(path);
}
public static string[] GetAllDirsInFolder(string path)
{
return Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
}
public static void CheckFileAndCreateDirWhenNeeded(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}
FileInfo file_info = new FileInfo(filePath);
DirectoryInfo dir_info = file_info.Directory;
if (!dir_info.Exists)
{
Directory.CreateDirectory(dir_info.FullName);
}
}
public static void CheckDirAndCreateWhenNeeded(string folderPath)
{
if (string.IsNullOrEmpty(folderPath))
{
return;
}
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
}
public static bool SafeWriteAllBytes(string outFile, byte[] outBytes)
{
try
{
if (string.IsNullOrEmpty(outFile))
{
return false;
}
CheckFileAndCreateDirWhenNeeded(outFile);
if (File.Exists(outFile))
{
File.SetAttributes(outFile, FileAttributes.Normal);
}
File.WriteAllBytes(outFile, outBytes);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeWriteAllBytes failed! path = {0} with err = {1}", outFile, ex.Message));
return false;
}
}
public static bool SafeWriteAllLines(string outFile, string[] outLines)
{
try
{
if (string.IsNullOrEmpty(outFile))
{
return false;
}
CheckFileAndCreateDirWhenNeeded(outFile);
if (File.Exists(outFile))
{
File.SetAttributes(outFile, FileAttributes.Normal);
}
File.WriteAllLines(outFile, outLines);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeWriteAllLines failed! path = {0} with err = {1}", outFile, ex.Message));
return false;
}
}
public static bool SafeWriteAllText(string outFile, string text)
{
try
{
if (string.IsNullOrEmpty(outFile))
{
return false;
}
CheckFileAndCreateDirWhenNeeded(outFile);
if (File.Exists(outFile))
{
File.SetAttributes(outFile, FileAttributes.Normal);
}
File.WriteAllText(outFile, text);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeWriteAllText failed! path = {0} with err = {1}", outFile, ex.Message));
return false;
}
}
public static byte[] SafeReadAllBytes(string inFile)
{
try
{
if (string.IsNullOrEmpty(inFile))
{
return null;
}
if (!File.Exists(inFile))
{
return null;
}
File.SetAttributes(inFile, FileAttributes.Normal);
return File.ReadAllBytes(inFile);
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeReadAllBytes failed! path = {0} with err = {1}", inFile, ex.Message));
return null;
}
}
public static string[] SafeReadAllLines(string inFile)
{
try
{
if (string.IsNullOrEmpty(inFile))
{
return null;
}
if (!File.Exists(inFile))
{
return null;
}
File.SetAttributes(inFile, FileAttributes.Normal);
return File.ReadAllLines(inFile);
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeReadAllLines failed! path = {0} with err = {1}", inFile, ex.Message));
return null;
}
}
public static string SafeReadAllText(string inFile)
{
try
{
if (string.IsNullOrEmpty(inFile))
{
return null;
}
if (!File.Exists(inFile))
{
return null;
}
File.SetAttributes(inFile, FileAttributes.Normal);
return File.ReadAllText(inFile);
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeReadAllText failed! path = {0} with err = {1}", inFile, ex.Message));
return null;
}
}
public static void DeleteDirectory(string dirPath)
{
string[] files = Directory.GetFiles(dirPath);
string[] dirs = Directory.GetDirectories(dirPath);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(dirPath, false);
}
public static bool SafeClearDir(string folderPath)
{
try
{
if (string.IsNullOrEmpty(folderPath))
{
return true;
}
if (Directory.Exists(folderPath))
{
DeleteDirectory(folderPath);
}
Directory.CreateDirectory(folderPath);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeClearDir failed! path = {0} with err = {1}", folderPath, ex.Message));
return false;
}
}
public static bool SafeDeleteDir(string folderPath)
{
try
{
if (string.IsNullOrEmpty(folderPath))
{
return true;
}
if (Directory.Exists(folderPath))
{
DeleteDirectory(folderPath);
}
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeDeleteDir failed! path = {0} with err: {1}", folderPath, ex.Message));
return false;
}
}
public static bool SafeDeleteFile(string filePath)
{
try
{
if (string.IsNullOrEmpty(filePath))
{
return true;
}
if (!File.Exists(filePath))
{
return true;
}
File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeDeleteFile failed! path = {0} with err: {1}", filePath, ex.Message));
return false;
}
}
public static bool SafeRenameFile(string sourceFileName, string destFileName)
{
try
{
if (string.IsNullOrEmpty(sourceFileName))
{
return false;
}
if (!File.Exists(sourceFileName))
{
return true;
}
SafeDeleteFile(destFileName);
File.SetAttributes(sourceFileName, FileAttributes.Normal);
File.Move(sourceFileName, destFileName);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeRenameFile failed! path = {0} with err: {1}", sourceFileName, ex.Message));
return false;
}
}
public static bool SafeCopyFile(string fromFile, string toFile)
{
try
{
if (string.IsNullOrEmpty(fromFile))
{
return false;
}
if (!File.Exists(fromFile))
{
return false;
}
CheckFileAndCreateDirWhenNeeded(toFile);
SafeDeleteFile(toFile);
File.Copy(fromFile, toFile, true);
return true;
}
catch (System.Exception ex)
{
Debug.LogError(string.Format("SafeCopyFile failed! formFile = {0}, toFile = {1}, with err = {2}",
fromFile, toFile, ex.Message));
return false;
}
}
}
下载链接: https://pan.baidu.com/s/1uOlQg6qcn4gFH16aDx8B-Q?pwd=w23u 提取码: w23u文章来源地址https://www.toymoban.com/news/detail-425307.html
到了这里,关于unity 中excel转json插件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!