示例#1
0
        private void GeneratePrefabs(string targetPath)
        {
            // confirmation dialog
            if (overwrite)
            {
                bool check = EditorUtility.DisplayDialog("Overwrite Prefabs?", "Are you sure you want to overwrite existing prefabs? This is permanent.", "Proceed", "Cancel");
                if (!check)
                {
                    return;
                }
            }

            CardController.Faction faction;
            // make the path relative to the project
            if (targetPath.StartsWith(Application.dataPath))
            {
                // get faction name based on folder name
                string factionName = targetPath.Substring(Application.dataPath.Length).Substring("/Resources/Cards/".Length);

                var result = System.Enum.TryParse <CardController.Faction>(factionName, out faction);
                if (!result)
                {
                    Debug.LogError("could not parse faction folder name to CardController enum");
                    return;
                }
                Debug.Log("Matched as faction: " + faction);
                targetPath = "Assets" + targetPath.Substring(Application.dataPath.Length) + "/";
            }
            else
            {
                Debug.LogError("invalid folder selected");
                return;
            }

            // resources.load expects a different relative path, prep that
            string assetPathPrefix = "Assets/Resources/";

            string resourcesLoadPath = targetPath;

            if (resourcesLoadPath.StartsWith(assetPathPrefix))
            {
                resourcesLoadPath = resourcesLoadPath.Substring(assetPathPrefix.Length);
            }

            cardsJson = Resources.Load(resourcesLoadPath + "cards");
            if (!cardsJson)
            {
                Debug.LogError("cards json was not found in the folder");
                return;
            }

            if (!cardsJson || targetPath == "" || !cardPrefab)
            {
                Debug.LogError("not the right stuff supplied");
                return;
            }

            // save data as per CardDataHolder
            CardDataCollection cardDataCollection = JsonUtility.FromJson <CardDataCollection>(cardsJson.ToString());
            int generatedCount = 0;
            int destroyedCount = 0;
            int skippedCount   = 0;
            int errorCount     = 0;

            List <string> devNameList = new List <string>();

            foreach (CardDataHolder card in cardDataCollection.cards)
            {
                if (card.devName == "")
                {
                    Debug.LogWarning("empty devName, likely incomplete card, skipping");
                    errorCount += 1;
                    continue;
                }

                // check to see if prefab exists, handle as specified
                GameObject existingPrefab = null;
                bool       prefabExists   = true;

                existingPrefab = (GameObject)Resources.Load(resourcesLoadPath + card.devName);
                if (existingPrefab == null)
                {
                    prefabExists = false;
                }

                if (!overwrite && prefabExists)
                {
                    // we'll need to add these ones to the list of cards to make sure we get all
                    devNameList.Add(existingPrefab.name);

                    skippedCount += 1;
                    continue;
                }
                else if (overwrite && prefabExists)
                {
                    DestroyImmediate(existingPrefab, true);
                    destroyedCount += 1;
                }

                // instantiate the CardBase prefab - this will bring all 3D stuff etc with it
                // add components and set properties as required

                GameObject newCard = (GameObject)PrefabUtility.InstantiatePrefab(cardPrefab);
                newCard.name = card.devName;

                CardController newCard_CC = newCard.GetComponent <CardController>();
                newCard_CC.displayName = card.Name;
                newCard_CC.faction     = faction;

                CardController.SlotType newCardSlot;

                if (System.Enum.TryParse <CardController.SlotType>(card.SlotType, out newCardSlot))
                {
                    // these two TP only
                    newCard_CC.effectText = card.Effect;

                    newCard_CC.SetBaseStats(card.Cost, card.Power, card.Initiative, card.Armour, card.Life);

                    // todo: remove these when effects are more implemented
                    newCard_CC.claimText   = card.Claim;
                    newCard_CC.specialText = card.Special;
                    newCard_CC.passiveText = card.Passive;
                    newCard_CC.effectText  = card.Effect;

                    // do we need to store this?
                    newCard_CC.devName = card.devName;

                    newCard_CC.slotType = newCardSlot;
                }
                else
                {
                    Debug.LogError("Invalid slottype, skipping");
                    errorCount += 1;
                    continue;
                }

                // check over every Effect Type defined, check their custom attribute to see if they are for this card

                foreach (Type t in GetAllEffectTypes())
                {
                    newCard.AddComponent(t);
                }
                foreach (var comp in newCard.GetComponents <CardEffectBase>())
                {
                    if (!comp.GetType().CustomAttributes.First().AttributeType.Name.Contains("064_ophelia"))
                    {
                        DestroyImmediate(comp);
                    }
                    else
                    {
                        if (comp.effectType == GamePlayConstants.EffectType.Claim && newCard_CC.claimText != "")
                        {
                            comp.fullText = newCard_CC.claimText;
                        }
                        if (comp.effectType == GamePlayConstants.EffectType.Passive && newCard_CC.passiveText != "")
                        {
                            comp.fullText = newCard_CC.passiveText;
                        }
                        if (comp.effectType == GamePlayConstants.EffectType.Special && newCard_CC.specialText != "")
                        {
                            comp.fullText = newCard_CC.specialText;
                        }
                        if (comp.effectType == GamePlayConstants.EffectType.TurningPoint && newCard_CC.effectText != "")
                        {
                            comp.fullText = newCard_CC.effectText;
                        }
                    }
                }


                // convert the gameobject to a prefab and store in the specified folder
                string localPath = targetPath + newCard.name + ".prefab";
                PrefabUtility.SaveAsPrefabAsset(newCard, localPath);

                // add the devName to list of devnames, with prefix for loading
                devNameList.Add("Cards/" + faction + "/" + newCard.name);

                // destory the gameobject we created in the scene
                DestroyImmediate(newCard);

                generatedCount += 1;
            }

            // save all the devnames
            SaveCardList(devNameList, faction);

            Debug.Log("Finished. Generated " + generatedCount + ", Destroyed " + destroyedCount + ", Skipped " + skippedCount + ", Errored " + errorCount);
        }