Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8895

Unity Cloud BuildでFirebaseを使ったiOSアプリをビルドする

$
0
0

Unity Cloud BuildではCocoapodが使えません。
なのでCocoapodを使わずにFirebaseの機能をiOSで使う方法を紹介します。

使えた機能

  • Analytics
  • Realtime Database
  • Authentication
  • Cloud Messaging
  • Dynamic Links 

UnityにFirebaseを実装する

FirebaseのSDKを入れる

まず ここからFirebaseのUnity用のSDKをダウンロードします。
次にダウンロードしたSDKをUnityにインポートします。
[Assets]>[Import Package]>[Custom Package...]
2019-11-05 (3)_LI.jpg
ダウンロードしたzipファイルから使うものを選びます
Downloads\firebase_unity_sdk_6.6.0\firebase_unity_sdk\dotnet4\
↑このパスの中の Firebase???.unitypackage

iOSのFrameworkを入れる

次にFirebaseのiOS版のFrameworkを ここからダウンロードします。
ダウンロードしたzipファイルを解凍し、使いたいFirebaseの機能のファイルを開く。

Analyticsの場合

[Downloads/Firebase-6.11.0/Firebase/Analytics]内のすべての .framework フォルダを
Unityの[Assets/Plugins/iOS]に入れます。
2019-11-05 (8).png
こんな感じでその他のframeworkも入れていきます。
frameworkが入れ終わったら、[Firebase-6.11.0\Firebase]内にある
[Firebase.h]というファイルも[Assets/Plugins/iOS]に入れます。

XcodeをC#で設定する

下のファイルを[Assets/Editor]内に置きます。

BuildProcess.cs

