111 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C#
		
	
	
	
using System.Collections;
 | 
						||
using System.Collections.Generic;
 | 
						||
using UnityEngine;
 | 
						||
using UnityEngine.UI;
 | 
						||
 | 
						||
namespace Touka
 | 
						||
{
 | 
						||
    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(string nativePos)
 | 
						||
        {
 | 
						||
            if (TKGSDKManager.Instance.IsNativeReady())
 | 
						||
            {
 | 
						||
                TKGSDKManager.Instance.ShowNative(nativePos, 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)
 | 
						||
            {
 | 
						||
               
 | 
						||
                Vector3[] worldcorners = new Vector3[4];
 | 
						||
                GetComponent<RectTransform>().GetWorldCorners(worldcorners);
 | 
						||
                Vector3 lefttop = RectTransformUtility.WorldToScreenPoint(null, worldcorners[1]);
 | 
						||
                Vector3 rightbottom = RectTransformUtility.WorldToScreenPoint(null, worldcorners[3]);
 | 
						||
                Vector3 picture_zero = RectTransformUtility.WorldToScreenPoint(null, 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;
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
    }
 | 
						||
} |