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. Dismiss Notice

How to set PBXProject

Discussion in 'iOS and tvOS' started by Chenfrank, May 12, 2020.

  1. Chenfrank

    Chenfrank

    Joined:
    Mar 25, 2015
    Posts:
    14
    I tried the following methods and they all showed the same error message.

    Code (CSharp):
    1. string proPath = $"{pathBuildProject}/Unity-iPhone.xcodeproj/project.pbxproj";
    2. PBXProject pbx = new PBXProject();
    3. pbx.ReadFromString(File.ReadAllText(proPath));
    4. string targetGuid = PBXProject.GetUnityTargetName();
    5. pbx.SetBuildProperty(targetGuid, "PRODUCT_NANE", "Name");
    6. string proPath = $"{pathBuildProject}/Unity-iPhone.xcodeproj/project.pbxproj";
    7. PBXProject pbx = new PBXProject();
    8. pbx.ReadFromFile(proPath);
    9. string targetGuid = PBXProject.GetUnityTargetName();
    10. pbx.SetBuildProperty(targetGuid, "PRODUCT_NANE", "Name");
    Error content: NullReferenceException: Object reference not set to an instance of an object UnityEditor.iOS.Xcode.PBXProject.GetConfigListForTarget (System.String targetGuid) (at /Users/builduser/buildslave/unity/build/External/XcodeAPI/Xcode/PBXProject.cs:1225) UnityEditor.iOS.Xcode.PBXProject.SetBuildProperty (System.String targetGuid, System.String name, System.String value) (at /Users/builduser/buildslave/unity/build/External/XcodeAPI/Xcode/PBXProject.cs:1326)
     
  2. NicTda

    NicTda

    Joined:
    Jan 11, 2018
    Posts:
    19
    Had to change my code to this for it to work (Unity 2019.3.13f1 and Xcode 11.4.1), to accommodate with the GetUnityMainTargetGuid() and build configs changes.
    Check that you set "PRODUCT_NAME" as well (instead of PRODUCT_NANE)

    Code (CSharp):
    1. // Get the project file
    2. string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
    3. PBXProject proj = new PBXProject();
    4. proj.ReadFromString(File.ReadAllText(projPath));
    5. string target = proj.GetUnityMainTargetGuid();
    6.  
    7. // Change the project's name
    8. var configNames = proj.BuildConfigNames();
    9. foreach(var configName in configNames)
    10. {
    11.     var config = proj.BuildConfigByName(target, configName);
    12.     proj.SetBuildPropertyForConfig(config, "PRODUCT_NAME", "Name");
    13. }
    14.  
    15. // Write the changes
    16. File.WriteAllText(projPath, proj.WriteToString());
     
    Dargon_huihui and frank231love like this.
  3. Chenfrank

    Chenfrank

    Joined:
    Mar 25, 2015
    Posts:
    14
    thank you~