【发布时间】:2019-08-10 00:50:10
【问题描述】:
我正在统一编写应用程序。将来我需要能够从网络上提取图像并将它们用作跟踪器,因此我目前正在编写代码,在运行时将图像从列表中添加到 AugmentedImageDatabase。
但是,每次我添加图像时,AugmentedImageDatabase.AddImage() 方法都会返回 -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;
}
}
列表中的所有图像都已保存为 Sprite。
所有带有[SerializeField] 标签的变量都在统一编辑器中定义。
【问题讨论】:
-
你的纹理格式是什么。 ARCore 仅支持两种格式。你可以看看这个answer
-
尝试将格式更改为 rgb32,仍然无效。使用受支持的 .png 文件扩展名。
-
据我所知,代码错误
-1是您的nativeSession == null的情况。您是否尝试在 ARCore 会话中添加图像? -
@AliKanat 我将 ARCoreSession 文件传递给代码 sn-p 顶部的
session变量。这与传递给相机的会话相同。我还有一个被加载到imageDatabase中的AugmentedImageDatabase,“DB”。然后最后,我做session.AugmentedImageDatabase = imageDatabase -
另一个原因可能是您在
Start中启动协程,但可能在那个阶段会话刚刚初始化。在 Session 初始化后尝试通过触摸来执行此操作。您可以查看Session.Status。