コード例 #1
0
    public static void CreateAsset(ScriptableObject o, string name)
    {
        string path;
        if (o.GetType() == typeof(Quest))
        {
            path = "Assets/Resources/Quests";
        }
        else
            path = "Assets/Resources/Items";

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

        string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/" + name + ".asset");
        if (!AssetDatabase.Contains(o))
        {
            AssetDatabase.CreateAsset(o, assetPathAndName);
            Debug.Log("Asset has been created in " + assetPathAndName);
        }
        else
        {
            Debug.Log("Asset has already been in database in " + o.ToString());
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = o;
            return;
        }

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