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