nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

Unity で iOS ビルド時に Framework の自動追加・画像の自動追加・任意のファイルの登録・URL Scheme の設定をする

はじめに

  • Unity で PostprocessBuildPlayer を使ってタイトルのようなことをする。
  • 掲載のコードはGAMEFEATのSDKを組み込んだときのもの。

ruby から XCode を操作する gem のインストール

http://starzero.hatenablog.com/entry/2014/02/18/163330 を参考に

ビルド後に実行されるスクリプトの用意

  • Assets/Editor/PostprocessBuildPlayer という名前で以下のスクリプトを配置する
#!/usr/bin/env ruby
 
require 'xcodeproj'
require 'fileutils'

PROJECT = 'Unity-iPhone'
TARGET = 'Unity-iPhone'
LIBRARY = 'Libraries'
 
# システムFramework追加
def add_frameworks(project, names, optional = false)
  project.targets.each do |target|
    next unless TARGET == target.name
 
    build_phase = target.frameworks_build_phase
    framework_group = project.frameworks_group
 
    names.each do |name|
      next if exist_framework?(build_phase, name)
 
      path = "System/Library/Frameworks/#{name}.framework"
      file_ref = framework_group.new_reference(path)
      file_ref.name = "#{name}.framework"
      file_ref.source_tree = 'SDKROOT'
      build_file = build_phase.add_file_reference(file_ref)
      if optional
        build_file.settings = { 'ATTRIBUTES' => ['Weak'] }
      end
    end
  end
end
 
# 外部Framework追加
def add_external_frameworks(project, names)
  project.targets.each do |target|
    next unless TARGET == target.name
 
    target.build_configurations.each do |configuration|
      # Framework Search Pathsを設定
      configuration.build_settings['FRAMEWORK_SEARCH_PATHS'] = configuration.build_settings['LIBRARY_SEARCH_PATHS']
    end
 
    build_phase = target.frameworks_build_phase
    library_group = project.main_group.children.find {|child| child.path == LIBRARY}
 
    names.each do |name|
      next if exist_framework?(build_phase, name)
 
      copy_library(name)
 
      path = "#{LIBRARY}/#{name}.framework"
      file_ref = library_group.new_reference(path)
      file_ref.name = "#{name}.framework"
      file_ref.source_tree = 'SOURCE_ROOT'
      build_phase.add_file_reference(file_ref)
    end
  end
end
 
# Framework追加済みか
def exist_framework?(build_phase, name)
  build_phase.files.each do |file|
    return true if file.file_ref.name == "#{name}.framework"
  end
  false
end
 
# 外部FrameworkをUnityのディレクトリからXcodeのディレクトリへコピー
def copy_library(name)
  asset_path = "#{ARGV[0]}/../Assets"
  from = File.expand_path("#{asset_path}/Editor/iOS/#{LIBRARY}/#{name}.framework", __FILE__)
  to = "#{ARGV[0]}/#{LIBRARY}/#{name}.framework"
  FileUtils.copy_entry(from, to)
end


### ===================================================


project_path = ARGV[0] + "/#{PROJECT}.xcodeproj"
project = Xcodeproj::Project.new(project_path)
 
project.initialize_from_file
 
### Framework の自動追加 ###
# require で追加
add_frameworks(project, ["Foundation", "UIKit", "CoreTelephony"])
# optional で追加
add_frameworks(project, ["AdSupport", "StoreKit"], true)
# 外部 Framework の追加
# 事前に Assets/Editor/iOS/Libraries にファイルを置いておく
add_external_frameworks(project, ["GameFeatKit"])
 

### 画像のコピー ###
# 一度 XCode の Images.xcassets にファイルを追加し、作成されたファイルを Assets/Editor/iOS/Images に保存しておく
asset_path = "#{ARGV[0]}/../Assets"
FileUtils.cp_r("#{asset_path}/Editor/iOS/Images/gamefeat/", "#{ARGV[0]}/Unity-iPhone/Images.xcassets/")


### ライブラリのコピー ###
# 任意のファイルを XCode に登録する
files = [
  # [ファイル名, コピー元, コピー先 ]
  [ "JSONKit.h", "#{asset_path}/Editor/iOS/Libraries/JSONKit/", "#{ARGV[0]}/Libraries/" ], 
  [ "JSONKit.m", "#{asset_path}/Editor/iOS/Libraries/JSONKit/", "#{ARGV[0]}/Libraries/" ], 
]
files.each do |file|
  FileUtils.cp_r(file[1] + file[0], file[2])
  project.targets.each do |target|
    next unless TARGET == target.name
    library_group = project.main_group.children.find {|child| child.path == LIBRARY}
    file = library_group.new_file(file[2] + file[0])
    target.add_file_references([file])
  end
end


### URL Scheme の設定 ###
plist = "#{ARGV[0]}/Info.plist"
appname = "info.nirasan.ssec"
system("/usr/libexec/PlistBuddy -c 'Add :CFBundleURLTypes array' #{plist}")
system("/usr/libexec/PlistBuddy -c 'Add CFBundleURLTypes:0:CFBundleURLSchemes array' #{plist}")
system("/usr/libexec/PlistBuddy -c 'Add CFBundleURLTypes:0:CFBundleURLSchemes:0 string #{appname}' #{plist}")


### 設定の保存 ###
project.save