C# 无法将文件/图像设置为对象跟踪器的RunTimeImageSource
我试图在运行时动态创建图像目标。为此,我从vuforia AR摄像头捕获屏幕并将其保存在Application.persistentDataPath中。然后,我将按照中提供的脚本创建一个数据集可跟踪的行为C# 无法将文件/图像设置为对象跟踪器的RunTimeImageSource,c#,unity3d,augmented-reality,vuforia,C#,Unity3d,Augmented Reality,Vuforia,我试图在运行时动态创建图像目标。为此,我从vuforia AR摄像头捕获屏幕并将其保存在Application.persistentDataPath中。然后,我将按照中提供的脚本创建一个数据集可跟踪的行为 我捕获屏幕的代码: var datapath = Application.persistentDataPath; datapath = Path.Combine(datapath, "image.jpg&quo
我捕获屏幕的代码:
var datapath = Application.persistentDataPath;
datapath = Path.Combine(datapath, "image.jpg");
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToJPG();
File.WriteAllBytes(datapath, bytes);
用于创建可跟踪行为的代码:
var objectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
var runtimeImageSource = objectTracker.RuntimeImageSource;
//Problem here
bool result = runtimeImageSource.SetFile(VuforiaUnity.StorageType.STORAGE_ABSOLUTE, datapath, 0.15f, "Temp");
// create a new dataset and use the source to create a new trackable
var dataset = objectTracker.CreateDataSet();
var trackableBehaviour = dataset.CreateTrackable(runtimeImageSource, "Temp");
// add the DefaultTrackableEventHandler to the newly created game object
trackableBehaviour.gameObject.AddComponent<DefaultTrackableEventHandler>();
var objectTracker=TrackerManager.Instance.GetTracker();
var runtimeImageSource=objectTracker.runtimeImageSource;
//这里有问题
bool result=runtimeImageSource.SetFile(vuforiaaUnity.StorageType.STORAGE_绝对值,数据路径,0.15f,“Temp”);
//创建一个新的数据集,并使用源创建一个新的可跟踪数据集
var dataset=objectTracker.CreateDataSet();
var trackablebehavior=dataset.CreateTrackable(runtimeImageSource,“Temp”);
//将DefaultTrackableEventHandler添加到新创建的游戏对象中
trackablebehavior.gameObject.AddComponent();
这在我的windows笔记本电脑上运行良好,但在我的android手机上不起作用。经过一些调试后,我发现bool result
为false,因此,var trackablebehavior
变为null
编辑:问题不在于写入权限。我可以看到在persistentDataPath上保存的图像。问题在于将运行时图像源设置为保存的图像 编辑2:我尝试直接使用纹理而不是像这样的文件
bool result=runtimeImageSource.SetImage(tex,1f,“Temp”)代码>。结果仍然是错误的。因此,我认为问题不在于路径解析,而在于其他方面。
- 首先,尝试使用VuforiaaUnity.StorageType.STORAGE\u应用程序而不是绝对应用程序
- 第二:尝试bool result=DataSet.Exists(字符串路径,VuforiaUnity.StorageType-StorageType)并调试手机上哪个路径将返回true
- Application.persistentDataPath返回不带反斜杠的路径,这意味着尝试添加“/image.jpg”而不是image.jpg,这可能也是问题所在
我们无法使用DataSet.Exists,因为DataSet是在bool result=runtimeImageSource.SetFile(VuforiaaUnity.StorageType.STORAGE_ABSOLUTE,datapath,0.15f,“Temp”)之后创建的代码>这本身就是导致问题的原因。另外,我正在使用Path.Combine,它添加了开箱即用的反斜杠。我可以看到图像保存在路径中(正如我在问题中提到的),但问题是将其设置为RuntimeImageSource请查看编辑2