- Home /
Unity 5 PlayerSettings.bundleVersion unknown identifier : 'PlayerSettings' javascript-C#
Hello everyone,
I need to get the bundle version of the game in runtime. PlayerSettings.bundleVersion works good in unity editor but when building for Android, it says unknown identifier : 'PlayerSettings' .I watn to get the bundle version code in runtime. How can i make it work ?
Well i tried something like platform dependent compilation but it didn't work either. It was like :
#if UNITY_EDITOR
Debug.Log(PlayerSettings.bundleVersion);
#endif
#if UNITY_ANDROID
Debug.Log(PlayerSettings.Android.bundleVersion);
#endif
PlayerSettings in UNITY_EDITOR tags works good but PlayerSettings in UNITY_ANDROID tags gives same error, unknown identifier.
Thanks in advance.
Answer by NicoL3AS · May 05, 2016 at 05:02 AM
Hi guys, I may have a more elegant solution:
0 - Let's assume you want to access the bundleVersion in a script named MyAwesomeScript, attached to the GameObject named MyBeautifulObject.
1 - Create a C# script called RuntimePlayerSettings with the following code:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteInEditMode]
public class RuntimePlayerSettings : MonoBehaviour
{
public string bundleVersion;
#if UNITY_EDITOR
void Update () {
bundleVersion = PlayerSettings.bundleVersion;
}
#endif
}
2 - Attach this RuntimePlayerSettings script to MyBeautifulObject.
3 - In MyAwesomeScript, you can get the bundleVersion like that :
string version = gameObject.GetComponent<RuntimePlayerSettings>().bundleVersion;
It should works on all platforms (editor included) and, for devices, it will get the last bundleVersion entered before compiling.
Of course, it does not work only for bundleVersion. You can add as many static variables of PlayerSettings as you want to RuntimePlayerSettings.
Unity uses scripts in 2 ways: in the editor and in the running game. Problem is: "PlayerSettings.bundleVersion" is not accessible in the running game, only in the editor. Fortunately, scripts parameters (like the string "bundleVersion" you can see in my example) are both accessible from the editor and from the game. So with this script, in the editor, each time the bundle version is updated, I update the parameter "bundleVersion" with the value of "PlayerSettings.bundleVersion". So when the game is exported, it automatically export the last bundle version stored in the script parameter. Not sure I am clear enough. Do you get it?
Thank you for the explanation. At first I wasn't able to understand how a variable in memory is carried from editor to the build. After your comment I realized that the string bundleVersion is serialized and updating its initial value in the editor makes it available in the build as well. Very elegant solution, thanks.
Answer by furic · Apr 20, 2015 at 08:38 AM
PlayerSetting is in UnityEditor, that means like all other Editor scripts they are not imported when building the game.
So running PlayerSettings.Android.bundleVersion in Android will give you a NullException.
A simple but stupid solution, create your own constant and change it every time you build.
public const string VERSION = "0.9.2"; // Change this to build version everytime create a build!
That is not the answer. This way is not eligible. I need the version code in runtime. Thank you for your answer.
i don't see why u can't do this this way. you are like asking for a function not in Unity
Your answer
Follow this Question
Related Questions
Help with attacking, and death animation. Unity 5 javascript 1 Answer
Variable Not Changing. 0 Answers
This not reading folders in the Windows Store 0 Answers
Cant get Monodevolop to work 0 Answers