思路是用Android studio将java代码导出为jar或是aar,然后在unity端调用Java代码。
我采用的方式是导出aar。
安卓端Java代码如下:
package com.example.mylibrary2;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.print.PrintHelper;
public class Test2 {
public int Add(int i,int j){
return i+j;
}
private static Activity unityActivity;
public static void ReceiveunityActivity(Activity tActivity){
unityActivity = tActivity;
}
public void Toast(String msg){
}
private Activity _unityActivity;
Activity getActivity(){
if(null == _unityActivity) {
try {
Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
_unityActivity = activity;
} catch (ClassNotFoundException e) {
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException e) {
}
}
return _unityActivity;
}
public void Printer(String path)
{
//创建一个PrintHelper帮助类
PrintHelper ph=new PrintHelper(getActivity());
//调整缩放模式
ph.setScaleMode(PrintHelper.SCALE_MODE_FILL);
//读取图片文件
Bitmap bitmap=BitmapFactory.decodeFile(path);
//调用系统打印功能
ph.printBitmap("bitmap", bitmap);
}
}
需要记住包名,也就package的引用部分,这个名称需要在unity中使用。
BitmapFactory创建位图的方式有三种,一种是直接读取安卓图片资源,一种是采用路径读取,还有一种是读取字节数组,我这里采用的是第二种。
导出时的步骤如下,第二张图中两个按钮都可以导出aar
接下来是在Unity中调用安卓代码
void Initialized(string name)
{
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
_ajc = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
_pluginInstance = new AndroidJavaObject(name);
if (_pluginInstance == null)
{
Debug.Log("加载错误");
}
else
{
Debug.Log("加载成功");
//_pluginInstance.CallStatic("ReceiveunityActivity", _ajc);
}
Debug.Log("初始化成功");
//_pluginInstance.Call("Printer", _ajc);
}
上面这段代码主要是用来加载aar包,遇到变量类型不清楚的,前面可以加var。
IEnumerator ScreenShoot()
{
//图片大小
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
tex.Apply();
yield return tex;
byte[] byt = tex.EncodeToPNG();
//安卓平台才调用安卓的活动刷新相册
if (_pluginInstance != null)
{
Debug.Log("_pluginInstance不为空");
_pluginInstance.Call("Printer", byt);
}
else
{
Debug.Log("_pluginInstance为空");
}
}
private string path;
/// <summary>
/// 保存按钮点击事件
/// </summary>
public void OnScreenShootClick()
{
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].SetActive(false);
}
StartCoroutine(JieTu());
}
//获取图片的保存路径
public string ScreenshotPath()
{
string filePath = "";
if (Application.platform == RuntimePlatform.Android)
{
filePath = "/storage/emulated/0/Pictures/pictest/";
}
if (!Directory.Exists(filePath))
{
try
{
Directory.CreateDirectory(filePath);
}
catch (PathTooLongException)
{
Debug.Log("路径或者文件名超过系统定义的最大长度。");
//txt.text += "路径或者文件名超过系统定义的最大长度。";
}
catch (DirectoryNotFoundException)
{
//txt.text += "保存目录没有找到。";
Debug.Log("保存目录没有找到。");
}
catch (UnauthorizedAccessException)
{
//txt.text += "创建目录失败,因为没有足够的权限。";
Debug.Log("创建目录失败,因为没有足够的权限。");
}
}
return filePath;
}
//截图保存之后刷新相册
IEnumerator JieTu()
{
//图片大小
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
tex.Apply();
yield return tex;
byte[] byt = tex.EncodeToPNG();
//显示返回按钮
for (int i = 0; i < buttons.Length; i++)
{
buttons[i].SetActive(true);
}
DateTime currentTime = DateTime.Now;
string fileName = "1.png";
path = ScreenshotPath() + fileName;
imgpsth = path;
File.WriteAllBytes(path, byt);
string[] paths = { path };
//安卓平台才调用安卓的活动刷新相册
if (Application.platform == RuntimePlatform.Android)
{
_pluginInstance.Call("Printer", ScreenshotPath()+"1.png");
ScanFile(paths);
}
}
string imgpsth;
//刷新图片,显示到相册中
void ScanFile(string[] path)
{
using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
{
Conn.CallStatic("scanFile", playerActivity, path, null, null);
}
}
}
这段代码主要是将图片保存在本地,然后调用安卓打印图片,之所以用固定图片名称,是为了方便读取。
源码地址在下面贴出来了,有条件的小伙伴还是支持一下哈,研究不易,实在没积分的,可以私信我。