赞
踩
相关问题:
关屏幕再开屏幕导致重启问题https://blog.csdn.net/SnoopyNa2Co3/article/details/86215782
Sdk接入一定涉及到安卓既接入和通信,虽然现在很多sdk只要打一下母包就可以出n个渠道的包,不过母包还是要接入安卓
首先我们先确定一下unity里面的包名
File->New->Android application project
然后名字和unity一一对应,注意大小写问题
然后导入unity对应的Classes.Jar,最后点OK完成
把Activity改成UnityPlayerActivity
这里要导入import com.unity3d.player.UnityPlayerActivity;
一般闪退都是安卓包名和unity设置的包名不一致导致的
打包安卓报错,把buildSystem改成Internal,如下图
- AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
- jo.Call("CallUnity");
- jo.Call("CallUnity1","abc");
关键代码
AndroidJavaObject.Call("方法名",参数(可不填))
- public void CallUnity()
- {
- UnityPlayer.UnitySendMessage("TestGameObj", "Test1", "1");
- }
-
- public void CallUnity1(String name)
- {
- UnityPlayer.UnitySendMessage("TestGameObj", "Test2", name);
- }
TestGameObj是场景一个gameobject的名字
Test1是TestGameObj所挂脚本上面的一个方法
“1”是发给unity参数
unity代码
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class test : MonoBehaviour
- {
- public Text text;
- public Text text2;
-
- // Use this for initialization
- void Start ()
- {
- AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
- jo.Call("CallUnity");
- jo.Call("CallUnity1","abc");
- }
-
-
- public void Test1(string name)
- {
- text.text = name;
- }
-
- public void Test2(string name)
- {
- text2.text = name;
- }
- }
安卓代码
- package com.Snoopy.AndroidTest;
-
- import com.unity3d.player.UnityPlayer;
- import com.unity3d.player.UnityPlayerActivity;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
-
- public class MainActivity extends UnityPlayerActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
-
- public void CallUnity()
- {
- UnityPlayer.UnitySendMessage("TestGameObj", "Test1", "1");
- }
-
- public void CallUnity1(String name)
- {
- UnityPlayer.UnitySendMessage("TestGameObj", "Test2", name);
- }
- }
测试场景,创建空Gameobject命名TestGameObj,因为上面的安卓代码是给这个TestGameObj发消息所以命名这个,添加test组件,TestGameObj所挂的脚本有Test1和Test2方法就行,把对应的ui拖上去打包安卓包测试
测试结果
教程就到这里
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。