码迷,mamicode.com
首页 > 移动开发 > 详细

Android下重启Unity3d开发的游戏

时间:2015-08-01 12:55:18      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

前面项目中遇到一个问题,就是游戏更新完成之后重启游戏,按理说这个在android下面很简单没什么难度,可是自己对android不太熟悉,然后unity3d这里又有个坑,所以写这篇博客记录下来,希望能帮助到其它需要的人

首先在android下面我们一般会选择重启Activity,但是U3D的不unityplayeractivity 只要调用finish就会调用到他的退出消息,导致进程直接被关掉,从而无法重启,所以我后面加了一个闹钟解决了这个问题,也许对于android熟悉的人来说这个真不算什么问题先上代码!

package com.pixone.restartgame;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Restart {
    
    private static final String TAG = "restart";
    private static Restart _instance;
    private Class<?> _unityPlayerClass;
    private Field _unityPlayerActivityField;
    private Method _unitySendMessageMethod;
    public Activity _activity;
    private String gameObject;
    public static Restart getInstanceInActivity() {
        return _instance;
    }
    
    public static Restart getInstance(String gameObject) {
        if (_instance == null) {
            _instance = new Restart( gameObject);
        }
        return _instance;
    }
    
    private Restart(String gameObject) 
    {
        this.gameObject = gameObject;
        
        try {
            this._unityPlayerClass =  Class.forName("com.unity3d.player.UnityPlayer");
            
            this._unityPlayerActivityField = this._unityPlayerClass
                    .getField("currentActivity");
            this._unitySendMessageMethod = this._unityPlayerClass.getMethod(
                    "UnitySendMessage", new Class[] { String.class,
                            String.class, String.class });
            
        } catch (ClassNotFoundException e) {
            Log.i(TAG, "could not find UnityPlayer class: " + e.getMessage());
        } catch (NoSuchFieldException e) {
            Log.i(TAG,
                    "could not find currentActivity field: " + e.getMessage());
        } catch (NoSuchMethodException e) {
            Log.i(TAG, "could not find UnityPlayer class: " + e.getMessage());
        } catch (Exception e) {
            Log.i(TAG,
                    "unkown exception occurred locating getActivity(): "
                            + e.getMessage());
        }
        getActivity();
    }
    
    public Activity getActivity() {
        if(this._activity == null) {
            try {
                this._activity = (Activity) this._unityPlayerActivityField.get(this._unityPlayerClass);
            } catch (IllegalArgumentException e) {
                Log.i(TAG, "error getting currentActivity: " + e.getMessage());
            } catch (IllegalAccessException e) {
                Log.i(TAG, "error getting currentActivity: " + e.getMessage());
            }
        }
        
        return this._activity;
    }
    
    public void UnitySendMessage(String m, String p) {
//         UnityPlayer.UnitySendMessage(gameObject, m, p);

        if (this._unitySendMessageMethod != null) {
            try {
                
                this._unitySendMessageMethod.invoke(null, new Object[] {
                        gameObject, m, p });
            } catch (IllegalArgumentException e) {
                Log.i(TAG,
                        "could not find UnitySendMessage method: "
                                + e.getMessage());
            } catch (IllegalAccessException e) {
                Log.i(TAG,
                        "could not find UnitySendMessage method: "
                                + e.getMessage());
            } catch (InvocationTargetException e) {
                Log.i(TAG,
                        "could not find UnitySendMessage method: "
                                + e.getMessage());
            }
        } else {
            Log.i(TAG, "UnitySendMessage: " + gameObject + ", " + m + ", " + p);
        }
    }
    
    public  void restart() {
        Intent restartIntent = getActivity().getPackageManager()
                .getLaunchIntentForPackage(getActivity().getPackageName() );
        PendingIntent intent = PendingIntent.getActivity(
                getActivity(), 0,
                restartIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
        manager.set(AlarmManager.RTC, System.currentTimeMillis()+2000, intent);
        getActivity().finish();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    
    public  void restart(int delay) {
        if(delay <= 0 )
            delay = 10000;
        Intent restartIntent = getActivity().getPackageManager()
                .getLaunchIntentForPackage(getActivity().getPackageName() );
        PendingIntent intent = PendingIntent.getActivity(
                getActivity(), 0,
                restartIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
        AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
        manager.set(AlarmManager.RTC, System.currentTimeMillis()+delay, intent);
        getActivity().finish();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    
}

 

然后在Unity3d 里面调用是这样子的

using UnityEngine;
using System.Collections;
using System.IO;
public class Test : MonoBehaviour 
{
    private AndroidJavaObject jo;
    public void Start() 
    {
        if (jo == null)
        {
            AndroidJavaClass jc = new AndroidJavaClass("com.pixone.restartgame.Restart");
            jo = jc.CallStatic<AndroidJavaObject>("getInstance", gameObject.name);
        }
    }

    public void OnGUI() 
    {
        if (GUI.Button(new Rect(100, 100, 100, 100), "固定时间重启")) 
        {
            jo.Call("restart");
        }
        if (GUI.Button(new Rect(300, 100, 100, 100), "自定义时间重启"))
        {
            jo.Call("restart",1000);
        }

    }

}

 

到此就可以了

比如U3D里面怎么放插件啊这些都不用说了吧!网上一大堆的教程

 

Android下重启Unity3d开发的游戏

标签:

原文地址:http://www.cnblogs.com/MoveBricks/p/4693913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!