You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中ARCore的AugmentedImageDatabase无法添加图片求助

Why does AugmentedImageDatabase.AddImage() return -1 in Unity ARCore?

Let's break down the possible reasons your AddImage() call is returning -1 (indicating a failure) and how to fix them:

1. Your AugmentedImageDatabase is read-only

The imageDatabase you assigned in the Editor is likely a pre-built database (e.g., exported via ARCore Image Generator), which is read-only at runtime. You can't modify these pre-generated databases—you need to create a new, empty one in code instead.

Fix:
Replace your serialized imageDatabase field with a runtime-initialized one:

public class DataBaseGenerator : MonoBehaviour {
    [SerializeField] ARCoreSessionConfig session;
    [SerializeField] List<Texture2D> image;
    private AugmentedImageDatabase imageDatabase; // Don't serialize this
    private int ErrorIndex = 0;

    void Start () {
        CreateDatabase();
    }

    private void CreateDatabase() {
        // Initialize a NEW empty database using your session config
        imageDatabase = new AugmentedImageDatabase(session);
        int i = 0;
        foreach (Texture2D texture in image) {
            string name = $"Tracker_{i}"; // Unique names help with debugging
            // Skip redundant texture copy if original is valid (see point 4)
            if (texture.format != TextureFormat.RGBA32 && texture.format != TextureFormat.RGB24)
            {
                Debug.LogError($"Texture {texture.name} uses unsupported format {texture.format}");
                i++;
                continue;
            }
            ErrorIndex = imageDatabase.AddImage(name, texture, 0.1f); // Use a valid physical size
            Debug.Log($"{name}: {ErrorIndex}");
            i++;
        }
        session.AugmentedImageDatabase = imageDatabase;
    }
}

2. Your Sprite/Texture doesn't meet ARCore's tracking requirements

ARCore has strict rules for valid tracking images, and even small mismatches will cause AddImage() to fail:

  • Texture Format: Must be uncompressed (RGBA32 or RGB24). Sprites often use compressed formats by default.
  • Mipmaps: Must be disabled—ARCore can't use textures with mipmaps for tracking.
  • Read/Write Enabled: The texture's Read/Write Enabled option in the Inspector must be checked, otherwise GetPixels() will return invalid data.
  • Resolution: Images must be between 150x150 and 4096x4096 pixels. Square images (1:1 aspect ratio) are recommended, but non-square works as long as the ratio isn't extreme.

Fix:
In the Inspector for each Sprite's source Texture:

  • Set Texture Type to Texture2D (or keep as Sprite, but adjust advanced settings)
  • Under Advanced:
    • Uncheck Generate Mip Maps
    • Set Format to RGBA 32 bit
    • Check Read/Write Enabled

3. Invalid physical size parameter

You're passing 0 as the third parameter to AddImage(), which tells ARCore to auto-calculate the physical size. While this is allowed, it can sometimes fail if the image metadata is incomplete.

Fix:
Pass a realistic physical size (in meters) that matches how you'll use the image. For example, if your printed tracker is 10cm wide, pass 0.1f:

ErrorIndex = imageDatabase.AddImage(name, texture, 0.1f);

4. Redundant texture copying might introduce errors

Your code creates a new rgbTexture and copies pixels from the original, but this is unnecessary if the original texture already meets ARCore's requirements. This extra step can introduce errors if the copy process isn't perfect (e.g., mismatched texture formats).

Fix:
Skip the texture copy entirely if the original texture is valid, as shown in the modified code in point 1.


内容的提问来源于stack exchange,提问作者MyNameIsGuzse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:31:00