如何将Excel或Google Sheet与Unity连接并提取应用数据?
实现Unity与Excel/Google Sheet的数据交互方法
一、Excel与Unity的连接方法
1. 直接读取本地Excel文件
- 将Excel保存为
.csv或.xlsx格式,放入Unity的Assets或StreamingAssets目录 - 读取CSV可直接用Unity自带的
TextAsset:using UnityEngine; using System.Collections.Generic; public class CSVReader : MonoBehaviour { public TextAsset csvAsset; void Start() { List<string[]> dataRows = new List<string[]>(); string[] allLines = csvAsset.text.Split('\n'); foreach (string line in allLines) { if (!string.IsNullOrWhiteSpace(line)) { dataRows.Add(line.Split(',')); } } // 解析后的数据可直接用于赋值UI、配置游戏参数等逻辑 } } - 读取
.xlsx需导入第三方库(如EPPlus的DLL),通过库API直接操作工作表和单元格
2. 基于ScriptableObject导入Excel数据
- 创建对应数据结构的ScriptableObject类,例如:
using UnityEngine; [CreateAssetMenu(fileName = "GameConfig", menuName = "Config/GameConfig")] public class GameConfigSO : ScriptableObject { public int maxLevel; public float playerSpeed; public string[] itemNames; } - 编写Unity编辑器工具:读取Excel解析数据后,实例化ScriptableObject并赋值,保存为资源文件
- 运行时直接加载该资源,无需实时读取Excel,性能更稳定
二、Google Sheet与Unity的连接方法
1. 公开CSV链接读取(无授权)
- 在Google Sheet中点击「文件」→「发布到网络」,选择目标工作表,格式选
CSV,复制发布链接 - 在Unity中用
UnityWebRequest请求链接,下载后按CSV逻辑解析:using UnityEngine; using UnityEngine.Networking; using System.Collections; public class PublicSheetReader : MonoBehaviour { public string csvPublicUrl; IEnumerator Start() { using (UnityWebRequest request = UnityWebRequest.Get(csvPublicUrl)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { string[] lines = request.downloadHandler.text.Split('\n'); foreach (string line in lines) { if (!string.IsNullOrWhiteSpace(line)) { string[] rowData = line.Split(','); // 处理每行数据 } } } } } } - 注意:此方法仅适用于公开可访问的Sheet,私有数据请勿使用
2. Google Sheets API授权访问(私有数据)
- 前往Google Cloud Console创建项目,启用Google Sheets API,生成服务账号密钥(JSON格式),放入Unity的
StreamingAssets目录 - 导入
Google.Apis.Sheets.v4及相关依赖DLL(需匹配Unity的.NET版本) - 编写读取代码:
using Google.Apis.Sheets.v4; using Google.Apis.Auth.OAuth2; using System.IO; using UnityEngine; using System.Threading.Tasks; public class AuthorizedSheetReader : MonoBehaviour { private string sheetId = "你的Google Sheet ID"; private string dataRange = "Sheet1!A1:Z100"; async void Start() { var keyPath = Path.Combine(Application.streamingAssetsPath, "service-account-key.json"); using (var stream = new FileStream(keyPath, FileMode.Open)) { var credential = GoogleCredential.FromStream(stream) .CreateScoped(SheetsService.Scope.SpreadsheetsReadonly); var sheetService = new SheetsService(new Google.Apis.Services.BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Unity Sheet Reader" }); var request = sheetService.Spreadsheets.Values.Get(sheetId, dataRange); var response = await request.ExecuteAsync(); if (response.Values != null && response.Values.Count > 0) { foreach (var row in response.Values) { // 处理每行数据 Debug.Log(string.Join(" | ", row)); } } } } } - 关键步骤:在Google Sheet的分享设置中,添加服务账号的邮箱并授予读取权限
3. 修复Sheet Read UnityPackage的常见问题
- 检查包的.NET版本与Unity项目是否匹配(Unity默认多为.NET Framework,包若基于.NET Core会报错)
- 确认Google Sheet已给包使用的账号开放读取权限
- 查看Unity控制台的错误日志,定位网络请求失败、权限不足等具体问题
内容的提问来源于stack exchange,提问作者Kim Huisu
相关产品推荐
相关产品推荐

