- Home /
Using Environment variables (Such as keys to external services)
My game is open source (on Github: https://github.com/bdickason/stormsword) which adds some interesting challenges when working with 'protected' data.
We are trying to implement MixPanel for tracking things like the user's OS, how many times they've loaded the game, etc.
I have a background in web development and would normally set an environment variable on the server which allows different environments (dev, production, per-developer) to have different keys configured and loaded in at run-time.
I would like to avoid storing our MixPanel key in the code, both because I have learned better than doing this, and because I don't want to expose our Mixpanel key publicly in our Github repo.
I've looked into GetEnvioronmentVariable but have not been able to figure out how to get Unity to read from the current environment (for example if I add something to my ~/.profile script it doesn't show up). I also will need to figure out how to make this work on Windows as some of our devs use Windows, some OSX, some Linux.
I also have looked at the PlayerPrefs scripting API but this seems to be used more for player Data.
Any advice would be appreciated!
Figure out a solution for this? Im surprised I cant find any support for this
@mitchmeyer1 unfortunately I wasn't able to find a good solution for this and ended up punting on $$anonymous$$ixpanel support. I suspect it's a common problem and I was also surprised to find little to no support/solutions available for this :(
Don't let the name PlayerPrefs through you off: they don't mean "player" as in "player 1", they mean player as in the "Unity program player", or the DVD "player". This is a fine place to store client side configuration information.
I get playerprefs, they are very useful for certain things. But I was asking more about @bdickason's comments concerning Environment variables. $$anonymous$$y app signs in to local API, dev API, and prod API based on if my clients using it vs me making changes locally and testing. Would be great if i could use the dot net GetEnvironmentVariables to see where the instance of the app is running, but my user and process variables come back blank. Only system env vars come back and thats not where i store my api_url variable
to expand on that, i have a staging webgl build and a production webgl build. It would be nice if they could read an env var in their environments and point to the correct APIs based on that
try System.Environment.CurrentDirectory
? Other useful stuff in that class to..maybe that will help? https://msdn.microsoft.com/en-us/library/system.environment(v=vs.110).aspx
Answer by ContractorNation · Jul 23, 2020 at 08:22 PM
string dosFilePath = @"%USERPROFILE%\Documents\myFile.txt";
Debug.Log(System.Environment.ExpandEnvironmentVariables(dosFilePath));
// C:\Users\myUser\Documents\myFile.txt