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

Unity 2020.3.20f1运行时添加图片到XR参考图像库报错求助

解决Unity AR运行时添加图片到Reference Image Library的报错问题

你在Unity 2020.3.20f1中开发AR应用时,尝试运行时从文件夹加载图片添加到Reference Image Library,遇到了XRReferenceImageLibrary没有Add方法的编译错误,核心原因有两个:

  1. XRReferenceImageLibrary是只读抽象类,不支持直接修改,必须使用专门用于运行时编辑的MutableRuntimeReferenceImageLibrary
  2. XRReferenceImage的构造函数为内部实现,不能手动实例化,必须通过MutableRuntimeReferenceImageLibrary的方法添加图片

修正后的完整代码

using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using System;
using System.IO;

public class RuntimeImageLoader : MonoBehaviour
{
    public ARTrackedImageManager trackedImageManager;
    public string imagesFolderName = "RuntimeImages";

    private void Start()
    {
        // 将Reference Library转换为可编辑的Mutable版本
        var mutableLibrary = trackedImageManager.referenceLibrary as MutableRuntimeReferenceImageLibrary;
        if (mutableLibrary == null)
        {
            Debug.LogError("无法修改当前Reference Library,请确保在AR Tracked Image Manager中使用的是Runtime Reference Image Library");
            return;
        }

        // 检查目标文件夹是否存在
        var imagesFolder = $"{Application.dataPath}/{imagesFolderName}";
        if (!Directory.Exists(imagesFolder))
        {
            Debug.LogError($"文件夹不存在:{imagesFolder}");
            return;
        }

        var imageFiles = Directory.GetFiles(imagesFolder);
        foreach (var file in imageFiles)
        {
            // 过滤仅处理图片文件
            var extension = Path.GetExtension(file).ToLower();
            if (extension != ".png" && extension != ".jpg" && extension != ".jpeg")
                continue;

            // 读取图片数据并创建纹理
            var imageData = File.ReadAllBytes(file);
            var texture = new Texture2D(2, 2);
            if (!texture.LoadImage(imageData))
            {
                Debug.LogError($"加载图片失败:{file}");
                Destroy(texture);
                continue;
            }

            // 配置纹理属性以适配AR识别
            texture.wrapMode = TextureWrapMode.Clamp;
            texture.filterMode = FilterMode.Bilinear;

            // 设置图片实际物理尺寸(单位:米,根据实际打印大小调整)
            var imageSize = new Vector2(texture.width, texture.height) / 1000f;
            var imageName = Path.GetFileNameWithoutExtension(file);

            // 将图片添加到可编辑库
            var imageGuid = mutableLibrary.Add(texture, imageName, imageSize);
            Debug.Log($"成功添加图片:{imageName},GUID:{imageGuid}");

            // 销毁纹理释放内存(若后续无需使用)
            Destroy(texture);
        }

        // 更新AR管理器的引用库(部分版本需手动触发)
        trackedImageManager.referenceLibrary = mutableLibrary;
    }
}

关键注意事项

  • 使用正确的Reference Library类型:在AR Tracked Image Manager组件的Inspector面板中,必须选择Runtime Reference Image Library作为Reference Library,普通的Reference Image Library是只读的,无法运行时修改。
  • 图片尺寸设置:代码中imageSize的单位是米,需要根据你实际打印的图片物理尺寸调整,比如实际图片宽10cm,就设置为0.1f,否则识别精度会受影响。
  • 纹理可读性:通过LoadImage加载的纹理默认是可读的,但如果是从其他渠道获取的纹理,需要勾选Texture Import Settings中的Read/Write Enabled。
  • 临时数据特性:运行时添加的图片仅在当前应用会话有效,重启应用后需要重新加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 04:55:03