因为Google广告政策的原因,游戏中Banner广告不能遮挡游戏操作元素,一种是把操作内容移动一下,留出广告空间。
另一种方法是将Unity画面压缩,把Banner广告位置显示为黑色。
为此我们需要修改Camera的投影矩阵。通过获取广告的高度来计算。
但是留出的位置还是会触发触摸事件,我们可以用一块黑色 的UI来遮挡
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
/// <summary>
/// banner广告出现后,场景自动压缩高度
/// 该脚本需要拖到Camera上面
/// </summary>
[DisallowMultipleComponent,RequireComponent(typeof(Camera))]
public class CameraBannerAdjust : MonoBehaviour {
public bool debug = false;
private Camera m_Camera;
public Camera cam{ get{ return m_Camera; }}
private float m_HeightScale = 1f;
private float m_CurrHeightScale=1f;
private float m_OffsetY;
private static RectTransform _Bottom;
private static List<CameraBannerAdjust> _instances = new List<CameraBannerAdjust>();
private float _bannerHeight=50f;
// Use this for initialization
void Start () {
if(debug || Application.platform == RuntimePlatform.Android || Application.platform== RuntimePlatform.IPhonePlayer)
{
m_Camera = GetComponent<Camera>();
_instances.Add(this);
}
_bannerHeight = PluginManager.Instance.adsPlugin.GetBannerSize().y;
}
// Update is called once per frame
void Update ()
{
if(m_Camera!=null){
UpdateHeightScaleByAdHeight();
if(m_CurrHeightScale!=m_HeightScale)
{
m_CurrHeightScale = Mathf.Lerp(m_CurrHeightScale,m_HeightScale,Time.deltaTime*3f);
UpdateCameraMatrix();
}
}
}
void UpdateHeightScaleByAdHeight(){
if(PluginManager.Instance.CheckAdIsShowed(AdType.AdTypeBannerAds)){
if(m_HeightScale==1f){
if(PluginManager.Instance.internalSDk.hasNotch()){
m_HeightScale = 1f-(_bannerHeight+34f)/Screen.height;
}else{
m_HeightScale = 1f-_bannerHeight/Screen.height;
}
}
}else{
m_HeightScale = 1f;
#if UNITY_EDITOR
if(debug) m_HeightScale = 0.9f;
#endif
}
}
void UpdateCameraMatrix()
{
if(m_Camera){
m_Camera.ResetProjectionMatrix();
var m = m_Camera.projectionMatrix;
m.m11 *= m_CurrHeightScale;
m_OffsetY = 1f-m_CurrHeightScale;
m.m13 += m_OffsetY;
m_Camera.projectionMatrix = m;
if(_Bottom==null){
CreateBottomCanvas();
}
_Bottom.sizeDelta = new Vector2(Screen.width,Screen.height*m_OffsetY);
}
}
/// <summary>
/// enable
/// </summary>
public static void Enable(){
if(_Bottom) _Bottom.gameObject.SetActive(true);
for(int i=0;i<_instances.Count;++i){
CameraBannerAdjust ins = _instances[i];
if(ins!=null && ins.cam!=null){
ins.enabled=true;
ins.UpdateCameraMatrix();
}
}
}
/// <summary>
/// disable
/// </summary>
public static void Disable(){
for(int i=0;i<_instances.Count;++i){
CameraBannerAdjust ins = _instances[i];
if(ins!=null && ins.cam!=null){
ins.enabled=false;
ins.cam.ResetProjectionMatrix();
}
}
if(_Bottom) _Bottom.gameObject.SetActive(false);
}
void CreateBottomCanvas(){
GameObject go = new GameObject("__BottomCanvas__");
go.hideFlags = HideFlags.HideInHierarchy;
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
Canvas canvas = go.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.sortingOrder = 9999;
go.AddComponent<GraphicRaycaster>();
GameObject imgGo = new GameObject("__Img__");
imgGo.transform.SetParent(go.transform);
imgGo.transform.localPosition = Vector3.zero;
imgGo.transform.localScale = Vector3.one;
Image img = imgGo.AddComponent<Image>();
img.color = Color.black;
_Bottom = img.rectTransform;
_Bottom.anchoredPosition = Vector2.zero;
_Bottom.pivot = new Vector2(0.5f,0f);
_Bottom.anchorMin = Vector2.zero;
_Bottom.anchorMax = new Vector2(1f,0f);
_Bottom.offsetMin = Vector2.zero;
}
void OnDestroy(){
int len = _instances.Count;
for(int i=0;i<len;++i){
if(_instances[i] == null || _instances[i]==this){
_instances.RemoveAt(i);
}
}
}
}