unity調用android佈局

 上一篇說了android動態加載佈局,這裏說說unity調用android

public void videoTest() {
        AdType = 4;
        final Activity curActivity = getDiActivity();

        (new Handler(curActivity.getMainLooper())).post(new Runnable() {//只要涉及到unity UI,包括更新都需要這一句。否則出錯
            @SuppressLint("ResourceType")
            public void run() {
                LayoutInflater inflater = LayoutInflater.from(curActivity);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, 
               LinearLayout.LayoutParams.MATCH_PARENT);
                lp.gravity = Gravity.CENTER;
                View view = inflater.inflate(R.layout.unityvideo2, null);

                videoview = view.findViewById(R.id.videoView);
                mmid_layout = view.findViewById(R.id.mid_layout);
                mid_ico_img = view.findViewById(R.id.mid_ico_img);
                mmid_click_btn = view.findViewById(R.id.mid_click_btn);
                mmid_title_txt = view.findViewById(R.id.mid_title_txt);
                mmid_title_content_txt = view.findViewById(R.id.mid_title_content_txt);
                mlayout_skip = view.findViewById(R.id.layout_skip);
                mtv_time = view.findViewById(R.id.tv_time);
                mtv_time.setText("關閉");


             //這裏就是將動態佈局加入到unity中
                totalViewAd = (ViewGroup) curActivity.getWindow().getDecorView();
                totalViewAd.addView(view, lp);
}

android完整類:

package com.u3d.com.unityAndroid;



/**
 * Created by Admin on 2020/4/9.
 */

public class Unity1Android {
    public static Activity activity = null;
  ViewGroup totalViewAd;
    private static final String[] REQUIRED_PERMISSIONS = new String[]{
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.READ_PHONE_STATE",
            "android.permission.INTERNET",
            "android.permission.VIBRATE"
    };



    //視頻部分
    LinearLayout mmid_layout, mlayout_skip;
    TextView mmid_title_txt, mmid_title_content_txt;
    Button mmid_click_btn;
    ImageView mid_ico_img;
    VideoView videoview;
    TextView mtv_time;
    Context context;
  videoTracerMonitorsBean;

    public static Time time;
    public long startTime;
    Bitmap bmp;
    JSONArray jsonObject_video_tracer_monitors;
    String JSON;//json

   
    /**
     * 調用Unity的方法
     *
     * @param gameObjectName 調用的GameObject的名稱
     * @param functionName   方法名
     * @param args           參數
     * @return 調用是否成功
     */
    boolean callUnity(String gameObjectName, String functionName, String args) {
        try {
            Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
            Method method = classtype.getMethod("UnitySendMessage", String.class, String.class, String.class);
            method.invoke(classtype, gameObjectName, functionName, args);
            return true;
        } catch (ClassNotFoundException e) {

        } catch (NoSuchMethodException e) {

        } catch (IllegalAccessException e) {

        } catch (InvocationTargetException e) {

        }
        return false;
    }

   

    public void videoTest() {
        AdType = 4;
        final Activity curActivity = getDiActivity();

        (new Handler(curActivity.getMainLooper())).post(new Runnable() {
            @SuppressLint("ResourceType")
            public void run() {
                LayoutInflater inflater = LayoutInflater.from(curActivity);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                lp.gravity = Gravity.CENTER;
                View view = inflater.inflate(R.layout.unityvideo2, null);

                videoview = view.findViewById(R.id.videoView);
                mmid_layout = view.findViewById(R.id.mid_layout);
                mid_ico_img = view.findViewById(R.id.mid_ico_img);
                mmid_click_btn = view.findViewById(R.id.mid_click_btn);
                mmid_title_txt = view.findViewById(R.id.mid_title_txt);
                mmid_title_content_txt = view.findViewById(R.id.mid_title_content_txt);
                mlayout_skip = view.findViewById(R.id.layout_skip);
                mtv_time = view.findViewById(R.id.tv_time);
                mtv_time.setText("關閉");
//                mmid_click_btn.setOnClickListener(this);
//                mtv_time.setOnClickListener(this);
//                videoview.setVisibility(View.GONE);
//                mtv_time.setVisibility(View.GONE);


                totalViewAd = (ViewGroup) curActivity.getWindow().getDecorView();
                totalViewAd.addView(view, lp);

                videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mediaPlayer) {
                        Log.i("tag", "準備完畢");
                        mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                            @Override
                            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                                if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                                    // video 視屏播放的時候把背景設置爲透明
                                    videoview.setBackgroundColor(Color.TRANSPARENT);
                                    return true;
                                }
                                return false;
                            }
                        });
                    }
                });

                videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        Log.i("tag", "播放結束");
//                videocallback.DlgAdvEventEnd(true);
                    }
                });
                videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        Log.i("tag", "播放出錯");
                        return false;
                    }
                });

            }
        });

       
    }

    
}

這裏是unity部分:

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

public class aarTest : MonoBehaviour
{
    // Start is called before the first frame update
    AndroidJavaObject m_java;
    public Text text;
    private void Awake()
    {
        m_java = new AndroidJavaObject("com.u3d.com.unityAndroid.Views.Unity1Android");
    }
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ArrTest5()
    {
        m_java = new AndroidJavaObject("com.u3d.com.unityAndroid.Views.Unity1Android");
        text.text = "開始調用方法";
        m_java.Call("videoTest");
        text.text += "\n 調用結束";
    }


}

下一篇說說android調用unity及android與unity接口回調通信

 

                                                                                                                                                                                             -END

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