Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
2.97k stars 1.11k forks source link

How to add a XR referenceImagelibrary through a script #1175

Open linlifeng1995 opened 2 months ago

linlifeng1995 commented 2 months ago

I need to use over 200 images ` public static class AddTrackImageLibraryTest { [MenuItem("DebugTool/Add Image Lib")] public static void AddImageLibrary() { string selectedFolder = AssetDatabase.GetAssetPath(Selection.activeObject); XRReferenceImageLibrary lib = Selection.activeObject as XRReferenceImageLibrary;

    Debug.Log("current count:" + lib.count);
    foreach (var img in lib)
    {
        Debug.Log("img name:" + img.name);
    }
}

[MenuItem("Assets/Create XRReferenceImageLibrary From Folder", false, 0)]
static void CreateXRReferenceImageLibrary()
{
    // 获取选中的文件夹路径
    string selectedFolderPath = AssetDatabase.GetAssetPath(Selection.activeObject);
    if (!AssetDatabase.IsValidFolder(selectedFolderPath))
    {
        Debug.LogError("Please select a folder in the Project window.");
        return;
    }

    // 创建 XRReferenceImageLibrary
    XRReferenceImageLibrary library = ScriptableObject.CreateInstance<XRReferenceImageLibrary>();
    string folderName = Path.GetFileNameWithoutExtension(selectedFolderPath);
    string libraryAssetPath = Path.Combine(selectedFolderPath, $"../{folderName}.asset");
    string name = UnityEditor.AssetDatabase.GenerateUniqueAssetPath(libraryAssetPath);
    AssetDatabase.CreateAsset(library, name);
    // 获取文件夹中的所有图片文件
    string[] guids = AssetDatabase.FindAssets("t:Texture", new string[] { selectedFolderPath });
    foreach (string guid in guids)
    {
        string texturePath = AssetDatabase.GUIDToAssetPath(guid);
        Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
        if (texture != null)
        {

            int index = library.count;
            string textureName = Path.GetFileNameWithoutExtension(texturePath);
            string newName = textureName;

            // 检查图片名称是否包含"Marker"
            if (textureName.Contains("Marker"))
            {
                // 提取数字部分
                string[] parts = textureName.Split(new[] { "Marker" }, StringSplitOptions.None);
                if (parts.Length > 1)
                {
                    string numberPart = parts[1];
                    // 去除前导零
                    numberPart = numberPart.TrimStart('0');
                    // 构建新名称
                    newName = "XXX-" + numberPart;
                }
            }

            XRReferenceImageLibraryExtensions.Add(library);
            XRReferenceImageLibraryExtensions.SetName(library, index, newName);
            XRReferenceImageLibraryExtensions.SetSpecifySize(library, index, true);
            XRReferenceImageLibraryExtensions.SetSize(library, index, new Vector2(0.12f, 0.12f));
            XRReferenceImageLibraryExtensions.SetTexture(library, index, texture, false);
        }
    }

    // 保存 XRReferenceImageLibrary 到文件夹同目录下

    AssetDatabase.SaveAssets();
    EditorUtility.FocusProjectWindow();
    Selection.activeObject = library;
    Debug.Log("XRReferenceImageLibrary created at: " + libraryAssetPath);
    AssetDatabase.Refresh();
}

// 确保只在选择了文件夹时才显示菜单项
[MenuItem("Assets/Create XRReferenceImageLibrary From Folder", true)]
static bool ValidateCreateXRReferenceImageLibrary()
{
    string selectedFolderPath = AssetDatabase.GetAssetPath(Selection.activeObject);
    return AssetDatabase.IsValidFolder(selectedFolderPath);
}

} `

This is the script I am currently using,when i add more than 200 images. ar trackable system not work,and i see this error "ar_image_tracking_provider_t <0x3008468b0>: Failed to set up image tracking with error: Too many images were supplied."

linlifeng1995 commented 2 months ago

i use the visionOs platform

esbylan commented 1 month ago

Same problem.