Note : Using Git repository is not the only way to import this package into your project. You can also download the code and dlls into your project, or import the package from your remote npm where this package is deployed.
Option 1 (all versions of Unity that supports Package Manager is OK) :
-
Open Packages/manifest.json in your project with any text editor
-
Insert a new line as below into "dependencies"
{ "dependencies": { "com.greatclock.exceltoscriptableobject": "git+https://github.com/greatclock/excel_to_scriptableobject.git", ... } }
-
Save the file Packages/manifest.json
-
Back to Unity, and the package will be imported automatically
Option 2 (Unity2019 or newer only) :
-
Open "Package Manager" and click the "+" button on the left top. And then select "Add package from git URL..."
-
Fill in the blank with "https://github.com/greatclock/excel_to_scriptableobject.git" and then click "Add" button
A folder named "Excel To ScriptableObject" will be presented in Packages of Project window after the package imported into Unity.
In this guiding, we are going to make a ".xlsx" file that contains datas a bunch of students, including their basic information such as name, gender, and their parents' information with their phone number.
It aimed to show how basic data types, enum types, arrays and nested types are used in this tool.
- The whole work flow
- Rules in all useful names
- Comments row in xlsx files
- Using basic data types and arrays
- Retrieving data in Unity and debugging data
- Multi sheets in one xlsx file
- Nesting data in sheets via id or key
- Hiding get method of a specific sheet to make it internally used only
- Procedures in using enum types
- Defining enum values for enum types in sheet
- Dealing with special characters in various xlsx applications
- Avoiding GC alloc caused by creating 'List' when retrieving items
- Using multi-language string
- Using registered key-value translation function to make multi-language field available with nothing else to do
Note : Logical Rich-Text means : one or more segment of a string may be replaced with contents only of value at the time that the string shows. For example : player's nick name, player's current level. It's NOT used for dealing with text colors, sizes or in-text images.
- Using logical rich-text string
- Using registered enrich function to make logical rich-text field available with nothing else to do
-
Grouping users, AB test : Load different groups of data assets according to users' properties. The types of data assets in each group should be consistent.
-
Multi-Language : Load different data assets containing specific language text according to user's current language. Thus no extra languages text will be loaded, and it's possible to switch between languages.
-
Offline update : The client should download the data assets that will be used beforehand. And the latest asset can be used after a specific time point, even if the user is offline.
-
Click "+" button at right-bottom of
Slave Excels
in Excel Setting. Thus a slave item for a .xlsx file is added. -
Click "Select" to select a .xlsx file and specify a folder where the data asset lays.
-
Click
Flush Data
to generate data asset for the selected .xlsx file.
-
No script or data type will be generated for .xlsx file in
Slave Excels
. The generated asset file for slave excel will be of the same type of theExcel Setting
it belongs to. -
The sheets and fileds in slave .xlsx files should keep consistent with the .xlsx file in
Excel Setting
.
Some possible rules for a project :
-
The first 2 rows in all .xlsx sheets represent field names and field types. The 3rd row is specifies if the field is used for Client only (C) or Server only (S) or Both (CS).
-
If the field is used for Server only, the field together with data in it will not be imported into client.
The following codes describes foregoing rules :
// Assets/Editor/ExcelFieldFilterDemo.cs
using GreatClock.Common.ExcelToSO;
public class ExcelFieldFilterDemo {
// Match all tables via regex, with priority 0, reading the 3rd (index:2) row of the field for filtering.
[ExcelFieldFilter(eMatchType.Regex, @".+", priority: 0, requireRowIndex: 2)]
// Filter method must be static.
// The first two parameters will be table name and field name.
// The returned value is the field name that will be used in code and data asset.
// If null or empty string is returned, the field will be ignored.
static string DefaultFieldFilter(string tablename, string fieldname, string content) {
if (!string.IsNullOrEmpty(content) && (content.Contains("c") || content.Contains("C"))) {
return fieldname;
}
return null;
}
}
Notice :
-
The first column in each sheet will used as table index, and will not be filtered.
-
One table (aka sheet) may match more than one field filters. But only the one with biggest priority will be in use.
-
requireRowIndex
is a optional parameter. If it's not specified, thecontent
parameter in field filter method should be removed. -
The field filter method returns the field name that will be used in code and data asset. So you can use field filter method to fix the field name in .xlsx file.
-
More than one
ExcelFieldFilter
can be added to a field filter method.