5
\$\begingroup\$

I would like to access my app name in a Unity C# script; I've found PlayerSettings.productName, however it requires UnityEditor, which is only available in the editor.

So, in a normal application, how am I supposed to access its name, as it appears in the "product name" field of the player settings?

  • It must be cross-platform
  • retrieving the executable name (or directory) does not count
  • duplicating data does not count (obviously I can create a const with the same name, the whole point of this question is to avoid doing that)

(since the goal is to avoid duplicating data, backwards techniques such as having a const, and using that const to update PlayerSettings.productName is ok, as long as they require no extra step, i.e. they can be made to "run" automatically at compile time)

\$\endgroup\$
  • \$\begingroup\$ If you don't find an answer to this problem, another option is to just set a static variable in a class that contains this data and call that in your script. \$\endgroup\$ – SpartanDonut Jan 13 '14 at 13:57
  • \$\begingroup\$ @ToddersLegrande sure, that is the obvious solution, but it duplicates data, and it is always best to avoid duplicating data, since your risk it going out-of-sync \$\endgroup\$ – o0'. Jan 13 '14 at 14:40
  • 1
    \$\begingroup\$ Agreed. I hope you find the answer you are looking for. Just pointing out a work around incase it was something you hadn't thought of. \$\endgroup\$ – SpartanDonut Jan 13 '14 at 14:41
  • \$\begingroup\$ If I was simply answering your question "how am I supposed to access its name", I think the answer is "you aren't." The product name isn't anything special that you should need access to it at runtime, so I don't think Unity have bothered to let you access it. Nevertheless, I've given an answer below. \$\endgroup\$ – MrCranky Jan 16 '14 at 12:18
  • \$\begingroup\$ @MrCranky I've no clue why you think I should not need to access it, but your answer is quite intresting, so I appreciate it! \$\endgroup\$ – o0'. Jan 16 '14 at 13:46
2
\$\begingroup\$

I don't believe it's possible simply, i.e. there's not an API in some UnityEngine class that will let you get it at runtime that you simply haven't found.

There are two ways of going about this really. The first is to slave the productName variable to a static constant in your scripts that you can also access at runtime. I.e. add a static readonly string to a game class/script. Then add a step to your build process (which exists in the context of the Editor) that forcibly sets PlayerSettings.productName to be the same as your own value.

The second way would be to save a data file in Resources with your product name in it as part of your build process. As in, as a pre-build step, look up what PlayerSettings.productName is and save it out into a text file in an Assets/Resources sub-folder called 'productName.txt'. Then at runtime, call Resources.Load("productName") as TextAsset, and parse the productName out of the text asset.

Personally I'd say that both approaches are more work than they're worth, and you should simply have two copies of the name. Maybe add an Editor verification function that you can habitually call to double check that they're still in-sync during development and prior to making a release.

\$\endgroup\$
  • \$\begingroup\$ Those pre-build step techniques are actually interesting, I'll investigate, thanks! : ) \$\endgroup\$ – o0'. Jan 16 '14 at 13:42
  • \$\begingroup\$ No time to further investigate now, so meanwhile I'm just accepting this, trusting it will work : ) \$\endgroup\$ – o0'. Jan 27 '14 at 15:11
  • \$\begingroup\$ "Then add a step to your build process (which exists in the context of the Editor) that forcibly sets PlayerSettings.productName to be the same as your own value." I came here looking for exactly how to do this! Been googling for an hour or so and haven't figured it out yet. \$\endgroup\$ – livingtech Apr 27 '16 at 22:29
0
\$\begingroup\$
//Save bundleVersion in PlayerPrefs and access it from anywhere
//This class must be in "Editor" folder to work

using UnityEngine;
using UnityEditor;
using System.IO;

[InitializeOnLoad]
public class BundleVersionChecker
{

    static BundleVersionChecker () 
    {
        string bundleVersion = PlayerSettings.bundleVersion;
        PlayerPrefs.SetString("Key_AppBundleVersion", bundleVersion);
    }

}
\$\endgroup\$
-1
\$\begingroup\$

I thought you were looking for:

UnityEditor.PlayerSettings.productName

But I then realized you want this at runtime. Because the project needs to keep the same name as the folder its in, you can use something like this.

public string GetProjectName() {
    string[] s = Application.dataPath.Split('/');
    string projectName = s[s.Length - 2];
    return projectName;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ The dataPath reference (docs.unity3d.com/Documentation/ScriptReference/…) says that on Windows standalone builds, Web and Flash builds, the path doesn't necessarily end with a subdirectory. I think that means this answer will not work on those platforms (it assumes that the folder above the folder the dataPath refers to is also named with the productName, and I don't think the documentation guarantees that). It also doesn't account for the iOS version (productName.app) \$\endgroup\$ – MrCranky Jan 16 '14 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.