在Unity BuildPostProcessor中,您如何简单地“复制"?一个文件? [英] In Unity BuildPostProcessor, how exactly do you, simply, "copy over" one file?
问题描述
在Unity中,我只是在Assets/
in Unity I simply have a file, afile.ts, sitting in Assets/
在内置的Xcode中,我只是希望它位于项目中包含的项目文件夹中(真的,任何地方都可以).
In the built Xcode, I simply want it to be sitting in the project folder (really, anywhere is fine) included in the project.
您可以使用
project.AddFileToBuild(g, something something something);
但是我很简单无法弄清.
(显然在Unity中完全没有记录.)
(Obviously it is totally undocumented at Unity.)
如何完成?只是一个简单的文件-实际上是将其从Assets/filename复制到Xcode项目(它将具有目标成员身份,并位于主捆绑包中.)
How is it done? Just the one simple file - actually copy it from Assets/filename to the Xcode project (and it will have target membership and be in the main bundle).
这是一个完美的Unity构建后脚本BuildPostProcessor.cs,它很棒,可以完成其他所有操作:
https://stackoverflow.com/a/54370793/294884
但是我无法复制该死的文件!怎么做?
but I cannot copy over a damned file! How to?
注意.如果您只是简单地将文件放在Unity的名称不正确的magic文件夹中:
Note. If you very simply put a file in Unity's badly-named magic folder:
实际上,它确实执行了本质量检查中正在讨论的内容.
in fact it does exactly what is under discussion in this QA.
- Unity ..在/StreamingAssets中有一个music.mp4或mesh.txt文件
- 构建到Xcode
- 您将在Xcode项目中将Music.mp4或mesh.txt包含在Target中
- 并且iOS位置将为...
iOS上的文件夹名称,无论出于何种原因变为:/Data/Raw
因此,您的底层iOS辅助代码为:
the folder name on iOS for whatever reason becomes: /Data/Raw
Thus your low-level iOS side code would be:
而不是例如:
CFURLRef imageURL = CFBundleCopyResourceURL(
mainBundle, CFSTR("music"), CFSTR("mp4"), NULL);
您将拥有:
CFURLRef imageURL = CFBundleCopyResourceURL(
mainBundle, CFSTR("Data/Raw/music"), CFSTR("mp4"), NULL);
那很好.但是可以肯定的是,我们可以学习如何使用似乎存在的'.AddFileToBuild`.
That works fine. But surely we can learn how to use '.AddFileToBuild` which seems to exist for the purpose.
推荐答案
FTR我确实偶然发现了一些Unity示例代码(在一个不相关的示例项目中),这可能会有所帮助..
FTR I did stumble on to some Unity example code (in an unrelated example project) which may help ..
public class MyBuildPostprocessor
{
// Build postprocessor. Currently only needed on:
// - iOS: no dynamic libraries, so plugin source files have to be copied into Xcode project
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target == BuildTarget.iOS)
OnPostprocessBuildIOS(pathToBuiltProject);
}
private static void OnPostprocessBuildIOS(string pathToBuiltProject)
{
// We use UnityEditor.iOS.Xcode API which only exists in iOS editor module
#if UNITY_IOS
string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
UnityEditor.iOS.Xcode.PBXProject proj = new UnityEditor.iOS.Xcode.PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
Directory.CreateDirectory(Path.Combine(pathToBuiltProject, "Libraries/Unity"));
string[] filesToCopy = new string[]
{
"PlatformBase.h",
"RenderAPI_Metal.mm",
"RenderAPI_OpenGLCoreES.cpp",
"RenderAPI.cpp",
"RenderAPI.h",
"RenderingPlugin.cpp",
};
for(int i = 0 ; i < filesToCopy.Length ; ++i)
{
var srcPath = Path.Combine("../PluginSource/source", filesToCopy[i]);
var dstLocalPath = "Libraries/" + filesToCopy[i];
var dstPath = Path.Combine(pathToBuiltProject, dstLocalPath);
File.Copy(srcPath, dstPath, true);
proj.AddFileToBuild(target, proj.AddFile(dstLocalPath, dstLocalPath));
}
File.WriteAllText(projPath, proj.WriteToString());
#endif // #if UNITY_IOS
}
}
这篇关于在Unity BuildPostProcessor中,您如何简单地“复制"?一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!