コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】Exception: Calling TargetGuidByName with name='Unity-iPhone' is deprecated

概要

using UnityEditor;
using UnityEditor.iOS.Xcode;

public static class Example
{
    [MenuItem( "Tools/Hoge" )]
    private static void Hoge()
    {
        var path        = "iOS";
        var projectPath = PBXProject.GetPBXProjectPath( path );
        var project     = new PBXProject();

        project.ReadFromFile( projectPath );

        var ttargetGuid = project.TargetGuidByName( "Unity-iPhone" );
        project.SetBuildProperty( targetGuid, "ENABLE_BITCODE", "NO" );

        project.WriteToFile( projectPath );
    }
}

上記のように Xcode プロジェクトを編集するスクリプトを実行した時に

Exception: Calling TargetGuidByName with name='Unity-iPhone' is deprecated. 
There are two targets now, call GetUnityMainTargetGuid() - 
for app or GetUnityFrameworkTargetGuid() - 
for source/plugins to get Guid instead.

このようなエラーが発生する場合は

using UnityEditor;
using UnityEditor.iOS.Xcode;

public static class Example
{
    [MenuItem( "Tools/Hoge" )]
    private static void Hoge()
    {
        var path        = "iOS";
        var projectPath = PBXProject.GetPBXProjectPath( path );
        var project     = new PBXProject();

        project.ReadFromFile( projectPath );

        var targetGuid = project.GetUnityMainTargetGuid();
        project.SetBuildProperty( targetGuid, "ENABLE_BITCODE", "NO" );

        project.WriteToFile( projectPath );
    }
}

TargetGuidByName 関数を GetUnityMainTargetGuid 関数や
GetUnityFrameworkTargetGuid 関数に置き換えます