无法将图像添加到 ARCore 中的 AugmentedImageDatabase

Can't add images to AugmentedImageDatabase in ARCore

我正在统一编写一个应用程序。将来我需要能够从网络中提取图像并将它们用作跟踪器,因此我目前正在编写代码,在运行时将列表中的图像添加到 AugmentedImageDatabase。

然而,每次我添加图像时,AugmentedImageDatabase.AddImage() 方法 returns a -1。这意味着添加图像时出错,但没有说明错误类型。我检查了 API 文档,但他们也没有添加任何信息。

为什么我的代码不向 AugmentedImageDatabase 添加图像?

public class DataBaseGenerator : MonoBehaviour {
    [SerializeField]
    ARCoreSessionConfig session;

    [SerializeField]
    AugmentedImageDatabase imageDatabase;

    [SerializeField]
    List<Texture2D> image;

    private int ErrorIndex = 0;

    // What happens on the first frame
    void Start ()
    {
        CreateDatabase();
    }
    private void CreateDatabase()
    {
        int i = 0;
        foreach (Texture2D texture in image)
        {
            string name = "Tracker";
            Texture2D rgbTexture = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
            rgbTexture.SetPixels(texture.GetPixels());
            rgbTexture.Apply();

            ErrorIndex = imageDatabase.AddImage(name, rgbTexture, 0);
            GameUtility.ShowToastMessage(ErrorIndex.ToString());
            Debug.Log(name + ": " + ErrorIndex);
            i++;
        }
        session.AugmentedImageDatabase = imageDatabase;
    }
}

列表中的所有图像都已保存为精灵。 所有带有 [SerializeField] 标签的变量都在 unity 编辑器中定义。

由于 OP 没有回复我假设我的评论解决了问题并将我的评论转换为答案。

ARCore 仅支持 Augmented Image Database 的两种纹理格式(RGBA32 或 RGB24)。因此,必须首先转换纹理才能将图像添加到数据库中。

OP 代码中的第二个问题是他试图将图像添加到 Start 中的数据库,这显然是在应用程序启动时执行的。因此,会话状态是 NoneInitializing,这会导致 LifecycleManager.Instance.NativeSession 返回 null。由于目前没有 Session 您无法将图像添加到数据库。