Unity3D游戏版本更新怎么做
1个回答
展开全部
最近在学习android的知识,因为功能的需要,unity3d 要和android交互。最近公司的项目中遇到了unity3d 开发的app需要更新和自动安装的功能,但是使用unity3d是实现不了这样的功能的。这个时候就想到使用android原有的知识进行开发,然后打成jar包,导成插件,放到unity3d中去调用,想要找unity插件可到纳金论坛去找。
1.首先在eclipse或者android studio 中开发原有的app下载更新的功能。
*我这里是以eclipse为例开发的。
首先在eclipse中导入unity3d的jar包,class.jar,这里的class.jar在unity3d安装目录下,如下:D:\Unity5.2.3SetUp\Editor\Data\PlaybackEngines\androidplayer\Variations\il2cpp\Development\Classes
然后就要写相应的代码了,android的原有下载更新网上也有很多但是没有结合Untiy3d。具体的代码如下,首先要引入import com.unity3d.player.UnityPlayer,然后activity继承UnityPlayerActivity
package com.example.downapk;
import java.io.File;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends UnityPlayerActivity
{
private static String tag = "MainActivity";
private Handler mHandler = new Handler()
{
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
OpenFile();
break;
default:
break;
}
}
};
private AsyncTaskUtil mDownloadAsyncTask;
public float size;
public static MainActivity instance;
public static MainActivity getInstance()
{
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
public void SetSize(float size)
{
size = this.size;
Log.d(tag, this.size+"");
}
public void StartLoad(String httpUrl, String apkName)
{
mDownloadAsyncTask = new AsyncTaskUtil(MainActivity.this, mHandler);
mDownloadAsyncTask.execute(httpUrl, apkName);//必须传入两个参数——参数1:url;参数2:文件名(可以为null)
}
private void OpenFile() {
String str = "/test.apk";
String fileName = mDownloadAsyncTask.getFolderPath() + str;
File file = new File(fileName);
if (!file.exists()) {
Log.d("URL", "fileName:" + fileName);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
Log.d("URL", "fileName:" + fileName);
startActivity(intent);
}
}
上面代码是主activity 表黑部分是要在unity中调用的。调用可以是一个按钮,例如
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("StartLoad", ParentApp.getInstance().updateUrl, "test.apk");
这里是使用AsyncTask进行下载的,在后来可以看到下载进度。下面下载的代码。
package com.example.downapk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import com.unity3d.player.UnityPlayer;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
public class AsyncTaskUtil extends AsyncTask<String, Double, Boolean>
{
public static final String TAG = "AsyncTaskUtil";
public static final int NOTIFICATION_PROGRESS_UPDATE = 0x10;//用于更新下载进度的标志
public static final int NOTIFICATION_PROGRESS_SUCCEED = 0x11;//表示下载成功
public static final int NOTIFICATION_PROGRESS_FAILED = 0x12;//表示下载失败
//URL
private String mUrl;
//activity
private Context mContext;
//任务定时器
private Timer mTimer;
//定时任务
private TimerTask mTask;
//主线程传递过来的handler
private Handler mHandler;
//所要下载的文件大小
private long mFileSize;
//已下载的文件大小
private long mTotalReadSize;
//AsyncTaskRunnable实现了Runnable接口,用于更新下载进度的显示
private AsyncTaskRunnable mRunnable;
//构造方法
public AsyncTaskUtil(Context context, Handler handler) {
mContext = context;
mHandler = handler;
mTimer = new Timer();
mTask = new TimerTask() {//在run方法中执行定时的任务
@Override
public void run() {
//size表示下载进度的百分比
float size = (float) mTotalReadSize * 100 / (float) mFileSize;
//通过AsyncTaskRunnable的setDatas方法下载的进度和状态(更新中、失败、成功)
mRunnable.setDatas(NOTIFICATION_PROGRESS_UPDATE, size);
//更新进度
mHandler.post(mRunnable);
}
};
mRunnable = new AsyncTaskRunnable(mContext);
}
// 执行耗时操作,params[0]为url,params[1]为文件名(空则写入null)
@Override
protected Boolean doInBackground(String... params) {
//任务定时器一定要启动
mTimer.schedule(mTask, 0, 500);
try {
mUrl = params[0];
//建立链接
URLConnection connection = new URL(mUrl).openConnection();
//获取文件大小
mFileSize = connection.getContentLength();
Log.d(TAG, "the count of the url content length is : " + mFileSize);
//获得输入流
InputStream is = connection.getInputStream();
//先建立文件夹
File fold = new File(getFolderPath());
if (!fold.exists()) {
fold.mkdirs();
}
String fileName = "";
//判断文件名:用户自定义或由url获得
if(params[1] != null){
fileName = params[1];
} else{
fileName = getFileName(params[0]);
}
//文件输出流
FileOutputStream fos = new FileOutputStream(new File(getFolderPath()
+ fileName));
byte[] buff = new byte[1024];
int len;
while ((len = is.read(buff)) != -1) {
mTotalReadSize += len;
MainActivity.getInstance().size = (float) mTotalReadSize * 100 / (float) mFileSize;
DecimalFormat format = new DecimalFormat("0");
UnityPlayer.UnitySendMessage("UI Root","GetSize",format.format(MainActivity.getInstance().size)+"");
Log.d(TAG, "the count of the url content length is : " + MainActivity.getInstance().size);
fos.write(buff, 0, len);
}
fos.flush();
fos.close();
} catch (Exception e) {
//异常,下载失败
mRunnable.setDatas(NOTIFICATION_PROGRESS_FAILED, 0);
//发送显示下载失败
mHandler.post(mRunnable);
if(mTimer != null && mTask != null){
mTimer.cancel();
mTask.cancel();
}
e.printStackTrace();
return false;
}
//下载成功
mRunnable.setDatas(NOTIFICATION_PROGRESS_SUCCEED, 0);
mHandler.post(mRunnable);
if(mTimer != null && mTask != null){
mTimer.cancel();
mTask.cancel();
}
return true;
}
//由url获得文件名
private String getFileName(String string) {
return string.substring(string.lastIndexOf("/") + 1);
}
//下载文件夹路径
public String getFolderPath() {
return Environment.getExternalStorageDirectory().toString() + "/AsyncTaskDownload/";
}
// doInBackground方法之前调用,初始化UI
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// 在doInBackground方法之后调用
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Toast.makeText(mContext, "Download Completed ! ", Toast.LENGTH_SHORT).show();
mHandler.sendEmptyMessage(1);
} else {
Toast.makeText(mContext, "Download Failed ! ", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onProgressUpdate(Double... values) {
super.onProgressUpdate(values);
}
}
标红的地方是王unity中传的,我想要在unity中看到下载的进度,当时试了unity调用android都没有成功,后来想从android中往unity中传
UnityPlayer.UnitySendMessage("UI Root","GetSize",format.format(MainActivity.getInstance().size)+"");
UI Root 表示在untiy中有这样一个gameobject 在gameobject 上面挂着一个脚本,脚本中有一个方法名称为GetSize(),最后一个为要在unity中看到的进度。
在unity中的调用代码为
public void GetSize(string str)
{
mSize = str;
Debug.Log(mSize + "************************");
text.text = "下载进度: " + mSize + "%";
if (mSize == "100")
{
Application.Quit();
}
}
1.首先在eclipse或者android studio 中开发原有的app下载更新的功能。
*我这里是以eclipse为例开发的。
首先在eclipse中导入unity3d的jar包,class.jar,这里的class.jar在unity3d安装目录下,如下:D:\Unity5.2.3SetUp\Editor\Data\PlaybackEngines\androidplayer\Variations\il2cpp\Development\Classes
然后就要写相应的代码了,android的原有下载更新网上也有很多但是没有结合Untiy3d。具体的代码如下,首先要引入import com.unity3d.player.UnityPlayer,然后activity继承UnityPlayerActivity
package com.example.downapk;
import java.io.File;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends UnityPlayerActivity
{
private static String tag = "MainActivity";
private Handler mHandler = new Handler()
{
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
OpenFile();
break;
default:
break;
}
}
};
private AsyncTaskUtil mDownloadAsyncTask;
public float size;
public static MainActivity instance;
public static MainActivity getInstance()
{
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
public void SetSize(float size)
{
size = this.size;
Log.d(tag, this.size+"");
}
public void StartLoad(String httpUrl, String apkName)
{
mDownloadAsyncTask = new AsyncTaskUtil(MainActivity.this, mHandler);
mDownloadAsyncTask.execute(httpUrl, apkName);//必须传入两个参数——参数1:url;参数2:文件名(可以为null)
}
private void OpenFile() {
String str = "/test.apk";
String fileName = mDownloadAsyncTask.getFolderPath() + str;
File file = new File(fileName);
if (!file.exists()) {
Log.d("URL", "fileName:" + fileName);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
Log.d("URL", "fileName:" + fileName);
startActivity(intent);
}
}
上面代码是主activity 表黑部分是要在unity中调用的。调用可以是一个按钮,例如
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("StartLoad", ParentApp.getInstance().updateUrl, "test.apk");
这里是使用AsyncTask进行下载的,在后来可以看到下载进度。下面下载的代码。
package com.example.downapk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import com.unity3d.player.UnityPlayer;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;
public class AsyncTaskUtil extends AsyncTask<String, Double, Boolean>
{
public static final String TAG = "AsyncTaskUtil";
public static final int NOTIFICATION_PROGRESS_UPDATE = 0x10;//用于更新下载进度的标志
public static final int NOTIFICATION_PROGRESS_SUCCEED = 0x11;//表示下载成功
public static final int NOTIFICATION_PROGRESS_FAILED = 0x12;//表示下载失败
//URL
private String mUrl;
//activity
private Context mContext;
//任务定时器
private Timer mTimer;
//定时任务
private TimerTask mTask;
//主线程传递过来的handler
private Handler mHandler;
//所要下载的文件大小
private long mFileSize;
//已下载的文件大小
private long mTotalReadSize;
//AsyncTaskRunnable实现了Runnable接口,用于更新下载进度的显示
private AsyncTaskRunnable mRunnable;
//构造方法
public AsyncTaskUtil(Context context, Handler handler) {
mContext = context;
mHandler = handler;
mTimer = new Timer();
mTask = new TimerTask() {//在run方法中执行定时的任务
@Override
public void run() {
//size表示下载进度的百分比
float size = (float) mTotalReadSize * 100 / (float) mFileSize;
//通过AsyncTaskRunnable的setDatas方法下载的进度和状态(更新中、失败、成功)
mRunnable.setDatas(NOTIFICATION_PROGRESS_UPDATE, size);
//更新进度
mHandler.post(mRunnable);
}
};
mRunnable = new AsyncTaskRunnable(mContext);
}
// 执行耗时操作,params[0]为url,params[1]为文件名(空则写入null)
@Override
protected Boolean doInBackground(String... params) {
//任务定时器一定要启动
mTimer.schedule(mTask, 0, 500);
try {
mUrl = params[0];
//建立链接
URLConnection connection = new URL(mUrl).openConnection();
//获取文件大小
mFileSize = connection.getContentLength();
Log.d(TAG, "the count of the url content length is : " + mFileSize);
//获得输入流
InputStream is = connection.getInputStream();
//先建立文件夹
File fold = new File(getFolderPath());
if (!fold.exists()) {
fold.mkdirs();
}
String fileName = "";
//判断文件名:用户自定义或由url获得
if(params[1] != null){
fileName = params[1];
} else{
fileName = getFileName(params[0]);
}
//文件输出流
FileOutputStream fos = new FileOutputStream(new File(getFolderPath()
+ fileName));
byte[] buff = new byte[1024];
int len;
while ((len = is.read(buff)) != -1) {
mTotalReadSize += len;
MainActivity.getInstance().size = (float) mTotalReadSize * 100 / (float) mFileSize;
DecimalFormat format = new DecimalFormat("0");
UnityPlayer.UnitySendMessage("UI Root","GetSize",format.format(MainActivity.getInstance().size)+"");
Log.d(TAG, "the count of the url content length is : " + MainActivity.getInstance().size);
fos.write(buff, 0, len);
}
fos.flush();
fos.close();
} catch (Exception e) {
//异常,下载失败
mRunnable.setDatas(NOTIFICATION_PROGRESS_FAILED, 0);
//发送显示下载失败
mHandler.post(mRunnable);
if(mTimer != null && mTask != null){
mTimer.cancel();
mTask.cancel();
}
e.printStackTrace();
return false;
}
//下载成功
mRunnable.setDatas(NOTIFICATION_PROGRESS_SUCCEED, 0);
mHandler.post(mRunnable);
if(mTimer != null && mTask != null){
mTimer.cancel();
mTask.cancel();
}
return true;
}
//由url获得文件名
private String getFileName(String string) {
return string.substring(string.lastIndexOf("/") + 1);
}
//下载文件夹路径
public String getFolderPath() {
return Environment.getExternalStorageDirectory().toString() + "/AsyncTaskDownload/";
}
// doInBackground方法之前调用,初始化UI
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// 在doInBackground方法之后调用
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Toast.makeText(mContext, "Download Completed ! ", Toast.LENGTH_SHORT).show();
mHandler.sendEmptyMessage(1);
} else {
Toast.makeText(mContext, "Download Failed ! ", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onProgressUpdate(Double... values) {
super.onProgressUpdate(values);
}
}
标红的地方是王unity中传的,我想要在unity中看到下载的进度,当时试了unity调用android都没有成功,后来想从android中往unity中传
UnityPlayer.UnitySendMessage("UI Root","GetSize",format.format(MainActivity.getInstance().size)+"");
UI Root 表示在untiy中有这样一个gameobject 在gameobject 上面挂着一个脚本,脚本中有一个方法名称为GetSize(),最后一个为要在unity中看到的进度。
在unity中的调用代码为
public void GetSize(string str)
{
mSize = str;
Debug.Log(mSize + "************************");
text.text = "下载进度: " + mSize + "%";
if (mSize == "100")
{
Application.Quit();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询