facebook提供的unity原生广告插件 是根据摄像机内是否有广告展示对象来判断impression的
摄像机获取方式如下
GameObject currentObject = this.gameObject;
Camera camera = this.camera;
if (camera == null) {
camera = this.GetComponent<Camera>();
}
if (camera == null) {
camera = Camera.main;
}
一般使用FairyGUI的2D工程,是不需要MainCamera的,直接使用FairyGUI的Stage Camera,如果Scene里默认的MainCamera如果没有去掉,那么Camera.main就会取到MainCamera,这样在后续检测原生广告是否在相机内显示时就会出错
Vector3[] worldCorners = new Vector3[4];
transform.GetWorldCorners(worldCorners);
Vector3 gameObjectBottomLeft = worldCorners [0];
Vector3 gameObjectTopRight = worldCorners [2];
Vector3 cameraBottomLeft = camera.pixelRect.min;
Vector3 cameraTopRight = camera.pixelRect.max;
if (canvas.renderMode != RenderMode.ScreenSpaceOverlay) {
gameObjectBottomLeft = camera.WorldToScreenPoint(gameObjectBottomLeft);
gameObjectTopRight = camera.WorldToScreenPoint(gameObjectTopRight);
}
// Check that gameObject has 100% width visible
if (gameObjectBottomLeft.x < cameraBottomLeft.x || gameObjectTopRight.x > cameraTopRight.x) {
return this.logViewability(false, "Less than 100% of the width of the GameObject is inside the viewport.");
}
因为FairyGUI是用Stage Camera渲染的。
解决方式就是让Camera.main这个接口获取到的相机改成Stage Camera。
所以我们把Stage Camera的tag设置为MainCamera,同时根据实际项目需求去掉Main Camera或者把Main Camera的tag改成untagged或者其他(Camera.main获取的是第一个tag设置为MainCamera的相机)。
这样问题就解决了
另外也可以在注册广告点击对象的时候把对应的相机作为参数传入
nativeAd.RegisterGameObjectForImpression(this.gameObject, new Button[] { callToActionButton}, yourStageCamera);