2

I have to copy two text files and a folder to my build data folder after finishing the build (exe) for windows platform. Each time I build, I want to copy these files and folder from assets to build data folder. Is this possible in unity? Cause sometime I forget to copy desired file in data folder and my player does not work correctly.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186

2 Answers2

8

You can subscribe a static method to PostprocessBuild.OnPostprocessBuild This method will be called every time a build is finished. So your code might look like this:

using UnityEditor;
using System.IO;
using UnityEditor.Build;
using UnityEngine;

class MyCustomBuildProcessor : IPostprocessBuild
{
    public int callbackOrder { get { return 0; } }
    public void OnPostprocessBuild(BuildTarget target, string path)
    {
        File.Copy("sourceFilePath","destinationFilePath");
    }
}
  • you have forget to add system.io – Muhammad Faizan Khan Nov 13 '17 at 04:46
  • Good point, fixed that. Well, you could also use an editor window to set the paths and store them for example in your EditorPrefs (https://docs.unity3d.com/ScriptReference/EditorPrefs.html). To get the path to your project you can use Application.dataPath. – Johannes Deml Nov 14 '17 at 17:41
1

It sounds like you want your own custom editor build function. You can do this with a custom editor menu item.

Unity Editor Extensions – Menu Items

Here is what the code might look like:

[MenuItem("MyBuildMenu/MyBuildMenuItem")]
static void BuildAndCopyItems()
{
    File.Copy("path1","path2"); // copy your files
    BuildPipeline.BuildPlayer(); // build the player
}

you'll need to pass the correct arguments to BuildPlayer:

BuildPipeline.BuildPlayer

The file containing this script will need to be within your project in a folder called 'Editor'.

And you'll get a new menu item in the Unity editor to perform your custom build:

enter image description here

lockstock
  • 2,359
  • 3
  • 23
  • 39