C# Can';t在ARCore中将图像添加到AugmenteImage数据库
我正在联合写一份申请书。将来我需要能够从web上提取图像并将其用作跟踪器,因此我目前正在编写代码,在运行时将列表中的图像添加到AugmenteImage数据库中 但是,每次添加图像时,C# Can';t在ARCore中将图像添加到AugmenteImage数据库,c#,unity3d,arcore,C#,Unity3d,Arcore,我正在联合写一份申请书。将来我需要能够从web上提取图像并将其用作跟踪器,因此我目前正在编写代码,在运行时将列表中的图像添加到AugmenteImage数据库中 但是,每次添加图像时,AugmentedImageDatabase.AddImage()方法返回-1。这意味着添加图像时出错,但没有说明是哪种错误。我已经检查了API文档,但是它们也没有添加任何信息 为什么我的代码不向AugmenteImage数据库添加图像 public class DataBaseGenerator : MonoBe
AugmentedImageDatabase.AddImage()
方法返回-1。这意味着添加图像时出错,但没有说明是哪种错误。我已经检查了API文档,但是它们也没有添加任何信息
为什么我的代码不向AugmenteImage数据库添加图像
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;
}
}
公共类数据库生成器:MonoBehavior{
[序列化字段]
ARCoreSessionConfig会议;
[序列化字段]
增强图像数据库;
[序列化字段]
列表图像;
私有内部错误索引=0;
//第一帧会发生什么
无效开始()
{
CreateDatabase();
}
私有void CreateDatabase()
{
int i=0;
foreach(图像中的纹理2D纹理)
{
string name=“跟踪器”;
Texture2D rgbTexture=新的Texture2D(texture.width、texture.height、TextureFormat.RGBA32,false);
rgbTexture.SetPixels(texture.GetPixels());
rgbTexture.Apply();
ErrorIndex=imageDatabase.AddImage(名称,rgbTexture,0);
GameUtility.ShowToastMessage(ErrorIndex.ToString());
Debug.Log(名称+”:“+ErrorIndex);
i++;
}
session.AugmentedImageDatabase=imageDatabase;
}
}
列表中的所有图像都已保存为精灵。
带有
[SerializeField]
标记的所有变量都在unity编辑器中定义。由于OP没有回复,我假设我的评论解决了问题,并将我的评论转换为答案
ARCore仅支持增强图像数据库的两种纹理格式(RGBA32或RGB24)。因此,为了能够将图像添加到数据库中,必须首先转换纹理
OP代码中的第二个问题是他试图在
Start
中将图像添加到数据库中,这显然是在应用程序启动时执行的。因此,会话状态为None
或initialized
,这将导致LifecycleManager.Instance.NativeSession
返回null。由于目前没有会话
,因此无法将图像添加到数据库。纹理的格式是什么。ARCore仅支持两种格式。你可以看看这个,尝试将格式更改为rgb32,仍然不起作用。使用.png文件扩展名,这是受支持的。从代码错误-1
来看,当您的nativeSession==null
时,情况就是这样。您正在尝试在ARCore会话中添加图像吗?@AliKanat我将ARCoreSession文件传递给代码段顶部的session
变量。这与传递给摄影机的会话相同。我还有一个AugmentedImageDatabase“DB”,它被加载到imageDatabase
中。最后,我做了session.AugmentedImageDatabase=imageDatabase
另一个原因可能是您在start
中启动了协同程序,但可能在该阶段会话只是初始化。在会话初始化后,尝试通过触摸来执行此操作。您可以检查会话状态。确实,此错误已解决。您可以使用private void OnEnable(){LifecycleManager.Instance.EarlyUpdate+=CreateDatabase}
在会话生效时执行该操作,但我现在收到一个不同的错误,可能是这种方式导致的。