BuildProcess.cs
usingUnityEngine;usingUnityEditor;usingUnityEditor.Callbacks;usingUnityEditor.iOS.Xcode;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;usingSystem.Xml;publicclassBuildProcess{[PostProcessBuild]publicstaticvoidOnPostProcessBuild(BuildTargetbuildTarget,stringpath){// iOSじゃなければ処理しないif(buildTarget!=BuildTarget.iOS){return;}varproject=newPBXProject();project.ReadFromFile(PBXProject.GetPBXProjectPath(path));varprojectGuid=project.TargetGuidByName(PBXProject.GetUnityTargetName());// Enable Modules (C and Objective-C)をYESに設定するproject.SetBuildProperty(projectGuid,"CLANG_ENABLE_MODULES","YES");project.SetBuildProperty(projectGuid,"ENABLE_BITCODE","NO");project.WriteToFile(PBXProject.GetPBXProjectPath(path));stringprojPath=Path.Combine(path,"Unity-iPhone.xcodeproj/project.pbxproj");PBXProjectproj=newPBXProject();proj.ReadFromString(File.ReadAllText(projPath));stringtarget=proj.TargetGuidByName("Unity-iPhone");proj.AddBuildProperty(target,"OTHER_LDFLAGS","-ObjC");// 必須!proj.AddBuildProperty(target,"OTHER_LDFLAGS","-l???");/*
詳しくは下を参照
*/List<string>frameworks=newList<string>(){"???.framework"};foreach(varframeworkinframeworks){proj.AddFrameworkToProject(target,framework,false);}File.WriteAllText(projPath,proj.WriteToString());CreateEntitlements(path,"myapp");SetCapabilities(path);// Push通知を使わないなら多分いらない}privatestaticvoidCreateEntitlements(stringpath,stringyour_appname){XmlDocumentdocument=newXmlDocument();XmlDocumentTypedoctype=document.CreateDocumentType("plist","-//Apple//DTD PLIST 1.0//EN","http://www.apple.com/DTDs/PropertyList-1.0.dtd",null);document.AppendChild(doctype);XmlElementplist=document.CreateElement("plist");plist.SetAttribute("version","1.0");XmlElementdict=document.CreateElement("dict");plist.AppendChild(dict);document.AppendChild(plist);XmlElemente=(XmlElement)document.SelectSingleNode("/plist/dict");XmlElementkey=document.CreateElement("key");key.InnerText="aps-environment";e.AppendChild(key);XmlElementvalue=document.CreateElement("string");value.InnerText="development";e.AppendChild(value);stringentitlementsPath=Path.Combine(path,"Unity-iPhone/"+your_appname+".entitlements");//= path + "/Unity-iPhone/" + your_appname + ".entitlements";document.Save(entitlementsPath);stringprojPath=Path.Combine(path,"Unity-iPhone.xcodeproj/project.pbxproj");PBXProjectproj=newPBXProject();proj.ReadFromString(File.ReadAllText(projPath));stringtarget=proj.TargetGuidByName("Unity-iPhone");stringguid=proj.AddFile(entitlementsPath,entitlementsPath);proj.AddBuildProperty(target,"CODE_SIGN_ENTITLEMENTS",Path.Combine(path,"Unity-iPhone/"+your_appname+".entitlements"));proj.AddFileToBuild(target,guid);proj.WriteToFile(projPath);}privatestaticvoidSetCapabilities(stringpath){stringprojPath=Path.Combine(path,"Unity-iPhone.xcodeproj/project.pbxproj");PBXProjectproj=newPBXProject();proj.ReadFromString(File.ReadAllText(projPath));stringtarget=proj.TargetGuidByName("Unity-iPhone");bools=proj.AddCapability(target,PBXCapabilityType.PushNotifications);File.WriteAllText(projPath,proj.WriteToString());string[]lines=proj.WriteToString().Split('\n');List<string>newLines=newList<string>();booleditFinish=false;for(inti=0;i<lines.Length;i++){stringline=lines[i];if(editFinish){newLines.Add(line);}elseif(line.IndexOf("isa = PBXProject;")>-1){do{newLines.Add(line);line=lines[++i];}while(line.IndexOf("TargetAttributes = {")==-1);// この下のやつは多分無くても大丈夫 まあ、一応・・・newLines.Add("TargetAttributes = {");newLines.Add("********* = {");newLines.Add("DevelopmentTeam = ****;");newLines.Add("SystemCapabilities = {");newLines.Add("com.apple.BackgroundModes = {");newLines.Add("enabled = 1;");newLines.Add("};");newLines.Add("com.apple.Push = {");newLines.Add("enabled = 1;");newLines.Add("};");newLines.Add("};");newLines.Add("};");editFinish=true;}else{newLines.Add(line);}}File.WriteAllText(projPath,string.Join("\n",newLines.ToArray()));}}

IOSBuildPostProcessor.cs

IOSBuildPostProcessor.cs
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;usingUnityEditor.Callbacks;usingUnityEditor.iOS.Xcode;publicstaticclassIOSBuildPostProcessor{[PostProcessBuild]publicstaticvoidOnPostprocessBuild(BuildTargettarget,stringpathToBuiltProject){if(target==BuildTarget.iOS){OnPostprocessBuildIOS(pathToBuiltProject);//Dynamic Links用OnPostprocessBuildPush(pathToBuiltProject);//Cloud Messaging用OnPostprocessBuildbg(pathToBuiltProject);//Cloud Messaging用}}privatestaticvoidOnPostprocessBuildIOS(stringpathToBuiltProject){//This is the default path to the default pbxproj file. Yours might be differentstringprojectPath="/Unity-iPhone.xcodeproj/project.pbxproj";//Default target name. Yours might be differentstringtargetName="Unity-iPhone";//Set the entitlements file name to what you want but make sure it has this extensionstringentitlementsFileName="my_app.entitlements";varentitlements=newProjectCapabilityManager(pathToBuiltProject+projectPath,entitlementsFileName,targetName);vardomain="???.page.link";//ここにはDynamic Linksで設定した、???.page.linkなどを入れるentitlements.AddAssociatedDomains(newstring[]{"applinks:"+domain});//Applyentitlements.WriteToFile();}privatestaticvoidOnPostprocessBuildPush(stringpathToBuiltProject){//This is the default path to the default pbxproj file. Yours might be differentstringprojectPath="/Unity-iPhone.xcodeproj/project.pbxproj";//Default target name. Yours might be differentstringtargetName="Unity-iPhone";//Set the entitlements file name to what you want but make sure it has this extensionstringentitlementsFileName="my_app_2.entitlements";varentitlements=newProjectCapabilityManager(pathToBuiltProject+projectPath,entitlementsFileName,targetName);entitlements.AddPushNotifications(true);//Applyentitlements.WriteToFile();}privatestaticvoidOnPostprocessBuildbg(stringpathToBuiltProject){//This is the default path to the default pbxproj file. Yours might be differentstringprojectPath="/Unity-iPhone.xcodeproj/project.pbxproj";//Default target name. Yours might be differentstringtargetName="Unity-iPhone";//Set the entitlements file name to what you want but make sure it has this extensionstringentitlementsFileName="my_app_3.entitlements";varentitlements=newProjectCapabilityManager(pathToBuiltProject+projectPath,entitlementsFileName,targetName);entitlements.AddBackgroundModes(BackgroundModesOptions.RemoteNotifications);//Applyentitlements.WriteToFile();}}

長いですね・・・
コメントを見て少し書き換えたりしてください。

BuildProcess.csを書き換える

BuildProcess.cs
proj.AddBuildProperty(target,"OTHER_LDFLAGS","-ObjC");// 必須!proj.AddBuildProperty(target,"OTHER_LDFLAGS","-l???");List<string>frameworks=newList<string>(){"???.framework"};

上のBuildProcess.csにこんな行があると思います
ここをframeworkファイル内のmodule.modulemapに沿って書き換えます。

例 Analyticsの場合

場所 [FirebaseAnalytics.framework/Modules/module.modulemap]

module.modulemap
framework module FirebaseAnalytics {
  umbrella header "FirebaseAnalytics.h"
  export *
  module * { export * }
  link "sqlite3"
  link "z"
  link framework "CoreData"
  link framework "Security"
  link framework "StoreKit"
  link framework "SystemConfiguration"
  link framework "UIKit"
}

と書いてある場合は
proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");// 必須!
の下に
proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-lz");
proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-lsqlite3");
を追加します。
要するに
"-l"+"link の後に書いているもの"
を追加します。
link frameworkの部分は、
List<string> frameworks = new List<string>() {
の中に上の例では

"CoreData.framework","Security.framework","StoreKit.framework","SystemConfiguration.framework","UIKit.framework"

を追加します。

こんな感じで他のframeworkのmodule.modulemapを見て追加します。

後はビルドしてみましょう!
そうすれば多分できるはずです。

わからないことやエラーが起きたらコメントで知らせてください。


Viewing all articles
Browse latest Browse all 8895

Trending Articles