San*_*ada 5 c# unity-game-engine augmented-reality vuforia
我正在尝试在运行时动态创建图像目标。为此,我从 vuforia AR 相机捕获屏幕并将其保存在 Application.persistentDataPath 中。然后,我按照如何在 Unity 中创建和加载目标中提供的脚本创建数据集可跟踪行为。
我的捕获屏幕的代码:
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);
Run Code Online (Sandbox Code Playgroud)
创建可跟踪行为的代码:
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>();
Run Code Online (Sandbox Code Playgroud)
这在我的 Windows 笔记本电脑上运行良好,但在我的 Android 手机上不起作用。经过一番调试后,我发现这bool result是错误的,因此,var trackableBehaviour变成了null。
编辑:问题不在于写入权限。我可以看到图像保存在持久数据路径中。问题在于将运行时图像源设置为保存的图像。
编辑2:我尝试直接使用纹理而不是像这样的文件bool result = runtimeImageSource.SetImage(tex, 1f, "Temp");。结果仍然是假的。因此,我认为问题不在于路径解析,而在于其他问题。