Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a XR referenceImagelibrary through a script #1175

Open
linlifeng1995 opened this issue Apr 26, 2024 · 2 comments
Open

How to add a XR referenceImagelibrary through a script #1175

linlifeng1995 opened this issue Apr 26, 2024 · 2 comments
Labels
how to Explains how to accomplish something

Comments

@linlifeng1995
Copy link

linlifeng1995 commented Apr 26, 2024

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 linlifeng1995 added the how to Explains how to accomplish something label Apr 26, 2024
@linlifeng1995 linlifeng1995 changed the title How to add a referencelibrary through a script How to add a XR referenceImagelibrary through a script Apr 26, 2024
@linlifeng1995
Copy link
Author

i use the visionOs platform

@esbylan
Copy link

esbylan commented May 8, 2024

Same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
how to Explains how to accomplish something
Projects
None yet
Development

No branches or pull requests

2 participants