protected override bool Create(object data)
        {
            bool          result    = true;
            List <object> args      = data as List <object>;
            string        schemaKey = args[0] as string;
            string        itemName  = args[1] as string;

            Dictionary <string, object> schemaData = null;

            if (GDEItemManager.AllSchemas.TryGetValue(schemaKey, out schemaData))
            {
                Dictionary <string, object> itemData = schemaData.DeepCopy();
                itemData.Add(GDMConstants.SchemaKey, schemaKey);

                string error;
                if (GDEItemManager.AddItem(itemName, itemData, out error))
                {
                    SetFoldout(true, itemName);
                    SetNeedToSave(true);
                }
                else
                {
                    result = false;
                    EditorUtility.DisplayDialog(GDEConstants.ErrorCreatingItem, error, GDEConstants.OkLbl);
                }
            }
            else
            {
                result = false;
                EditorUtility.DisplayDialog(GDEConstants.ErrorLbl, GDEConstants.SchemaNotFound + ": " + schemaKey, GDEConstants.OkLbl);
            }

            return(result);
        }
Exemplo n.º 2
0
        void CreateItems(string schemaName, List <FieldInfo> fields, ExcelRange sheetCells)
        {
            Dictionary <string, object> schemaData = null;

            GDEItemManager.AllSchemas.TryGetValue(schemaName, out schemaData);

            // If schema failed to parse (schema data is null), we can't parse any items
            if (schemaData == null)
            {
                return;
            }

            string itemName;
            string error;
            Dictionary <string, object> itemData;

            var itemKeysColumn = (from cell in sheetCells["a:a"]
                                  where cell != null &&
                                  !string.IsNullOrEmpty(cell.Text) &&
                                  !cell.Text.Trim().ToLower().Equals(GDEExcelDataHelper.IgnoreToken) &&
                                  !cell.Text.Trim().ToLower().Equals(GDEExcelDataHelper.FieldNameToken) &&
                                  !cell.Text.Trim().ToLower().Equals(GDEExcelDataHelper.FieldTypeToken)
                                  select cell);

            // Add to the total item count
            totalItems += itemKeysColumn.Count();

            foreach (var keyCell in itemKeysColumn)
            {
                itemName = keyCell.Text.Trim();
                itemData = schemaData.DeepCopy();
                itemData.Add(GDMConstants.SchemaKey, schemaName);

                for (int x = 0; x < fields.Count; x++)
                {
                    try
                    {
                        string        cellText = sheetCells[fields[x].cellCol + keyCell.End.Row].Text;
                        List <object> matches;
                        if (fields[x].skip)
                        {
                            continue;
                        }
                        else if (fields[x].isList)
                        {
                            if (fields[x].type.Equals(BasicFieldType.String) ||
                                fields[x].type.Equals(BasicFieldType.Vector2) ||
                                fields[x].type.Equals(BasicFieldType.Vector3) ||
                                fields[x].type.Equals(BasicFieldType.Vector4) ||
                                fields[x].type.Equals(BasicFieldType.Color) ||
                                GDEItemManager.IsUnityType(fields[x].type) ||
                                fields[x].isCustom)
                            {
                                matches = GDEParser.Parse(cellText);
                            }
                            else
                            {
                                matches = new List <object>(cellText.Split(','));
                            }

                            itemData[fields[x].name] = ConvertListValueToType(fields[x].type, matches);
                        }
                        else if (fields[x].is2DList)
                        {
                            if (fields[x].type.Equals(BasicFieldType.GameObject))
                            {
                                itemData[fields[x].name] = LoadUnityType2DList <GameObject>(cellText);
                            }
                            else if (fields[x].type.Equals(BasicFieldType.Texture2D))
                            {
                                itemData[fields[x].name] = LoadUnityType2DList <Texture2D>(cellText);
                            }
                            else if (fields[x].type.Equals(BasicFieldType.Material))
                            {
                                itemData[fields[x].name] = LoadUnityType2DList <Material>(cellText);
                            }
                            else if (fields[x].type.Equals(BasicFieldType.AudioClip))
                            {
                                itemData[fields[x].name] = LoadUnityType2DList <AudioClip>(cellText);
                            }
                            else
                            {
                                itemData[fields[x].name] = Json.Deserialize(cellText);
                            }
                        }
                        else
                        {
                            if (fields[x].type.Equals(BasicFieldType.Vector2) ||
                                fields[x].type.Equals(BasicFieldType.Vector3) ||
                                fields[x].type.Equals(BasicFieldType.Vector4) ||
                                fields[x].type.Equals(BasicFieldType.Color))
                            {
                                matches = new List <object>(cellText.Split(','));
                                matches.ForEach(obj => obj = obj.ToString().Trim());
                                itemData[fields[x].name]   = ConvertValueToType(fields[x].type, matches);
                            }
                            else
                            {
                                itemData[fields[x].name] = ConvertValueToType(fields[x].type, cellText);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError(ex);
                        Debug.LogError(string.Format(GDEConstants.ErrorParsingCellFormat, schemaName, fields[x].cellCol + keyCell.End.Row));
                        object defaultValue = GDEItemManager.GetDefaultValueForType(fields[x].type);
                        if (fields[x].isList || fields[x].is2DList)
                        {
                            itemData[fields[x].name] = new List <object>();
                        }
                        else
                        {
                            itemData[fields[x].name] = defaultValue;
                        }
                    }
                    processedItems++;

                    if (OnUpdateProgress != null)
                    {
                        OnUpdateProgress(GDEConstants.ImportingGameDataLbl, progressMessage, processedItems / totalItems);
                    }
                }

                GDEItemManager.AddItem(itemName, itemData, out error);
            }
        }