我正在统一编写一个应用程序。将来,我需要能够从网络中汲取图像并将其用作跟踪器,因此我目前正在编写代码,将图像从列表中添加到运行时的增强型杂志。
每次添加图像时,AugmentedImageDatabase.AddImage()
方法都会返回A -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;
}
}
列表中的所有图像都保存为精灵。所有带有[SerializeField]
标签的变量均在Unity编辑器中定义。
由于OP没有回复,所以我假设我的评论解决了问题并将我的评论转换为答案。
ARCORE仅支持Augmented Image Database
的两种纹理格式(RGBA32或RGB24)。因此,必须首先转换纹理才能将图像添加到数据库中。
OP代码中的第二个问题是他试图将图像添加到Start
中的数据库中,该图像显然在启动应用程序时被执行。因此,会话状态是None
或Initializing
,导致LifecycleManager.Instance.NativeSession
返回null。由于目前没有Session
,您无法将图像添加到数据库中。