public static SheetPage DeconstructDatasheetCode(DatasheetType datasheetType) { SheetPage sheetPage = new SheetPage(); sheetPage.sheetName = datasheetType.GetIdentifier(); sheetPage.index = (int)datasheetType; Type recordType = Type.GetType(SheetStringDefinitions.NAMESPACE + "." + sheetPage.sheetRecordName + SheetStringDefinitions.ASSEMBLY_LOCATION); FieldInfo[] fieldInfos = recordType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); fieldInfos = fieldInfos.Where(i => i.IsPublic || i.GetCustomAttribute <SerializeField>() != null).ToArray(); sheetPage.columns = new List <SheetColumn>(fieldInfos.Length); for (int i = 0; i < fieldInfos.Length; i++) { FieldInfo fieldInfo = fieldInfos[i]; SheetColumn column = new SheetColumn(); sheetPage.columns.Add(column); ColumnName columnNameAttribute = fieldInfo.GetCustomAttribute <ColumnName>(); column.serializationName = columnNameAttribute.columnName; column.propertyName = fieldInfo.Name.Remove(0, 1).FirstLetterToUpper(); column.isCollection = fieldInfo.FieldType.IsArray; column.dataType = GetDataType(fieldInfo.FieldType); if (column.dataType == SheetDataType.Reference) { string typeName = fieldInfo.FieldType.Name; string datasheetName; if (column.isCollection) { datasheetName = typeName.Remove(typeName.Length - SheetStringDefinitions.IDENTIFIER_SUFFIX.Length - SheetStringDefinitions.ARRAY_VARIABLE_SUFFIX.Length); } else { datasheetName = typeName.Remove(typeName.Length - SheetStringDefinitions.IDENTIFIER_SUFFIX.Length); } column.referenceSheet = Enum.Parse(typeof(DatasheetType), datasheetName).GetIdentifier(); } } Type modelType = Type.GetType(SheetStringDefinitions.NAMESPACE + "." + sheetPage.sheetModelNameUpperCase + SheetStringDefinitions.ASSEMBLY_LOCATION); object modelObject = AssetDatabase.LoadAssetAtPath(sheetPage.scriptableObjectFileLocation, modelType); FieldInfo recordsFieldInfo = modelType.GetField(SheetStringDefinitions.RECORDS_PROPERTY_NAME, BindingFlags.NonPublic | BindingFlags.Instance); Array modelRecords = (Array)recordsFieldInfo.GetValue(modelObject); Type recordBaseType = recordType.BaseType; FieldInfo recordIdentifierFieldInfo = recordBaseType.GetField(SheetStringDefinitions.RECORD_IDENTIFIER_VARIABLE, BindingFlags.NonPublic | BindingFlags.Instance); Type identifierType = Type.GetType(sheetPage.sheetIdentifierName + SheetStringDefinitions.ASSEMBLY_LOCATION); sheetPage.rows = new List <SheetRow>(modelRecords.Length); for (int i = 0; i < modelRecords.Length; i++) { object record = modelRecords.GetValue(i); SheetRow sheetRow = new SheetRow(); sheetPage.rows.Add(sheetRow); object identifierEnumValue = recordIdentifierFieldInfo.GetValue(record); sheetRow.index = (int)identifierEnumValue; sheetRow.identifier = identifierEnumValue.GetIdentifier(); sheetRow.enumValue = identifierEnumValue.ToString(); sheetRow.cells = new List <SheetCell>(fieldInfos.Length); for (int j = 0; j < fieldInfos.Length; j++) { FieldInfo fieldInfo = fieldInfos[j]; SheetColumn column = sheetPage.columns[j]; object data = fieldInfo.GetValue(record); if (data == null) { data = column.GetDefaultCellValue(); } if (column.dataType == SheetDataType.Reference) { if (column.isCollection) { Array arrayData = ((Array)data); string[] stringData = new string[arrayData.Length]; for (int h = 0; h < arrayData.Length; h++) { stringData[h] = arrayData.GetValue(h).GetIdentifier(); } data = stringData; } else { data = data.GetIdentifier(); } } SheetCell cell = new SheetCell(data); sheetRow.cells.Add(cell); } } return(sheetPage); }
private void CalculateDimensions() { int[,] widthSizes = new int[sheetPage.columns.Count + 1, sheetPage.rows.Count + 1]; int[,] heightSizes = new int[sheetPage.columns.Count + 1, sheetPage.rows.Count + 1]; int identifierWidth = CalculateWidth("Identifier"); widthSizes[0, 0] = identifierWidth; widthDimensions = new List <int>(); heightDimensions = new List <int>(); for (int i = 0; i < sheetPage.rows.Count; i++) { SheetRow row = sheetPage.rows[i]; identifierWidth = CalculateWidth(row.identifier); widthSizes[0, i + 1] = identifierWidth; } for (int i = 0; i < sheetPage.columns.Count; i++) { SheetColumn column = sheetPage.columns[i]; int columnWidth = CalculateWidth(column.serializationName); widthSizes[i + 1, 0] = columnWidth; heightSizes[i + 1, 0] = WindowSettings.ONE_LINE_HEIGHT * 2; for (int j = 0; j < sheetPage.rows.Count; j++) { SheetRow row = sheetPage.rows[j]; SheetCell cell = row.cells[i]; if (column.isCollection) { Array cellData = (Array)cell.data; int highestWidth = 0; for (int h = 0; h < cellData.Length; h++) { object cellObject = cellData.GetValue(h); string content = cellObject != null?cellObject.ToString() : Localization.NULLOBJECT_TO_STRING; int width = CalculateWidth(content); highestWidth = Mathf.Max(highestWidth, width); } int widthSet = Mathf.Clamp(highestWidth, WindowSettings.MINWIDTH, WindowSettings.MAXWIDTH_INITALIZATION); int heightSet = Mathf.Min(cellData.Length * WindowSettings.ONE_LINE_HEIGHT, WindowSettings.MAXHEIGHT_INITALIZATION); widthSizes[i + 1, j + 1] = widthSet; heightSizes[i + 1, j + 1] = heightSet; } else { string content = cell.data != null?cell.data.ToString() : Localization.NULLOBJECT_TO_STRING; int width = CalculateWidth(content); width = Mathf.Clamp(width, WindowSettings.MINWIDTH, WindowSettings.MAXWIDTH_INITALIZATION); widthSizes[i + 1, j + 1] = width; heightSizes[i + 1, j + 1] = WindowSettings.ONE_LINE_HEIGHT; } } } for (int i = 0; i < widthSizes.GetLength(0); i++) { int heighestWidth = WindowSettings.MINWIDTH; for (int j = 0; j < widthSizes.GetLength(1); j++) { int width = widthSizes[i, j]; heighestWidth = Mathf.Max(heighestWidth, width); } widthDimensions.Add(heighestWidth + WindowSettings.WIDTH_PADDING); } for (int i = 0; i < heightSizes.GetLength(1); i++) { int heighestHeight = WindowSettings.MINHEIGHT; for (int j = 0; j < heightSizes.GetLength(0); j++) { int height = heightSizes[j, i]; heighestHeight = Mathf.Max(heighestHeight, height); } heightDimensions.Add(heighestHeight + WindowSettings.HEIGHT_PADDING); } }
private static void CreateModelInstance(SheetPage sheetPage) { ScriptableObject instance = AssetDatabase.LoadAssetAtPath <ScriptableObject>(sheetPage.scriptableObjectFileLocation); if (instance == null) { instance = ScriptableObject.CreateInstance(sheetPage.sheetModelNameUpperCase); Directory.CreateDirectory(SheetStringDefinitions.SCRIPTABLEOBJECT_DIRECTORY); AssetDatabase.CreateAsset(instance, sheetPage.scriptableObjectFileLocation); } Type typeModel = instance.GetType(); FieldInfo recordsMemberInfo = typeModel.GetField(SheetStringDefinitions.RECORDS_PROPERTY_NAME, BindingFlags.NonPublic | BindingFlags.Instance); Type typeRecord = recordsMemberInfo.FieldType.GetElementType(); Type baseType = typeRecord.BaseType; Array recordCollection = Array.CreateInstance(typeRecord, sheetPage.rows.Count); FieldInfo identifierFieldInfo = baseType.GetField(SheetStringDefinitions.RECORD_IDENTIFIER_VARIABLE, BindingFlags.NonPublic | BindingFlags.Instance); for (int i = 0; i < sheetPage.rows.Count; i++) { SheetRow sheetRow = sheetPage.rows[i]; object recordInstance = Activator.CreateInstance(typeRecord); identifierFieldInfo.SetValue(recordInstance, sheetRow.identifier.GetIdentifierEnum(identifierFieldInfo.FieldType)); for (int j = 0; j < sheetPage.columns.Count; j++) { SheetColumn sheetColumn = sheetPage.columns[j]; FieldInfo propertyInfo = typeRecord.GetField("_" + sheetColumn.propertyName.FirstLetterToLower(), BindingFlags.NonPublic | BindingFlags.Instance); object data = sheetRow.cells[j].data; if (sheetColumn.dataType == SheetDataType.Reference) { if (sheetColumn.isCollection) { Type identifierType = propertyInfo.FieldType.GetElementType(); string[] dataStrings = data as string[]; Array dataArray = Array.CreateInstance(identifierType, dataStrings.Length); for (int h = 0; h < dataStrings.Length; h++) { dataArray.SetValue(dataStrings[h].GetIdentifierEnum(identifierType), h); } data = dataArray; } else { Type identifierType = propertyInfo.FieldType; data = ((string)data).GetIdentifierEnum(identifierType); } } propertyInfo.SetValue(recordInstance, data); } recordCollection.SetValue(recordInstance, i); } recordsMemberInfo.SetValue(instance, recordCollection); EditorUtility.SetDirty(instance); }