Exemplo n.º 1
0
        public static LoadedPageData Initialize(DataSheet dataSheet, DatasheetType dataSheetType)
        {
            string         dataString = dataSheetType.GetIdentifier();
            SheetPage      sheetPage  = Array.Find(dataSheet.datasheetPages, i => i.sheetName == dataString);
            LoadedPageData pageData   = new LoadedPageData();

            pageData.dataSheetIdentifier = dataString;
            pageData.sheetPage           = sheetPage;

            if (!pageData.TryLoadStoredDimensions())
            {
                pageData.CalculateDimensions();
            }

            pageData.RecalculateTotalHeight();
            pageData.RecalculateTotalWidth();
            return(pageData);
        }
Exemplo n.º 2
0
 public override void OnInspectorGUI()
 {
     EditorGUILayout.LabelField(Localization.ONLY_ADJUSTABLE_WITH_TOOL);
     if (GUILayout.Button(Localization.OPEN_WITH_TOOL))
     {
         string        modelName     = serializedObject.targetObject.GetType().Name;
         string        identifier    = modelName.Remove(modelName.Length - "Model".Length, "Model".Length);
         DatasheetType datasheetType = (DatasheetType)Enum.Parse(typeof(DatasheetType), identifier);
         string        sheetName     = datasheetType.GetIdentifier();
         SheetCodesWindow.ShowWindow(sheetName);
     }
     EditorGUILayout.Space();
     EditorGUI.BeginDisabledGroup(true);
     serializedObject.Update();
     for (int i = 0; i < records.arraySize; i++)
     {
         SerializedProperty record   = records.GetArrayElementAtIndex(i);
         SerializedProperty property = record.FindPropertyRelative("identifier");
         string             enumName = property.enumValueIndex >= 0 ? property.enumDisplayNames[property.enumValueIndex] : "";
         EditorGUILayout.PropertyField(records.GetArrayElementAtIndex(i), new GUIContent(enumName), true);
     }
     EditorGUI.BeginDisabledGroup(false);
 }
Exemplo n.º 3
0
        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);
        }