How to add images to Images.xcassets

Hi! I was wondering how I could add images to my Xcode build without having to manually place them in Images.xcassets every time I build my Unity-VisionOS project. I have some images I that want to display in a SwiftUI window. Is there a special folder I would have to place these images into for them to be automatically included in the build as Swift assets? Thank you for your help!

I’m not aware of any special folder, but I believe what you can do is add a custom BuildProcessor to your project that uses PBXProject to add files to the build folder and the Xcode project. We use that method internally to modify the project in certain cases. The code looks something like this:

public void OnPostprocessBuild(BuildReport report)
{
     var project = new PBXProject();

     var pbxProjectPath = Path.Combine(report.summary.outputPath, "Unity-VisionOS.xcodeproj/project.pbxproj");

     project.ReadFromString(File.ReadAllText(pbxProjectPath));

     var relativePathToFolder = Path.Combine("Unity-VisionOS", "YourFolderHere.xcassets");
     var fullPathToFolder = Path.Combine(report.summary.outputPath, relativePathToFolder);

     // Here's where you create/update the .xcassets folder and its contents at fullPathToFolder.

     var folderGuid = project.AddFile(relativePathToFolder, relativePathToFolder);
     project.AddFileToBuild(project.GetUnityMainTargetGuid(), folderGuid);

     File.WriteAllText(pbxProjectPath, project.WriteToString());
}

Hope that helps!

1 Like

Thank you! This worked; using similar code I was also able to copy over other files into the Xcode build.

At first I tried to copy the image files over directly but it wouldn’t show up in Xcode. I realized I had to create a .imageset folder for each image and within that folder include the original image and a Contents.json file pointing to the image. To reference images inside a subfolder in Images.xcassets like subfolder/image, I had to make another Contents.json with the “Provides namespace” property set to true and place that in the subfolder (equivalent of creating a folder with the “Provides namespace” box checked in Xcode).

1 Like