112 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
| 
 | ||
| using System.Collections;
 | ||
| using System.Collections.Generic;
 | ||
| using UnityEngine;
 | ||
| using UnityEngine.UI;
 | ||
| 
 | ||
| public class NativeAd : MonoBehaviour
 | ||
| {
 | ||
|     protected static RectTransform s_NativeAd;
 | ||
|     private static NativeAd s_instance;
 | ||
|     public static NativeAd Instance
 | ||
|     {
 | ||
|         get
 | ||
|         {
 | ||
|             if (s_instance == null)
 | ||
|             {
 | ||
|                 Debug.LogError("s_instance == null ");
 | ||
|             }
 | ||
|             return s_instance;
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
|     [SerializeField]
 | ||
|     private Camera uiCamera;
 | ||
|    
 | ||
|     bool horizontal = true;
 | ||
| 
 | ||
|     private void Awake()
 | ||
|     {
 | ||
|         s_instance = this;
 | ||
|         
 | ||
|     }
 | ||
|     public void ShowNativeAd()
 | ||
|     {
 | ||
|         if (ToukaAdManager.Instance.IsReadyNative)
 | ||
|         {
 | ||
|             ToukaAdManager.Instance.ShowNative(GetX(), GetY(), GetWidth(), GetHeight());
 | ||
|         }
 | ||
|     }
 | ||
|     //由于loadnative 函数有个问题,比如多个位置多个不同尺寸的native。目前只能处理一个尺寸,所以设置了firstFlg,位置宽高得到一次即可。
 | ||
|     static bool firstFlg = true;
 | ||
|     static float m_width;
 | ||
|     static float m_height;
 | ||
|     static float m_x;
 | ||
|     static float m_y;
 | ||
|     public float GetWidth()
 | ||
|     {
 | ||
|         FirstGetWAndH();
 | ||
| 
 | ||
|         return m_width;
 | ||
|     }
 | ||
|     public float GetHeight()
 | ||
|     {
 | ||
|         FirstGetWAndH();
 | ||
|         return m_height;
 | ||
|     }
 | ||
|     public float GetX()
 | ||
|     {
 | ||
|         FirstGetWAndH();
 | ||
|         return m_x;
 | ||
|     }
 | ||
|     public float GetY()
 | ||
|     {
 | ||
|         FirstGetWAndH();
 | ||
|         return m_y;
 | ||
|     }
 | ||
| 
 | ||
|     void FirstGetWAndH()
 | ||
|     {
 | ||
|         if (firstFlg)
 | ||
|         {
 | ||
|             if(uiCamera==null)
 | ||
|             {
 | ||
|                 uiCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
 | ||
|             }
 | ||
|             Debug.Log("YANGWY "+uiCamera);
 | ||
|             Vector3 lefttop = uiCamera.WorldToScreenPoint(new Vector3(transform.position.x - GetComponent<RectTransform>().rect.size.x / 2 * transform.lossyScale.x, transform.position.y + GetComponent<RectTransform>().rect.size.y / 2 * transform.lossyScale.y, transform.position.z));
 | ||
|             Vector3 rightbottom = uiCamera.WorldToScreenPoint(new Vector3(transform.position.x + GetComponent<RectTransform>().rect.size.x / 2 * transform.lossyScale.x, transform.position.y - GetComponent<RectTransform>().rect.size.y / 2 * transform.lossyScale.y, transform.position.z));
 | ||
|             Vector3 picture_zero = uiCamera.WorldToScreenPoint(transform.position);
 | ||
|             float x = lefttop.x;
 | ||
|             float y = rightbottom.y;
 | ||
|             float width = rightbottom.x - lefttop.x;
 | ||
|             float height = Mathf.Abs(rightbottom.y - lefttop.y);
 | ||
|             y = Screen.height - (picture_zero.y + height / 2);
 | ||
|             if (horizontal)
 | ||
|             {
 | ||
|                 if (height > width)
 | ||
|                 {
 | ||
|                     height = width;
 | ||
|                 }
 | ||
|                 if (width > height * 1.2f)
 | ||
|                 {
 | ||
|                     x += (width - (height * 1.2f)) / 2;
 | ||
|                     width = height * 1.2f;
 | ||
| 
 | ||
|                 }
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
| 
 | ||
|             }
 | ||
|             m_x = x;
 | ||
|             m_y = y;
 | ||
|             m_width = width;
 | ||
|             m_height = height;
 | ||
|             firstFlg = false;
 | ||
|         }
 | ||
|     }
 | ||
| 
 | ||
| }
 |