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;
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\$