Preprocessor directives forced to be upper case
Hello! 👋
I am working on a project using third-party assets using #define preprocessor directives.
Currently, BuildProject.GenerateDefaultDefines (see here) collects the defines of the given release type and calls the string extension method SanitizeDefine (see here) which transforms the define to uppercase. Because preprocessor directives are case-sensitive, the third-party code between the directives is stripped, which causes issues in the build.
// Editor/Build/BuildProject.cs
public static string GenerateDefaultDefines(BuildReleaseType releaseType, BuildPlatform platform, BuildArchitecture arch,
BuildScriptingBackend scriptingBackend, BuildDistribution dist)
{
// ...
if (releaseType != null && !string.IsNullOrEmpty(releaseType.customDefines))
{
string[] customDefines = releaseType.customDefines.Split(';', ',');
for (int i = 0; i < customDefines.Length; i++)
{
defines.Add(customDefines[i].SanitizeDefine());
}
}
return string.Join(";", defines.ToArray());
}
// Editor/Generic/ExtensionMethods.cs
public static string SanitizeDefine(this string input)
{
return input.ToUpperInvariant().Replace(" ", "").SanitizeCodeString();
}
Is there any reason to force the defines to be uppercase?
Thanks in advance.
Info
Unity version: Unity 2021.3.21f1 Platform: Windows Target build platform tested: Windows
Reproduction steps:
- Create a new scene, leave the camera and light unchanged
- Create a new script called
NewBehaviourScriptwith the following content, and attach it to an empty object you place in front of the default camera
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private GameObject _gameObject;
void Start()
{
_gameObject = UnityEngine.GameObject.CreatePrimitive(PrimitiveType.Cube);
}
void Update()
{
#if MyDefine
_gameObject.transform.localScale = Vector3.one * Mathf.Sin(Time.time) * 5;
#else
_gameObject.transform.position = new Vector3(Mathf.Cos(Time.time), Mathf.Sin(Time.time), 0) * 4;
#endif
}
}
- In SuperUnityBuild, define a new release type with the following define:
MyDefine, add your empty scene to the scene list - Create a new build platform you can build to
- Click on the build configuration you've created from the
Build Configurationsoptions at the bottom of the window, and click onSelected Build Info - Observe in the
Definessection that your define has been transformed to upper-case - Build the game
- Observe that the cube is moving in circles (code outside of the MyDefine) instead of growing and shrinking (code between MyDefine)
Note that the issue can be resolved by doing the following:
- Remove the define(s) with lower case characters
- Add an
OverrideDefinesBuildAction (see BuildActions repository) - Add your define in the
Add Definesfield
There is discrepancy between the "core" code and that build action.