Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by kromenak · Jul 19, 2011 at 06:57 PM · versionbuildpipelineautomatic

How to automatically run code before a build?

Hey everyone,

I'm working on getting a simple build versioning system in place that will basically store a version number in a game object and allow me to display the number during the game. This works, but it required me to create a menu item called "Increment Build Number" and I have to manually run it whenever I create a build.

I know there is a PostprocessPlayer functionality to do things after a build has occurred, but is there any way to run some code right before a build takes place? If so, I could just automatically increment my build number there instead of doing it manually.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Wollbobaggins · Aug 24, 2021 at 06:37 PM

For anyone looking into this today, you can now implement, IPreprocessBuildWithReport, to receive a callback before the build is started.

Here is the example provided on the unity documentation:

 using UnityEditor;
 using UnityEditor.Build;
 using UnityEditor.Build.Reporting;
 using UnityEngine;
 
 class MyCustomBuildProcessor : IPreprocessBuildWithReport
 {
     public int callbackOrder { get { return 0; } }
     public void OnPreprocessBuild(BuildReport report)
     {
         Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + 
             report.summary.platform + " at path " + report.summary.outputPath);
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by teslatron · Apr 28, 2014 at 02:37 PM

You should write your own build script for this. Recently I did exactly what you wanted, showing a version in the game. I generate a file in "Resources" folder which contains version number. Then in game I open this file as text asset and get version number. I generate version according to build date and time, so I don't need to increment it automatically. But with some logic change that can also be done. Read the last value, increment and save...ect.

These are in BuildScript.cs

BuildClient method:

 [MenuItem("Inspire13/Build Test Client #F5")]
 public static void BuildClient() 
 {
     //KillClients(); // You can kill the running processes here before build.
     SignVersionFile();
     UnityEngine.Debug.Log("Building Client, Directory: " + mClientBuildDirectory);
 
     try
     {
         PlayerSettings.productName = "TestClient";
         string[] scenes = new string[] { "Assets/_Scenes/Level1.unity", "Assets/_Scenes/Level2" };
 
         // Build player
         string fileFullPath = mClientBuildDirectory + "/" + mClientBinaryName + ".exe";
         BuildPipeline.BuildPlayer(scenes, fileFullPath, BuildTarget.StandaloneWindows, BuildOptions.Development);
 
         UnityEngine.Debug.Log("Built!, File: " + fileFullPath);
     }
     catch (Exception e)
     {
         UnityEngine.Debug.LogException(e);
     }     
 }

Don't mind the local variables "mClientBuildDirectory, mClientBinaryName...ect"

And here is my version file generator method SignVersionFile method:

 static void SignVersionFile() 
 {
     UnityEngine.Debug.Log("Signing version file...");
 
     try
     {
         string versionFilePath = Application.dataPath + "/Resources/" + VersionFile;
 
         // Writes with UTF-8 without BOM (byte order mark)
         using (StreamWriter writer = new StreamWriter(versionFilePath))
         {
             string versionDate = DateTime.Now.ToString("ddMM-HHmm", CultureInfo.InvariantCulture);
             string versionLine = String.Format("{0}.{1}.{2}", MainVersion, SubVersion, versionDate);
             writer.Write(versionLine);
         }
 
         AssetDatabase.Refresh();
         UnityEngine.Debug.Log("Signed!");
     }
     catch (Exception e)
     {
         UnityEngine.Debug.LogException(e);
     }
 }

If you don't want to write your own build script, you can do the similar thing with PostProcessSceneAttribute: http://docs.unity3d.com/412/Documentation/ScriptReference/PostProcessSceneAttribute.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hantaro · Apr 05, 2016 at 01:22 PM 0
Share

Hey there!

I couldn't help but notice you have that method $$anonymous$$illClients();

What does it have inside? I am trying to stop the build pipeline if one of my postProcessor requirements are not fulfilled but I am having a hard time.

Thanks in Advance, Cai

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Automate iPhone build 1 Answer

Custom DLL: mybuildcode.dll is identical to mybuildpluseditor.dll, but does not work in build 0 Answers

How can I get Unity to respect mcs.rsp changes between multiple builds? 0 Answers

Automatic Screen Resize 4 Answers

Scripting ragdoll creation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges