公司让搞一套SDK 小白一个没有接过原生的SDK只能从头写起,看了很多文章 加上自己总结写出这个文章 问题一 UnityPlayerActivity找不到! 路径 Unity\Hub\Editor\2020.3.15f2c1\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player 这边建议直接目录拷贝过去
Android Studio
创建工程,记住包名
名称随便 包名最好和项目名称保持一致
Android Studio中的配置和代码示例
首先导入classes.jar 路径unity\Hub\Editor\2020.3.15f2c1\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes
ctrl+c,然后在AS Libs文件夹中ctrl+v,然后再点击Add as library 插件unity的Classes.jar包中是没有这个类存在的,这个时候我们需要在Untiy中找到UnityPlayerActivity,例如我的路径 路径 Unity\Hub\Editor\2020.3.15f2c1\Editor\Data\PlaybackEngines\AndroidPlayer\Source\com\unity3d\player 直接把这个包拷贝过来
然后修改build.gradle 然后我们在MainActivity 中写代码
package com.test.unityandroidtest;
import android.os.Bundle;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class MainActivity extends UnityPlayerActivity {
public static MainActivity instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance=this;
}
public void SayHi(final String str)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}
public int GetSum(int num1,int num2)
{
return num1+num2;
}
public static void SendUnity(String str)
{
String newstr="我是Android传来的消息:"+str;
UnityPlayer.UnitySendMessage("UnityAndroidConnect","AndroidCallUnityMsg",newstr);
}
}
然后修改AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.unityandroidtest">
<application>
<!--表明这个Activity是启动Activity 路径-->
<activity android:name="com.lj.unityandroid.UnityTestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>
android:属性大全 然后在AS中,标题任务栏 Build->Make Project. 一般的我们只要一个aar可以了
用压缩包打开aar包,删除里面libs里面的classes文件和res文件夹 打开classees.jar文件
然后删除unity3d和删除test文件中的buildConfig.class
注:在unity打包的时候,如果遇到
这种D8:Program type already presen就表明资源重复了,上述两部都是 在删除多余的文件
Unity中
导入aar和xml
接着在unity中 新建一个gameobject,命名为UnityAndroidConnect,然后新添加一个脚本,写入代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
public Button btnToast, btnNum, btnString;
public Text text;
AndroidJavaClass jc;
AndroidJavaObject androidInstance;
private void Start()
{
try
{
jc = new AndroidJavaClass("com.test.unityandroidtest.MainActivity");
androidInstance = jc.GetStatic<AndroidJavaObject>("instance");
}
catch (Exception e)
{
text.text = "初始化错误:" + e.ToString();
}
btnToast.onClick.AddListener(MakeToast);
btnNum.onClick.AddListener(GetSum);
btnString.onClick.AddListener(UnitySendAndroid);
}
public void MakeToast()
{
try
{
androidInstance.Call("SayHi", "hello unity");
}
catch (Exception e)
{
text.text = e.ToString();
}
}
public void GetSum()
{
try
{
int num = 90;
int num2 = 90;
num = androidInstance.Call<int>("GetSum", num, num2);
text.text = "计算结果:" + num.ToString();
}
catch (Exception e)
{
text.text = e.ToString();
}
}
public void UnitySendAndroid()
{
try
{
string str = "loveyou";
jc.CallStatic("SendUnity", str);
}
catch (Exception e)
{
text.text = e.ToString();
}
}
public void AndroidCallUnityMsg(string recvMsg)
{
text.text = "结果:" + recvMsg;
}
}
测试成功 有啥问题大家一起探究
|