Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I properly enable the internal profiler via script/code?

Discussion in 'Android' started by tim_h, Jul 7, 2015.

  1. tim_h

    tim_h

    Joined:
    Feb 13, 2014
    Posts:
    10
    I am attempting to generate a build via BuildPipeline.BuildPlayer with the Android Internal Profiler enabled (so I can see the performance data in adb logcat).

    This works if I enable the "Enable Internal Profiler" checkbox in the GUI first, but I would like to leave the setting disabled (so it's saved that way in source control) and then only update the local copy via script. There is a hacky way to do this by editing the ProjectSettings.asset file directly and changing the value of AndroidProfiler from 0 to 1, but that's not an option when the project is using binary data files (which I need to support).

    I have tried the SetPropertyInt method that I've seen being used to enable iOS x64 support, but that doesn't seem to be working for this setting. There's nothing built directly into the API that I can find.

    Anyone know the correct way to do this?

    I'm using Unity 4.6.x series, specifically testing this with 4.6.5.
     
    Last edited: Jul 8, 2015
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    There seems to be a property to get/set this:
    http://docs.unity3d.com/ScriptReference/PlayerSettings-enableInternalProfiler.html

    When you start your build, enable it:
    Code (CSharp):
    1. PlayerSettings.enableInternalProfiler = true;
    Then turn it off once you're done.

    I just tested it though, it doesn't seem to reflect the changes you make in the actual UI (under Player Settings).
    This could be a bug.. not sure.

    Can to try using it and see if that works out for you ?
     
  3. tim_h

    tim_h

    Joined:
    Feb 13, 2014
    Posts:
    10
    Unfortunately, that property doesn't exist for me. I'm using Unity 4.6.5.
     
    liortal likes this.
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,559
    You could try to use custom editor scripting code for modifying that value:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using UnityEngine;
    5.  
    6. public class Test
    7. {
    8.     [MenuItem("Tools/Set Android Internal Profiler")]
    9.     public static void SetAndroidInternalProfiler ()
    10.     {
    11.         var ps = Resources.FindObjectsOfTypeAll<PlayerSettings>();
    12.         var so = new SerializedObject(ps);
    13.  
    14.         var sp = so.FindProperty("enableInternalProfiler");
    15.  
    16.         sp.boolValue = true;
    17.         so.ApplyModifiedProperties();
    18.     }
    19. }
    I actually have no idea whether that works or not...

    This code basically looks up any loaded object of type PlayerSettings (in which this setting is strored).

    It when grabs the appropriate property and changes its value.

    Not sure whether this actually does anything or not: trying to print the current value for this setting seems to be incorrect, so i am not sure whether this code actually does what you want.
     
    tim_h likes this.
  5. tim_h

    tim_h

    Joined:
    Feb 13, 2014
    Posts:
    10
    Circled back to this and you were actually very close!

    You just need to change "enableInternalProfiler" to be "AndroidProfiler" matching the text string in the PlayerSettings.asset file.

    Thanks so much!
     
    liortal likes this.
  6. pweeks

    pweeks

    Joined:
    Mar 29, 2011
    Posts:
    104
    i'm also trying to accomplish this either by setting it via code or passing a preprocessor value...neither of the properties you mention "enableInternalProfiler" nor "AndroidProfiler" exist in the PlayerSettings for 4.6...the value is in project settings which I dont seem to be able to get at during build time....also just passing a pre-processor define doesnt seem to enable it...anyone from Unity want to chime in on how to enable this during a build script (like for a build machine that is building a "profile" version)
     
  7. tim_h

    tim_h

    Joined:
    Feb 13, 2014
    Posts:
    10
    pweeks, that is my exact use case and the above snippet from liortal works (with the correction to the string in my post).
     
    liortal likes this.
  8. pweeks

    pweeks

    Joined:
    Mar 29, 2011
    Posts:
    104
    so you're setting a pre-pro define on the command line and then checking in your builder script and then setting the flag with property setter/getter? or are you using the script code and applying the change to the property?

    i was looking for something like "DevelopmentBuild" or "ConnectWithProfiler", which when passed as a build option, do the things I want it to...granted those are probably going to the "build settings" so i might have to pass my own flag on the command line, check it and enable the profiler via the snippet.

    edit: i tried passing my own flag, that was easy enough, but trying to set the enableInternalProfiler does not work...i tried
    Code (csharp):
    1.  
    2. if (PlayerSettings.enableInternalProfiler == false)
    3.  
    and that did not return false (was definitely off in the scene)...i then tried
    Code (csharp):
    1.  
    2. PlayerSettings.GetPropertyBool("enableInternalProfiler")
    3.  
    and that yielded errors like
    EditorOnlyPlayerSettings property enableInternalProfiler::enableInternalProfiler not inititalized.
    and trying to set it similarly yielded
    EditorOnlyPlayerSettings property enableInternalProfiler::enableInternalProfiler not inititalized. Please initialize it using corresponding PlayerSettings.InitializeProperty function!

    i was trying to avoid applying any changes to the project that might be stuck after a build (by a build machine for example).

    edit2: i did see the AndroidPlayer value in the ProjectSettings.asset file, in the PlayerSettings section, and tried setting it via the find & set property, and it did find it, did enable the profiler, and it did modify the projectsettings.asset file...so i guess as long as a build machine doesnt commit changes it's "okay", just not my ideal...thanks
     
    Last edited: Sep 17, 2015