Last active
January 25, 2016 11:17
-
-
Save rainbow23/fe8e9de108ebd312563b to your computer and use it in GitHub Desktop.
Library追加だけ実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEditor; | |
using UnityEditor.Callbacks; | |
using UnityEditor.iOS.Xcode; | |
public class LibrarySettings { | |
//constructor | |
public LibrarySettings(string buildRealPath, string copiedFolderRealPath){ | |
} | |
#if UNITY_IOS | |
public List<CustomFile> files_; | |
public List<CustomFramework> customFrameworks_; | |
private PBXProject m_PBXProject; | |
private string copiedFolderRealPath_; | |
private string m_topParentFolderName_; | |
private string m_ProjPath; | |
private string m_Target; | |
//constructor | |
public LibrarySettings(string buildRealPath, string copiedFolderRealPath){ | |
files_ = new List<CustomFile>(); | |
customFrameworks_ = new List<CustomFramework>(); | |
initPbxProject(buildRealPath); | |
copiedFolderRealPath_ = copiedFolderRealPath; | |
m_topParentFolderName_ = GetFolderName(copiedFolderRealPath); | |
Debug.Log("m_topParentFolderName_, "+ m_topParentFolderName_); | |
init(); | |
} | |
private void init(){ | |
AddCustomFrameworksXcodeSettings(copiedFolderRealPath_, m_topParentFolderName_); | |
File.WriteAllText (m_ProjPath, m_PBXProject.WriteToString ()); | |
} | |
public void AddFilesXcodeSettings(string folderRealPath, string localFilePath) { | |
string localPath = localFilePath; | |
foreach (string file in Directory.GetFiles(folderRealPath)) | |
{ | |
string fileName = Path.GetFileName(file); | |
string LastFolderName = GetFolderName(file); | |
Debug.Log ("LastFolderName in file: " + LastFolderName); | |
//親フォルダ名が.frameworkの場合は追加しない | |
bool ParentFolderHasframeworkExtension = FolderHasframeworkExtension(LastFolderName); | |
if(ParentFolderHasframeworkExtension){ | |
continue; | |
} | |
localPath = Path.Combine(localFilePath, fileName); | |
//Debug.Log ("localPath: " + localPath); | |
files_.Add(new CustomFile(localPath)); | |
} | |
foreach(var cFile in files_) | |
{ | |
string guid = m_PBXProject.AddFile (cFile.srcPath_, cFile.srcPath_, PBXSourceTree.Source);//PBXSourceTree.Sourceを使う場合ローカルパスが使える | |
m_PBXProject.AddFileToBuild(m_Target, guid); | |
} | |
} | |
public void AddCustomFrameworksXcodeSettings(string folderRealPath, string localFilePath){ | |
// 独自ライブラリ、ファイルの追 | |
foreach(string dir in Directory.GetDirectories(folderRealPath)){ | |
//Debug.Log("dir: " + dir); | |
string makeWorldFolderPath =null; | |
string makeLocalFolderPath =null; | |
string dirFolderName = GetFolderName(dir); | |
Debug.Log("dirFolderName in dir: " + dirFolderName); | |
//一回目のループはfolderを足さない m_TopParentFolderNameが二重に足されるから | |
if (string.Equals (m_topParentFolderName_, dirFolderName)) { | |
makeWorldFolderPath = folderRealPath; | |
makeLocalFolderPath = localFilePath; | |
} | |
else{ | |
makeWorldFolderPath = Path.Combine (folderRealPath, dirFolderName); | |
makeLocalFolderPath = Path.Combine (localFilePath, dirFolderName); | |
} | |
//フォルダが.framework フォルダがあれば追加 | |
if(FolderHasframeworkExtension(dirFolderName)){ | |
customFrameworks_.Add(new CustomFramework(dirFolderName)); | |
continue; | |
} | |
else{ | |
//ファイルを追加 | |
AddFilesXcodeSettings (makeWorldFolderPath, makeLocalFolderPath); | |
} | |
AddCustomFrameworksXcodeSettings(makeWorldFolderPath, makeLocalFolderPath); | |
} | |
foreach(var cFramework in customFrameworks_) | |
{ | |
string guid = m_PBXProject.AddFile (cFramework.srcPath_, cFramework.srcPath_, PBXSourceTree.Source); | |
m_PBXProject.AddFileToBuild(m_Target, guid); | |
} | |
} | |
/// <summary> | |
/// file名が引数なら一つ上の階層のフォルダ、 | |
/// folder名なら最後のフォルダを取得 | |
/// </summary> | |
/// <returns>The folder name.</returns> | |
/// <param name="fileOrFolderName">File or folder name.</param> | |
private string GetFolderName(string fileOrFolderpath) | |
{ | |
//string NameExtension = Path.GetExtension(fileOrFolderName); | |
string fName =null; | |
if(File.Exists(fileOrFolderpath)){ | |
fName = Path.GetFileName( Path.GetDirectoryName( fileOrFolderpath ) ); | |
} | |
else if(Directory.Exists(fileOrFolderpath)) | |
{ | |
//Path.GetDirectoryNameはフォルダ名が最後にある場合、最後から2番目のフォルダを取得してしまうので使わない | |
int _tempCount = fileOrFolderpath.LastIndexOf("/"); | |
fName = fileOrFolderpath.Substring(_tempCount + 1); | |
} | |
return fName; | |
} | |
private bool FolderHasframeworkExtension(string FolderName){ | |
string folderNameOfExtentions = Path.GetExtension(FolderName); | |
if(string.Equals(folderNameOfExtentions, ".framework") == true){ | |
return true; | |
} | |
return false; | |
} | |
private void initPbxProject(string buildPath) | |
{ | |
m_PBXProject = new PBXProject(); | |
m_ProjPath = buildPath + "/Unity-iPhone.xcodeproj/project.pbxproj"; | |
m_PBXProject.ReadFromFile(m_ProjPath); | |
m_Target = m_PBXProject.TargetGuidByName ("Unity-iPhone"); | |
} | |
} | |
/// <summary> | |
/// その他ファイル | |
/// </summary> | |
public class CustomFile | |
{ | |
public string srcPath_; | |
public CustomFile() { } | |
public CustomFile(string srcPath) | |
{ | |
srcPath_ = srcPath; | |
} | |
} | |
/// <summary> | |
/// カスタムフレームワーク | |
/// </summary> | |
public class CustomFramework : CustomFile { | |
//フォルダ名が.frameworkなら追加する | |
public CustomFramework(string srcPath) | |
{ | |
srcPath_ = srcPath; | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment