Android under restart Unity3d development game

Source: Internet
Author: User

The previous project encountered a problem, that is, the game update after the game restart, supposedly this under Android is very simple no difficulty, but they are not very familiar with Android, and then Unity3d here there is a pit, so write this blog record down, hoping to help other people in need

First of all under Android we will generally choose to restart activity, but u3d not unityplayeractivity as long as the call to finish will be called to his exit message, causing the process to be switched off directly, so as not to restart, So I added an alarm clock to solve this problem, perhaps for Android familiar with the people that this really is not a problem first on the code!

 PackageCom.pixone.restartgame;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;Importandroid.app.Activity;ImportAndroid.app.AlarmManager;Importandroid.app.PendingIntent;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classRestart {Private Static FinalString TAG = "Restart"; Private StaticRestart _instance; PrivateClass<?>_unityplayerclass; PrivateField _unityplayeractivityfield; PrivateMethod _unitysendmessagemethod;  PublicActivity _activity; PrivateString Gameobject;  Public StaticRestart getinstanceinactivity () {return_instance; }         Public StaticRestart getinstance (String gameobject) {if(_instance = =NULL) {_instance=NewRestart (Gameobject); }        return_instance; }        PrivateRestart (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",NewClass[] {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 (); }         PublicActivity 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 voidunitysendmessage (String m, string p) {//unityplayer.unitysendmessage (Gameobject, M, p);        if( This. _unitysendmessagemethod! =NULL) {            Try {                                 This. _unitysendmessagemethod.invoke (NULL,Newobject[] {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  voidRestart () {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  voidRestartintdelay) {        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 ()); }    }

And then call it in Unity3d.

usingUnityengine;usingSystem.Collections;usingSystem.IO; Public classTest:monobehaviour {PrivateAndroidjavaobject Jo;  Public voidStart () {if(Jo = =NULL) {Androidjavaclass JC=NewAndroidjavaclass ("Com.pixone.restartgame.Restart"); Jo= JC. Callstatic<androidjavaobject> ("getinstance", Gameobject.name); }    }     Public voidOngui () {if(GUI. Button (NewRect ( -, -, -, -),"fixed-time restart") {Jo. Call ("Restart"); }        if(GUI. Button (NewRect ( -, -, -, -),"Custom Time restart") {Jo. Call ("Restart", +); }    }}

We can do that.

such as u3d inside how to put the plug-in AH these are needless to say! A whole bunch of tutorials online

Android under restart Unity3d development game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.