1
\$\begingroup\$

Before building the standalone build I want to update one of the variables in the script using this.

class MyCustomBuildProcessor : IPreprocessBuild
 {
      public int callbackOrder { get { return 0; } }
      public void OnPreprocessBuild(BuildTarget target, string path) {
        // Do the preprocessing here
        CopyRightCls.buildDateTime = System.DateTime.Now;
        Debug.Log("+++++++++++++++++++++++++++++++++++BuildDateTime" + CopyRightCls.buildDateTime);
      }
 }

But the problem is my date variable is not updating its showing 1 january 0001.

\$\endgroup\$
2
  • \$\begingroup\$ Is buildDateTime a static variable? This will get overridden by the initializer when your game runs. Have you tried storing this in an instance variable instead, like in a member of a ScriptableObject? \$\endgroup\$
    – DMGregory
    Feb 19, 2020 at 11:11
  • \$\begingroup\$ Yes it is static, no i never used scriptableobject before. \$\endgroup\$ Feb 19, 2020 at 11:12

1 Answer 1

1
\$\begingroup\$

If you have a static variable like this:

public class CopyRightCls {

    public static System.DateTime buildDateTime;

    //...
}

That translates into code in your built game that says "At startup, initialize buildDateTime to default" (since we didn't specify an initializer, C# structs are default-initialized)

When you set CopyRightCls.buildDateTime = System.DateTime.Now;, what you're setting is the current value in memory within your editor. Nothing serializes that value to disc, or modifies the code above to add an initializer you didn't write. So at startup, you still get the same default-initializing behavior as the runtime executes your code.

To actually save the value (without dynamically modifying your source code), you need to write it down in a serialized data structure - something that will get exported into your game as an asset, and loaded from disc when your game runs.

For example:

[CreateAssetMenu(fileName = "buildInfo.Asset", menuName = "Metadata/Build Info")]
public class BuildInfo : ScriptableObject {
    public System.DateTime buildDateTime;
}

This will let you create a new "Build Info" asset in your project hierarchy. You can assign a reference to this asset in any script that needs to consult the build metadata.

Your build processor can then load this object from the asset database and write the new value into it. Be sure to flag the object with Undo.RecordObject in that method, so the serializer knows you changed some values it needs to commit to the serialized version.

string infoPath = "Assets/Build/buildInfo.asset"; // Or wherever you keep it.
BuildInfo info = (BuildInfo)AssetDatabase.LoadAssetAtPath(infoPath, typeof(BuildInfo));
Undo.RecordObject(info, "Update Build Timestamp");
info.buildDateTime = System.DateTime.Now;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .