Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

PBXProject.GetUnityMainTargetGuid returns empty string on macOS, API inconsistencies with iOS

Discussion in 'macOS' started by jason_yak, Mar 25, 2021.

  1. jason_yak

    jason_yak

    Joined:
    Aug 25, 2016
    Posts:
    531
    Hi there,

    I'm using Unity 2020.3.0. When I call this PBXProject.GetUnityMainTargetGuid on a project instance of a generated macOS xcode project I'm getting an empty string.

    It works fine on iOS and I can only assume that GetUnityMainTargetGuid under the hood is hard coding something like this to return the GUID: project.TargetGuidByName( "Unity-iPhone" ), which doesn't work on macOS because the main target name uses this convention: project.TargetGuidByName( PlayerSettings.productName ). So yes I can use this workaround for mac, but the PBXProject.GetUnityMainTargetGuid method really should be updated to work for mac or the documentation edited to make it clear that method is only for iOS/tvOS.

    Similarly the documentation for GetUnityFrameworkTargetGuid() method should note that it also doesn't work for Mac. This I understand because iOS uses 'UnityFramework' while mac projects uses a kind of equivalent 'GameAssembly' for IL2CPP based xcode projects. But all of this is just not entirely clear from the outset that these methods are not for mac based xcode projects. There probably should be some notes for this method also that it's not for mac, perhaps a new method for GetUnityGameAssemblyGuid() added for mac only, or something else to make all of this more consistent.
     
    Last edited: Mar 25, 2021
  2. sdalex

    sdalex

    Joined:
    Dec 17, 2014
    Posts:
    21
    They didn't adapt it for the macOS xcode project yet I think. It just happen that it work partially with macOS xcode project.
    You still need the "iOS Build Support" module and the namespace imply that this is for iOS: "UnityEditor.iOS.Xcode".

    On my side I'm using the project.ProjectGuid() that returns the exact same value as the project.TargetGuidByName(PlayerSettings.productName) for the macOS xcode project.
     
  3. jason_yak

    jason_yak

    Joined:
    Aug 25, 2016
    Posts:
    531
    Yea fair point it's under the iOS namespace, I'd overlooked that. I guess the greater feature request would be for a UnityEditor.macOS.Xcode equivalent, at same time it does seem overkill to branch and duplicate all of the PBXProject classes when it would be 99% the same between platforms. Perhaps it being moved to a more generlised namespace for iOS/tvOS/macOS with a few minor platform extensions would be better.

    Also.. thanks for the ProjectGuid suggestion, it's good that that works, but I think that might be actually exposing another issue. I could be wrong on this, but I think project guid is meant to resolve to the project root which is more for setting default build settings that the build targets adopt if the build targets don't override the project root defaults.

    I'm wondering if project.ProjectGuid() under the hood is not actually targeting the project root and is falsely targeting the main build target, perhaps because both project and main target happen to have the same name. Conincidentally that's the guid what we do want to edit, but this might be something that breaks down the line because looking at a generated project.pbxproj the project root has a different guid to the main build target ie. anyone that does actually want to edit the project root default build settings might not be able to dig out the correct guid.
     
  4. sdalex

    sdalex

    Joined:
    Dec 17, 2014
    Posts:
    21
    Actually I was wrong. project.ProjectGuid() returns a different value by one digit:
    752CA8272149E6C100295EFC // proj.TargetGuidByName(PlayerSettings.productName)
    752CA8202149E6C100295EFC // proj.ProjectGuid()

    I realize that when I was trying to add files using the project GUID instead of the target GUID. I was getting NullReferenceException when calling the method "AddFileToBuild".
     
  5. jason_yak

    jason_yak

    Joined:
    Aug 25, 2016
    Posts:
    531
    Ahh right... yea I did actually notice the project guid and main target guid's are very very similar with my xcode project too, only a slight difference like this which was hard to spot. That's good to know thanks, cause I actually wanted to apply some default settings to the project a couple of days ago and thought about ^ this and instead I made the changes to all of my targets thinking that there was no way to easily access the base project guid. But now this all makes sense.
     
  6. jason_yak

    jason_yak

    Joined:
    Aug 25, 2016
    Posts:
    531
    So I guess the only way at the moment to get target guid's for mac xcode projects is to use project.TargetGuidByName("targetName") which is fine, at least there's an option, but an api and docs cleanup around this for mac would be great.
     
  7. Wriggler

    Wriggler

    Joined:
    Jun 7, 2013
    Posts:
    133
    Just wanted to chime in here and agree with y'all. Post processing macOS Xcode projects is a bit of a pain at the moment. When you get to want to do more complex stuff you find that a bunch of APIs are missing. I ended up cloning the iOS stuff and adapted it for macOS, but agree that a proper macOS namespace would be super useful for this...

    Ben
     
  8. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    thanks for bringing this to our attention. We have a fix and will do backports to LTS soon, though only down to 21.3.
    If you still need to do this on 20.3, then indeed
    Code (CSharp):
    1. proj.TargetGuidByName(PlayerSettings.productName)
    is the way