I think I’ve found yet another way to do this. I don’t know if it’s considered “hacky” or not (CLR noob here, I usually work on the JVM) but you can manually edit your *.csproj file and include the source files in folders from somewhere else. So, the common library is currently residing within the unity project (no harm done, and unity is the more sensitive environment due to AOT compilation for WebGL), and in the web API project I do this in the *.csproj file:
<ItemGroup>
<Compile Include="..\..\unity\MyProject\Assets\Code\Main\Common\**\*.cs">
<Link>Common\%(RecursiveDir)%(FileName)%(Extension)</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
At the very least, Rider seems to have zero issues with this. As an added bonus, I can now go ahead and edit the files from both projects easily. The only unity-import the common module had was for logging, and while that could be thrown out entirely, I’m trying a conditional compilation approach:
namespace myproject.common.util
public static class Log {
public static void Info(string message,
[System.Runtime.CompilerServices.CallerMemberName]
string callerClassName = "",
[System.Runtime.CompilerServices.CallerLineNumber]
int sourceLineNumber = 0
) {
var msg = Format("INFO", callerClassName, sourceLineNumber, message);
#if UNITY_EDITOR
UnityEngine.Debug.Log(msg);
#elif UNITY_WEBGL
UnityEngine.Debug.Log(msg);
#else
System.Console.WriteLine(msg);
#endif
}
public static void Warn(string message,
[System.Runtime.CompilerServices.CallerMemberName]
string callerClassName = "",
[System.Runtime.CompilerServices.CallerLineNumber]
int sourceLineNumber = 0
) {
var msg = Format("WARN", callerClassName, sourceLineNumber, message);
#if UNITY_EDITOR
UnityEngine.Debug.LogWarning(msg);
#elif UNITY_WEBGL
UnityEngine.Debug.LogWarning(msg);
#else
System.Console.WriteLine(msg);
#endif
}
public static void Error(string message,
[System.Runtime.CompilerServices.CallerMemberName]
string callerClassName = "",
[System.Runtime.CompilerServices.CallerLineNumber]
int sourceLineNumber = 0
) {
var msg = Format("ERROR", callerClassName, sourceLineNumber, message);
#if UNITY_EDITOR
UnityEngine.Debug.LogError(msg);
#elif UNITY_WEBGL
UnityEngine.Debug.LogError(msg);
#else
System.Console.WriteLine(msg);
#endif
}
private static string Format(string level, string callerClass, int lineNumber, string message) {
return $"[{level}] [{callerClass,32}:{lineNumber:000}] {message}";
}
}
This solution seems to work. Both unity and rider are happy, and I can actually already run the web API, so that seems to be good to go too.
Am I doing something truly dirty here? I genuinely have no idea, but it seems to work okay.