game-ci / documentation

📚 Documentation for GameCI open source projects
https://game.ci
MIT License
183 stars 118 forks source link

Add an example for setting preprocessor macros #473

Open efarraro opened 1 month ago

efarraro commented 1 month ago

Context

Sometimes you want to include a preprocessor macro in your build (eg: #ANDROID, #DEMO, etc... ) in order to change what gets compiled into the build. Unity supports this behavior in the editor.

Suggested solution

Add the following example outlined here as an example in the 'Builder' section of the official wiki: https://github.com/game-ci/unity-builder/issues/207

(Credit to @xwipeoutx for posting the following solution)

steps:
  - name: Apply csc.rsp (pre-processor symbols)
    run: echo -define:MY_SYMBOL >> Assets/csc.rsp

You will get warnings about "unapplied changes" - you could get around this by putting csc.rsp in your .gitignore, or allowing dirty build.

with:
  # ...
  allowDirtyBuild: true

Now in your code you can just do this:

void Start() {
#if MY_SYMBOL
  GetComponent<TMP_Text>().text = "I have a symbol!";
#else
  GetComponent<TMP_Text>().text = "I do not have a symbol...";
#endif
}

See Unity docs for the inspiration - under the heading "Setting scripting symbols via an asset file"

Originally posted by @xwipeoutx in https://github.com/game-ci/unity-builder/issues/207#issuecomment-2269981553

webbertakken commented 1 month ago

Accepting PRs.

We will not suggest using a dirty build if you can simply ignore that file. The dirty check is there to ensure no files were accidentially changed during or after checkout and would effectively be disabled by this measure. The docs should therefor not suggest using allowDirtyBuild for this purpose.

webbertakken commented 1 month ago

cc @xwipeoutx

efarraro commented 1 month ago

I still need to run yarn / test my changes locally, but I put up an initial PR and would appreciate any feedback, to make sure this is on the right track https://github.com/game-ci/documentation/pull/474