Ejemplo n.º 1
0
    public void ExecuteImport()
    {
        //create the folders
        foreach (C_RowData data in importData.rows)
        {
            string subFolder        = data.entries[importData.groupIndex];
            string importFolderPath = importFolder + "/" + subFolder;

            //test if group name is correct
            if (string.IsNullOrEmpty(subFolder))
            {
                importState = E_ImportState.Imported_Error;
                importInfo  = "'GroupName' can't be empty. Please reimport correct data set.";
                return;
            }

            //test if card name is correct
            if (string.IsNullOrEmpty(data.entries[importData.cardNameIndex]))
            {
                importState = E_ImportState.Imported_Error;
                importInfo  = "'CardName' can't be empty. Please reimport correct data set.";
                return;
            }

            if (importFolderPath.StartsWith(Application.dataPath))
            {
                data.importFolderPath = "Assets" + importFolderPath.Substring(Application.dataPath.Length);
                data.importFilePath   = data.importFolderPath + "/" + data.entries[importData.cardNameIndex] + ".prefab";
            }
            else
            {
                importInfo  = "The Path '" + importFolderPath + "' is not part of the actual project and can't be used.";
                importState = E_ImportState.Analyzed_Error;
                return;
            }

            //create directory if it doesn't exist yet
            if (!Directory.Exists(importFolderPath))
            {
                //if it doesn't exist, create it
                Directory.CreateDirectory(importFolderPath);
                Debug.Log("Create " + importFolderPath);
            }
        }

        //test if prefab exists
        foreach (C_RowData data in importData.rows)
        {
            string         styleName  = data.entries[importData.styleIndex];
            GameObject     cardPrefab = (GameObject)AssetDatabase.LoadAssetAtPath(data.importFilePath, typeof(GameObject));
            KingsCardStyle cardStyle  = styleDefinitions.GetStyle(styleName);

#if UNITY_2018_3_OR_NEWER
            //to get the nested prefabs working a slightly different approach is necessary
            //because actualization is done in between, calling a function would generate an ugly parameterlist
            //=> doing it in this place
            {
                GameObject instanceRoot = null;

                if (cardPrefab == null)
                {
                    if (cardStyle.prefab != null)
                    {
                        // make an instance of the prefab
                        instanceRoot = (GameObject)PrefabUtility.InstantiatePrefab(cardStyle.prefab);
                        Debug.Log("Creating card '" + data.entries[importData.cardNameIndex] + "' based on prefab '" + cardStyle.prefab.name + "' of the cardstyle '" + cardStyle.name + "'.");
                    }
                    else
                    {
                        Debug.LogError("Actualization/Creation of element failed. The prefab at the cardStyle '" + cardStyle.name + "' is 'null' (missing).");
                    }
                }
                else
                {
                    //an actual prefab exists
                    // make an instance of the already existing prefab
                    instanceRoot = (GameObject)PrefabUtility.InstantiatePrefab(cardPrefab);
                    Debug.Log("Actualizing the card prefab '" + cardPrefab.name + "', the linked prefab '" + cardStyle.prefab.name + "' of the cardstyle '" + cardStyle.name + "' is ignored because the prefab already exists.");
                }

                if (instanceRoot != null)
                {
                    // actualize the spawned object
                    ActualizeCard(instanceRoot, data, styleDefinitions, styleName);
                    // create prefab of the changed object
                    bool success = false;
                    UnityEngine.Object prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(instanceRoot, data.importFilePath, InteractionMode.AutomatedAction, out success);

                    if (success == false)
                    {
                        Debug.LogError("The saving as prefab asset was unsuccessful.");
                    }

                    // destroy the spawned object
                    DestroyImmediate(instanceRoot);
                }
                else
                {
                    //some error messages if something failed with the instance Root
                    if (cardStyle.prefab == null)
                    {
                        Debug.LogError("Actualization/Creation of element failed. The prefab at the cardStyle is 'null' (missing).");
                    }
                    else
                    {
                        Debug.LogError("Actualization/Creation of prafab '" + cardStyle.prefab.name + "' failed.");
                    }
                }
            }
#else
            //import precedure for older unity versions

            //If the name doesn't exist, create the new Prefab
            if (cardPrefab == null)
            {
                cardPrefab = CreateNewCard(cardStyle.prefab, data.importFilePath);
            }

            ActualizeCard(cardPrefab, data, styleDefinitions, styleName);
#endif
        }

        importState = E_ImportState.Imported_OK;
        importInfo  = "Import done.\n" + DateTime.Now.ToShortTimeString();
    }