创建一个新文件夹,用于通过C#脚本构建项目。

huangapple go评论73阅读模式
英文:

Create new folder for the project to get build into throught script C#

问题

我找到了一个方法,在构建每次项目时通过互联网的帮助更新版本。

但是,如果我不想覆盖上一次构建,我仍然需要手动创建一个新的文件夹。是否有一种方法可以创建一个文件夹,以unity用来构建游戏的版本命名?

例如:

游戏V1.5 -> 构建到C/Folder/Folder/SaveDestination/V1.5

游戏V1.6 -> 构建到C/Folder/Folder/SaveDestination/V1.6

游戏V1.6.1 -> 构建到C/Folder/Folder/SaveDestination/V1.6.1

...

希望你明白我的意思,对于我的糟糕英语,我在此提前道歉。

英文:

With the help of the internet i found a way to Update the Version of my build each time i build it.

But i still have to create a new folder for it by hand if i dont want to override the last build. Is there a way to create a folder named after the build version that unity uses to build the game into? Like

Game V1.5 -\> Build into C/Folder/Folder/SaveDestination/V1.5

Game V1.6 -\> Build into C/Folder/Folder/SaveDestination/V1.6

Game V1.6.1 -\> Build into C/Folder/Folder/SaveDestination/V1.6.1

...

I hope you understand what I mean and I apologise in advance for my bad english.

答案1

得分: 1

你可以使用Unity的 BuildPipeline.BuildPlayer API 来更改要构建的目录。你可以将其与自己的 BuildPlayerOptions 结合使用,以更改构建的路径。

以下是一个示例,演示如何使用Unity的 MenuItem 来实现这一点。这个脚本获取当前播放器选项的副本,并更改它的 locationPathName 以添加你的版本信息。请注意,你需要将构建器的路径设置为你想要的版本目录的父目录(如果你的期望目录是 C/Folder/Folder/SaveDestination/V1.6.1,构建器的路径应该是 C/Folder/Folder/SaveDestination)。

我现在没有打开Unity,但这段代码应该帮助你了解如何使其工作。

using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Reporting;

public class BuildPlayerWithVersion : MonoBehaviour
{
    [MenuItem("Build/Build With Version")]
    public static void MyBuild()
    {
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); 
        BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(buildPlayerOptions);

        // 在这里获取你的构建版本
        string version = "V1.6.2";
        string path = Path.Join(buildPlayerOptions.locationPathName, version);

        Directory.CreateDirectory(path);      
        buildPlayerOptions.locationPathName = path;
        
        BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
        BuildSummary summary = report.summary;

        if (summary.result == BuildResult.Succeeded)
        {
            Debug.Log("构建成功: " + summary.totalSize + " 字节");
        }

        if (summary.result == BuildResult.Failed)
        {
            Debug.Log("构建失败");
        }
    }
}
英文:

You can use Unity's BuildPipeline.BuildPlayer API to change the directory to which you want to build. You can combine this with your own BuildPlayerOptions to change the path to your build.

Below is an example on how you could achieve this using Unity's MenuItem. This script gets a copy of the current player options and changes it's locationPathName to add your version to it. Do note that you need to set your builder's path to the parent directory of where you want your versioned directories to be (if your desired directory is C/Folder/Folder/SaveDestination/V1.6.1, the builder's path should be C/Folder/Folder/SaveDestination).

I don't have Unity open right now, but this code should give you the gist of how to get things to work.

using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Reporting;

public class BuildPlayerWithVersion : MonoBehaviour
{
    [MenuItem("Build/Build With Version")]
    public static void MyBuild()
    {
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); 
        BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(buildPlayerOptions);

        // Get your build version here
        string version = "V1.6.2"
        string path = Path.Join(buildPlayerOptions.locationPathName, version);

        Directory.CreateDirectory(path);      
        buildPlayerOptions.locationPathName = path;
        
        BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
        BuildSummary summary = report.summary;

        if (summary.result == BuildResult.Succeeded)
        {
            Debug.Log("Build succeeded: " + summary.totalSize + " bytes");
        }

        if (summary.result == BuildResult.Failed)
        {
            Debug.Log("Build failed");
        }
    }
}

huangapple
  • 本文由 发表于 2023年3月4日 02:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定