简体   繁体   English

Unity脚本仅一次创建资产

[英]Unity script to create assets one time only

I'm trying to make a TCG (trading card game) in Unity as an University assignment. 我正在尝试在Unity中作为大学作业进行TCG(交易卡游戏)。 I have some scriptable objects to create different types of cards. 我有一些可编写脚本的对象来创建不同类型的卡。 The problem is I have 600+ cards so it would be nice to have a script that creates a card asset for every card and sets their parameters(load images, texts). 问题是我有600多张卡片,所以最好有一个脚本为每张卡片创建卡片资产并设置其参数(加载图像,文本)。

Is there any way to achieve this? 有什么办法可以做到这一点? It is important that the script runs only once, not every time I open the editor or I start a new instance of the game. 重要的是,脚本只能运行一次,而不是每次打开编辑器或启动游戏的新实例时都运行一次。

You can implement a method that is only called via a specific button in the UnityEditor that generates all your assets using the MenuItem attribute: 您可以实现仅通过UnityEditor中的特定按钮调用的方法,该方法使用MenuItem属性生成所有资产:

#if UNITY_EDITOR

    using System.IO;
    using UnityEditor;
    using UnityEngine;

    public static class YourExtensions
    {
        [MenuItem("YourMenu/GenerateAssets")]
        private static void GenerateAssets()
        {
            // TODO Get and parse required information e.g. from Database / Xml or Text file
            // I'ld simply make a new class for that like "CardInformation"

            // List<CardInformation>() cardlist = new List<CardInformation>();


            // Somehow receive your information
            // cardlist.add(new CardInformation(parameters));



            // TODO Generate Scriptableobjects

            // foreach(var cardInfo in cardlist){
                    YourScriptableObjectClass asset = ScriptableObject.CreateInstance<YourScriptableObjectClass > ();

                    string path = AssetDatabase.GetAssetPath (Selection.activeObject);
                    if (path == "") 
                    {
                        path = "Assets";
                    } 
                    else if (Path.GetExtension (path) != "") 
                    {
                        path = path.Replace (Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
                    }

                    string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/New " + typeof(YourScriptableObjectClass ).ToString() + ".asset");

                    AssetDatabase.CreateAsset (asset, assetPathAndName);

                    AssetDatabase.SaveAssets ();
                    AssetDatabase.Refresh();
                    EditorUtility.FocusProjectWindow ();
                    Selection.activeObject = asset;

                    // TODO here set your passed parameters for this asset
                    // asset.parameter = cardInfo.parameters;
            // end foreach
        }

    }

#endif

Source for AssetCreation: CreateScriptableObjectAsset AssetCreation的来源: CreateScriptableObjectAsset

This method will never be called unless you click the Button in the top Menubar of the Unity Editor. 除非您单击Unity编辑器顶部菜单栏中的按钮,否则永远不会调用此方法。

Note: You can skip the #if UNITY_EDITOR pre-processors if you simply place that script into a folder called Editor . 注意:如果只是将脚本放入名为Editor的文件夹中,则可以跳过#if UNITY_EDITOR预处理程序。 Those scripts are automatically excluded from any build. 这些脚本会自动从任何版本中排除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM