Log in or register to post comments

android webview plugin didn't work

August 29, 2017 - 1:41am #1

Hello Unity is a newbie.

I tried to make a plugin and it succeeded that I called the function of the class in Android studio.

============================================================

package sd.unityplugin;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.view.menu.MenuWrapperFactory;

import android.webkit.WebView;

/**

* Created by user on 2017-08-29.

*/

public class PluginClass extends Activity{

    public static WebView mWebview;

    @SuppressLint("setJavaScriptEnabled")@Override

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout);

        mWebview = (WebView) findViewById(R.id.webview);

        mWebview.getSettings().setJavaScriptEnabled((true));

    }

    public static void openWV(String url){

        mWebview.loadUrl(url);

    }

    public static String str(String str){

        return "AN_S+"+str;

    }



}

============================================================

============================================================

using System.Collections;

        using System.Collections.Generic;

        using UnityEngine;

public class PluginWrapper : MonoBehaviour {

private bool a;

        // Use this for initialization

        void Start () {

        var plugin = new AndroidJavaClass("sd.unityplugin.PluginClass");

        plugin.CallStatic<string>("onCreate");

        }

        // Update is called once per frame

        void Update () {

        }

private void OnGUI()

        {

        if(GUI.Button(new Rect(100, 100, 100, 100), ""))

        {

        TextMesh textMesh = GetComponent<TextMesh>();

        textMesh.text = "Unity Script";

        }

        if (GUI.Button(new Rect(100, 300, 100, 100), ""))

        {

        androidCall();

        }

        if (GUI.Button(new Rect(100, 500, 100, 100), ""))

        {

        androidCall2();

        }

        }

public void androidCall()

        {

        a = !a;

        var plugin = new AndroidJavaClass("sd.unityplugin.PluginClass");

        plugin.CallStatic<string>("openWV", "http://google.com/");

        }

public void androidCall2()

        {

        TextMesh textMesh = GetComponent<TextMesh>();

        var plugin = new AndroidJavaClass("sd.unityplugin.PluginClass");

        textMesh.text = plugin.CallStatic<string>("str", "sss");

        }

        }

============================================================

under is the code in Unity , a above is the code in Android studio.

I created a new module to access Unity from Android.

I built the newly created module and placed it inside the Unity project.

============================================================

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <WebView

        android:id="@+id/webview"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</LinearLayout>

============================================================

This is a new layout & code.

I built Unity with apk and confirmed that the String value is returning.

However, WebView is not created.

I do not know where it went wrong.

I hope you understanded even if my English is poor.

Answers I'll wait.

Thank you.

Log in or register to post comments