Is It Possible to Execute Code Only on Build?

Is it possible to have code that executes only when you do a build, and NOT when the app is run?

We’ve been having some problems with our testers not being sure if they’re testing the current version of an app, as well a me not being sure if I posted the most recent update. I’d like to have a time/date field that gets filled in when the app is built, not every time the app is run. I could do this manually, but that would depend on me not forgetting it and then being in the same boat as we are now.

LOL< I would just get more organized…use http://tortoisesvn.net/

Just make a routine that saves the date/time provided that it hasn’t been saved before. This won’t save it at building time, but it will save it only the first time the app is launched.

I wish. The biggest problem is with our iPad apps. I post them on a web site, and the testers download and install them. Sometimes they don’t download them right away, or worse, they download but don’t run them right away. So saving the date of the first launch wouldn’t help. They could have had the app sitting around for a week before launching it, during which time I may have posted one or more updates. Then they get upset that they spent time finding a bug that’s already been fixed. They’ve been asking for a time stamp they can see in the app and know what version they’re running, and I’ve not been able to come up with a foolproof (i.e., me-proof) solution.

You may have to slap these people around.

Or just use a guiText with the version and build date ont he top somewhere.

That’s my solution so far. But if I forget to update the date field before posting, then we’re as bad or worse off than with no time stamp at all.

I thought there was a way to execute code in the Unity editor that doesn’t run in the executable.

I vaguely remember something about code in the editor folder doing something. I’m not sure.

Put a sticky note on your monitor to remind you.

You could try having PostProcessBuildAttribute generate/update a “build-info” file in the build that is loaded at runtime and used to retrieve the version number.

You could alternatively write a custom build script that uses BuildPipeline.BuildPlayer to carry out the build, but updates your version asset first. I don’t believe it’s possible to do this and also disable the ‘regular’ build though, so you’d have to remember to use the new build option.

Seems like this should work:

@script ExecuteInEditMode()

var timeStamp: SpriteText;

function Update () {
	
    if(EditorApplication.isCompiling) {
    	var myDateTime = new System.DateTime();
    	timeStamp.Text = myDateTime.Now.ToString("MM-dd-yyyy");
    }
}

It does work when I do a build (it puts the date into the field), but stops the build with an error: Unknown identifier: ‘EditorApplication’. That’s if I attach it to a game object. I tried putting it in the Editor folder, but then I can’ assign the text field in the Inspector.

Yeah, anything that requires the UnityEditor namespace can’t be used in a build.

You can probably get around that with this:

#if UNITY_EDITOR
    // Your code...
#endif

Do a search for “c# conditional compilation” for more details on this, but in essence it tells the compiler to only include certain bits of code if a flag has been set, and there are a few flags which Unity sets for you depending on what you’re doing.

However, having an update loop permanently running for this seems like a bit of a kludge to me. It’ll work, but it’s very heavy handed. I’d suggest looking into superpig’s suggestion, as this kind of thing is exactly what post processing stuff is intended for.