public RawImage img_target;
private void Awake()
{
WX.InitSDK(InitCallBack);
WX.OnNeedPrivacyAuthorization((sub) =>
{
Debug.Log(sub);
});
WXInit();
}
public void WXInit()
{
AuthorizeOption ao = new AuthorizeOption();
ao.scope = "scope.camera";
WX.Authorize(ao);
sco = new StartCompassOption();
sco.success += Success;
sco.fail += Fail;
WX.StartCompass(sco);
WX.OnCompassChange((result) =>
{
Debug.Log(result.direction);
}
CreateCameraOption cco = new CreateCameraOption();
cco.devicePosition = "back";
cco.fail += Fail;
cco.success += Success;
WXCamera wxCam = WXBase.CreateCamera(cco);
wxCam.ListenFrameChange();
wxCam.OnCameraFrame((data) =>
{
Debug.Log("=====" + data.data.Length);
Texture2D texture = ConvertArrayBufferToTexture(data.data);
if (texture != null)
{
img_target.material.mainTexture = texture;
}
});
var a = WX.GetAppAuthorizeSetting();
Debug.Log("摄像头权限:" + a.cameraAuthorized);
}
public void InitCallBack(int i)
{
Debug.Log("WXInit Success!!-----------" + i);
}
public void Success(GeneralCallbackResult gg)
{
var a = WX.GetAppAuthorizeSetting();
Debug.Log("相机权限:" + a.cameraAuthorized);
}
public void Fail(GeneralCallbackResult gg)
{
Debug.LogError("失败!!");
}
Texture2D ConvertArrayBufferToTexture(byte[] buffer)
{
int width = 500;
int height = 500;
Texture2D texture = new Texture2D(width, height);
if (texture.LoadImage(buffer))
{
return texture;
}
return null;
}
没有做小游戏开发,相机开启失败,在失败回调里应该有错误原因,您在Fail函数中打印一下gg参数试试