UGUI UniWebView適配

using System.Collections;
using System.Collections.Generic;
using NxFish;
using UnityEngine;
using UnityEngine.UI;

public class UniTest : MonoBehaviour {
    private UniWebView webView;
    public NxFish.Data.AccountData account;
    public RectTransform trans;
    public Button btn_back;
    private float viewWidth= 1920f;
    private float viewHeght= 1080f;

    float scaleWidth ;
    float scaleHeight;

    private void Awake() {
        //this.webView = Singleton<WebView>.Instance;
        if (webView == null) {
            webView = this.gameObject.AddComponent<UniWebView>();
        }
        scaleWidth = (Screen.width * 1f / viewWidth);
        scaleHeight = (Screen.height * 1f / viewHeght);
    }

    // Use this for initialization
    void Start () {
        btn_back.onClick.AddListener(()=> {
         
        });
        //動態修改panel大小
        UnityEngine.Debug.Log(trans.rect.top + ":" +trans.rect.bottom + ":" + trans.rect.left + ":" + trans.rect.right);
        //this.webView.Show(this.account.platformLink, trans.rect, () => { UnityEngine.Debug.Log("遊戲加載完成"); });
   
        float neweWidth = scaleWidth * trans.sizeDelta.x;
        float neweHeight = scaleHeight * trans.sizeDelta.y;

        //this.SetRectTransformSize(trans, new Vector2(neweWidth, neweHeight));
        this.WebViewInsets();
        this.webView.Load(this.account.platformLink);
        this.webView.Show(true, UniWebViewTransitionEdge.None, 0.3f);
    }

    public void SetRectTransformSize(RectTransform trans, Vector2 newSize) {
        Vector2 oldSize = trans.rect.size;
        Vector2 deltaSize = newSize - oldSize;
        trans.offsetMin = trans.offsetMin - new Vector2(deltaSize.x * trans.pivot.x, deltaSize.y * trans.pivot.y);
        trans.offsetMax = trans.offsetMax + new Vector2(deltaSize.x * (1f - trans.pivot.x), deltaSize.y * (1f - trans.pivot.y));
    }

    void WebViewInsets() {
        Vector3[] VectorArray=new Vector3[4];
        trans.GetWorldCorners(VectorArray);
        //Vector3 bottomLeft = Camera.main.WorldToScreenPoint(VectorArray[0]);
        //Vector3 topRight = Camera.main.WorldToScreenPoint(VectorArray[2]);
        float Left = VectorArray[0].x;
        float Top = Screen.height - VectorArray[2].y;
        float Bottom = VectorArray[0].y;
        float Right = Screen.width - VectorArray[2].x;
        //UnityEngine.Debug.Log(Left + ":" + Top + ":" + Bottom + ":" + Right);
        //this.webView.insets = new UniWebViewEdgeInsets(UniWebViewHelper.ConvertPixelToPoint(Top, false), UniWebViewHelper.ConvertPixelToPoint(Left, true), UniWebViewHelper.ConvertPixelToPoint(Bottom, false), UniWebViewHelper.ConvertPixelToPoint(Right, true));
        this.webView.insets = new UniWebViewEdgeInsets(70 * (int)scaleHeight, 0, 0, 0);
    }

}

Image是容器

附:

Android劉海屏手機適配正在測試中。。

iPhoneX可以單獨處理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章