C#/수업내용

2020.07.31. 수업내용 - WebView

dev_sr 2020. 7. 31. 14:53
 

UniWebView | Getting Started

Getting Started What is UniWebView UniWebView is a Unity 3D plugin for adding a web view component to your Unity 3D mobile games. UniWebView includes a set of abstract high-level APIs in C#, which wrapped the native APIs of iOS and Android platforms. With

docs.uniwebview.com

 

webview
웹페이지를 렌더링해서 보여주는 기능
앱 안에 브라우저가 열리는 것

 

webview prefab


webview url 은 http 로 시작되는 것만 가능 (http, https 둘다 됨!)
show on start : 시작할 때 자동으로 보여줄 지 여부

full screen : 풀스크린 여부

 

★한 페이지에 한 웹뷰만 나타날 수 있음
다른 페이지를 보여주려면 새로 만들어야함

 

 

docs.uniwebview.com/game.html

 

https://docs.uniwebview.com/game.html

ACCELERATE Close Page Use the ACCELERATE button to stay in the air How long can you stay alive?

docs.uniwebview.com

여기 페이지에서 페이지 소스를 보면 자바스크립트가 나오는데 페이지를 구성하는 function을 유니티 안에서도

전달받아 사용할 수 있다.

 

★webview를 닫게 되면 모든 구성요소가 파괴된다.

 

document를 보고 튜토리얼을 따라하면 웹뷰로 게임을 할 수 있다.

 

 

------------------------------------------------------------------------------------------------------

 

버튼을 누르면 webview 로 naver가 나오게 하기

 

씬구성을 이렇게 해준다.

 

 

main에서 버튼을 누르면 webview 팝업이 뜨면서 네이버에 접속하도록 url을 보내줌

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

public class UIMain : MonoBehaviour
{
    public Button btnOpenWebViewPopUp;
    public UIPopUp uiPopUp;
    
    void Start()
    {
        this.uiPopUp.btnClose.onClick.AddListener(() =>
        {
            this.uiPopUp.Close();
        });

        this.btnOpenWebViewPopUp.onClick.AddListener(() =>
        {
            this.uiPopUp.Open("http://www.naver.com");
            Debug.Log("Open");
        });
    }

}

 

 

먼저 오브젝트를 활성화하고 (활성화부터 안하면 웹페이지가 안뜸)

프레임 안에서 네이버 페이지가 뜨게 함.

닫기 버튼을 누르면 webview를 null로 만들고 파괴한 뒤 팝업을 비활성화함.

using UnityEngine;
using UnityEngine.UI;

public class UIPopUp : MonoBehaviour
{
    private UniWebView webView;
    public RectTransform frame;
    public Button btnClose;

    public void Init()
    {

    }

    public void Open(string url)
    {
        this.gameObject.SetActive(true);
        //uniWebview 인스턴스 생성
        this.CreateNewWebView(url, () =>
          {
              Debug.Log("CreateNewWebView");
          });
    }

    public void Close()
    {
        if (this.webView != null)
        {
            Destroy(this.webView.gameObject);
            this.webView = null;
            this.gameObject.SetActive(false);
        }


    }

    private void CreateNewWebView(string loadurl, System.Action onPageFinished)
    {
        var go = new GameObject("UniWebView");
        this.webView = go.AddComponent<UniWebView>();

        Debug.Log(loadurl);

        this.webView.Load(loadurl);
        this.webView.ReferenceRectTransform = this.frame;
        this.webView.Show();

        //add events
        this.webView.OnPageFinished += (view, StatusCode, url) =>
         {
             Debug.Log("onPageFinished");
             onPageFinished();
         };

        this.webView.OnShouldClose += (view) =>
        {
            this.Close();
            return true;
        };

    }
}