Search Unity

Switching Project Ids with Scripting

Discussion in 'Unity Analytics' started by SimonOliver, May 17, 2016.

  1. SimonOliver

    SimonOliver

    Joined:
    Jul 16, 2007
    Posts:
    30
    Hi,

    We've got a single Unity project that outputs a number of different applications (different bundle ids), but currently it seems that there's no easy way of modifying cloudProjectId (its set as read only). We need to modify this at build time to switch project ids for analytics etc, otherwise the various builds can't be automated.

    What is the best way to do this? I've tried using reflection but get "
    EditorOnlyPlayerSettings property cloudProjectId::cloudProjectId not inititalized. Please initialize it using corresponding PlayerSettings.InitializeProperty function!"

    Thanks,
    Simon
     
  2. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @SimonOliver,

    Unfortunately, we don't currently have a way to change project id's via code. Can I ask that you add this as a suggestion (or vote on the idea if it already exists) on our feedback page? I've seen this feature request before, but it's good to have it's popularity documented on the feedback page, since this is what we look at when trying to decide what new features to implement. Here's the link to the page: https://feedback.unity3d.com/forums...f8=✓&status=0&category=analytics&view=hottest
     
  3. SimonOliver

    SimonOliver

    Joined:
    Jul 16, 2007
    Posts:
    30
    Ok, thanks for letting me know - will add this.
     
  4. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    584
    Hi Erica, we need this feature as well. we have eight apps that we are producing from one unity project. Simple API like SwithProject(id) would be really helpful.
     
    erika_d likes this.
  5. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @Foriero,

    Thanks for that information and use case! I will make note of that, and I would suggest creating an idea for it on the feedback page linked above. Right now the only one I see is for changing it at runtime, which is a bit different from build time which is what I believe you're describing. Having your use case documented there would be very useful.
     
  6. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    584
    Hi, if I can change it from code in runtime then it is enough. Can you provide us link or sample code?

    Thanks, Marek.
     
  7. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
  8. kodekq

    kodekq

    Joined:
    Sep 27, 2014
    Posts:
    23
    Please write if there have been any changes since 2016?
     
  9. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    I'm afraid we have not opted to make this a feature or development priority.
     
  10. kodekq

    kodekq

    Joined:
    Sep 27, 2014
    Posts:
    23
    Ok( Thanks for answer.
     
  11. jekill

    jekill

    Joined:
    Sep 17, 2015
    Posts:
    1
    I understand that this is quite difficult to do in runtime. But can it be done in build time?
     
  12. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    584
    Yes please build time would be really helpful.
     
  13. TS-Trooper

    TS-Trooper

    Joined:
    Apr 30, 2019
    Posts:
    18
    Hi @kodekq , @Foriero , @jekill,
    About a year and some ago I was trying to achieve this as well.
    Back then I stumbled upon
    https://github.com/DragonBox/gists/blob/master/Assets/UnityUtils/Editor/UnityConnectUtils.cs

    It has been working quite nicely.
    Until recently. Somehow it does not always do the switch correctly. For us this is a problem, because we build the project in batchmode.
    When I have Unity open and I use it though it works nice.
    I thought you might like it as well.

    Also, if you happen to find a fix for the switch not always switching correctly (at least in batchmode) please let me know how I'd be able to fix it.
     
  14. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    211
    is there any update with Unity 2019.x ?
    how can we use Unity Analytics with multiple apps from the same project?
     
  15. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry there is no API that I'm familiar with. Can you elaborate? The Analytics dashboard is associated with the ProjectID. Multiple projectIDs would have multiple Dashboards. If you are considering multiple apps with one projectID, what is your plan to keep them separate? Would these be separate apps on the Google developer dashboard, each with their own revenue stream?
     
  16. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    211
    we have a big (even opensource) project ( https://www.antura.org ) from which we build 7 different apps, with their own bundle id and store presence (for example one teaches arabics, another teaches english, another french.. we have some particular editions for foundations etc)

    in our case we don't have IAP nor complex things.. we are using the Unity Cloud Builder for everything (with custom cunfigs) but we would really like to separate the Unity Analytics changing the project id at build time.. we could do it manually (unlinking and relinking another project)..
    we have used Google Analytics forst, then Firebase, then Facebook Analytics.. but now we would like to use Unity for everything.. but we have this obstacle with the Unity Analytics ids!

    thank you
     
  17. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Given a single ProjectID, how would you keep the apps separate in your reporting? Number of users per day, etc. All 7 apps would share the same Analytics data and would show as one app in the Dashboard. Perhaps that doesn't matter to you, but I wanted to point it out.
     
  18. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    211
    we want to CHANGE ProjectID at build time.. so to assign each "edition" of our game its own analytics and stuff.
    we have one Unity Repository for 7 independent App editions...
     
    Serhii-Horun likes this.
  19. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Ah, clearly. I see what you mean. Sorry I don't have a definitive answer for you. One approach I might suggest would be to start with a new project, and take a snapshot of the file structure in the project after Saving. Then change the ProjectID link in the Services window, then go back and see which files may have changed. Not entirely elegant and no API, but you may be able to edit the files during your build process. You may have to first generate 7 ProjectID's this way, so each has their own Dashboard.
     
    Energy0124 and Endahs like this.
  20. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    211
    thank you.
    we are going to create a Unity Project for every published edition on the Store... and manually relink the correct one when building an edition.
    if you plan to allow to "switch" easily, maybe by preBuildProcess, the UnityId, even better!!!
    thank you
     
  21. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Were you able to test my suggestion? It would be a scriptable workaround in the meantime, if so.
     
  22. Foriero

    Foriero

    Joined:
    Jan 24, 2012
    Posts:
    584
    Same issue here. We export from one Unity Project 15 apps. I would definitely welcome some official solution. :)
     
    Serhii-Horun likes this.
  23. GustavNinja

    GustavNinja

    Joined:
    Jun 13, 2016
    Posts:
    96
  24. Serhii-Horun

    Serhii-Horun

    Joined:
    Apr 12, 2015
    Posts:
    151
    Yeah, we need the same!
    We have an app and we use different staging servers. So, we want to be able to send analytics to different dashboards depends on server environment which we have connected(production, stage, testing and so on)
     
  25. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    The previous post looks like a possible solution
     
    Serhii-Horun likes this.
  26. Endahs

    Endahs

    Joined:
    Sep 16, 2014
    Posts:
    94
    Ooh, we so need this too. We have a common Unity project that acts as a platform for our individual games. At build time we include only the relevant game assets + all the common platform code and assets to make the game. We have been able to automate everything except for unlinking and relinking the project to the correct Unity Dashboard Project.

    I followed @JeffDUnity3D 's suggestion above, comparing in Git the project before and after unlinking + relinking the project. Only two files were changed:

    ProjectSettings/ProjectSetting.asset, under "cloudServicesEnabled" it added (probably because I didn't set the projects in dashboard to have same services enabled):

    Build: 0
    Collab: 0
    ErrorHub: 0
    Hub: 0

    ProjectSettings/UnityConnectSettings.asset, it changed only one line:

    cloudProjectID: (old ID changed to new ID)

    Worth looking into. Could easily make these changes with C# File methods. Not sure Unity would react if you changed those files in file system when Unity was still open.

    Here's the only available Unity API I could find for accessing the projectID, unfortunately it's all read only and I haven't found any Unity API that allows you to write to/change it: https://docs.unity3d.com/ScriptReference/CloudProjectSettings.html
     
    Last edited: Jan 28, 2021
  27. frankadoriv

    frankadoriv

    Joined:
    Jul 19, 2017
    Posts:
    14
    Hi guys! i want to confirm that the following code is working on batch mode:

    Code (CSharp):
    1.        
    2. private static Boolean SetProjectID(string projectID,string projectName){
    3.                 const string ProjectSettingsAssetPath = "ProjectSettings/ProjectSettings.asset";
    4.                 SerializedObject projectSettings = new SerializedObject(UnityEditor.AssetDatabase.LoadAllAssetsAtPath(ProjectSettingsAssetPath)[0]);  
    5.                 SerializedProperty projectIDProp = projectSettings.FindProperty ("cloudProjectId");
    6.                 SerializedProperty projectNameProp = projectSettings.FindProperty ("projectName");
    7.                 projectIDProp.stringValue = projectID;
    8.                 projectNameProp.stringValue = projectName;
    9.                 return projectSettings.ApplyModifiedProperties();
    10.         }
    11.  
     
    Mankind_games likes this.
  28. StefanoCecere

    StefanoCecere

    Joined:
    Jun 10, 2011
    Posts:
    211
    hi.. what do mean by "batch mode"?
     
  29. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Try it and see? Not sure I quite follow.
     
  30. lorddesu

    lorddesu

    Joined:
    Aug 20, 2014
    Posts:
    31
    Seem works fine in editor with IPreprocessBuildWithReport
    At least for Remote Config
     
  31. Stpayne

    Stpayne

    Joined:
    Jul 8, 2022
    Posts:
    1
    Can you post your IPreprocessBuildWithReport file? I tried going that route and cannot get that script to execute for the life of me, even when it's in the Editor directory
     
  32. VirtualDestructor

    VirtualDestructor

    Joined:
    May 3, 2014
    Posts:
    44
    Unfortunately this did not work for me. ApplyModifiedProperties() returns true but the project settings don't change. Using Unity 2021.3.11f1
     
    Last edited: Oct 22, 2022
    MousePods and andersemil like this.