LoginSignup
7

More than 5 years have passed since last update.

Unity iOSビルド時にURLスキームを追加するだけのPostProcessBuild

Posted at

URLスキームを追加したかったので書いた。

using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;

/// <summary>
/// Unity iOSビルド後にUrlSchemeを追加する為のPostprocessor
/// </summary>
public class UrlSchemePostprocessor
{
    /// <summary>
    /// ビルド後の処理( Android / iOS共通 )
    /// </summary>
    [PostProcessBuild(1)]
    public static void OnPostProcessBuild( BuildTarget target, string path )
    {
        // iOSじゃなければ処理しない
        if ( target != BuildTarget.iOS )
        {
            return;
        }

        AddUrlScheme( path );
    }

    /// <summary>
    /// URLスキームを追加します
    /// </summary>
    /// <param name="path"> 出力先のパス </param>
    public static void AddUrlScheme( string path )
    {
        var plistPath   = Path.Combine( path, "Info.plist" );
        var plist       = new PlistDocument();

        // 読み込み
        plist.ReadFromFile( plistPath );

        // ほぼ定型文
        var urlTypeArray    = plist.root.CreateArray( "CFBundleURLTypes" );
        var urlTypeDict     = urlTypeArray.AddDict();
        var urlSchemeArray  = urlTypeDict.CreateArray( "CFBundleURLSchemes" );

        // URLスキームを追加
        urlSchemeArray.AddString( "hogehoge" );
        urlSchemeArray.AddString( "fugafuga" );

        // 書き込み
        plist.WriteToFile( plistPath );
    }
}

こんな感じでPostProcessBuild時にinfo.plistに対してUrlSchemeが複数書き込みできます。

以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7