Android interacts with fragment, fragment displays Unity3d view.

Source: Internet
Author: User

These two days just contact Unity3d, before has been to do Android development, for the development of Unity3d have specialized talent, I mainly involved in Unity3d with Android interaction, after two days is the experiment finally completed the following effect:Originally wanted to write a few blog, to detail the next Unity3d export Android project and then do two development process, later found a few blog, process and my similar, for the repetition of the process I will not write, we refer to his blog on it. And, including the Unity3d call Android script, this piece I haven't touched yet. Unity3d Game Development Unity and Android Interactive call research
Unity3d Game Development Embed Unity view in Android view
Unity3d game development from Unity3d to eclipse
In this, the road a Bo master hard, for the above three blog after you have finished reading, you must be able to export Android project from Unity3d, and then pour into eclipse. We can see that mainactivity is not inherited from Nativeactivity is integrated from unityplayeractivity (or unityplayernativeactivity), which gives the novice an illusion, is to show Unity3d view, it must be integrated from these several interfaces, then this idea, is wrong, specific people can refer to this blog: http://forum.unity3d.com/threads/ using-unity-android-in-a-sub-view.98315/
in this post, the U3d view is presented using a class that inherits from activity. in this case, there may be a problem before, if only inherit from those several classes, then the SUPPORTV4 package of fragment is not useless, we all know that Android.app.Fragment only supports more than 2.3 versions, while V4 can support up to 1.6, so it is generally recommended that you use the fragment in the V4 package. Well, to finish this question, we will look at the effect above, if the Android native application, this is a very common and very simple application, we can constantly switch the middle of the fragment.So instead of fragment to show u3d, and switch between them, how should you write code? Let's look at the following two blocks of code.
/** * Main interface * @author Gavin * */public class Mainactivity extends Fragmentactivity implements Onclicklistener {private Butto n btn1;private Button btn2;private fragmentmanager FM; View playerview;private Fragment currentfragment;private u3dfragment u3dfragment = new U3dfragment ();p rivate Menuonefragment menuonefragment = new Menuonefragment ();p rivate menutwofragment menutwofragment = new MenuTwoFragment () ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Constants.munityplayer = new Unityplayer (this), int glesmode = Constants.mUnityPlayer.getSettings (). GetInt ("Gles_mode ", 1); Boolean trueColor8888 = false; Constants.mUnityPlayer.init (Glesmode, trueColor8888); fm = Getsupportfragmentmanager (); Setcontentview ( R.layout.activity_main); Initview (); currentfragment = U3dfragment;changefragment (currentfragment);} /** * Initialize control */private void Initview () {btn1 = (button) Findviewbyid (R.ID.BTN1); btn2 = (Button) Findviewbyid (r.id.btN2); Btn1.setonclicklistener (this), Btn2.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.id.btn1:toast.maketext (Mainactivity.this, "btn1", Toast.length_short). Show (); if (currentfragment instanceof u3dfragment | | currentfragment instanceof menutwofragment) {currentfragment = Menuonefragment;changefragment (currentfragment);} Else{currentfragment = U3dfragment;changefragment (currentfragment);} Break;case R.id.btn2:toast.maketext (mainactivity.this, "btn2", Toast.length_short). Show (); if (currentfragment instanceof U3dfragment | | Currentfragment instanceof menuonefragment) {currentfragment = Menutwofragment;changefragment (currentFragment);} Else{currentfragment = U3dfragment;changefragment (currentfragment);} Break;}} The/** * method is used to change the interface */private void Changefragment (Fragment Fragment) {fragmenttransaction ft = fm.begintransaction (); Ft.replace (r.id.content, fragment); Ft.commit ();} protected void OnDestroy () {Constants.mUnityPlayer.quit (); Super.ondestroy ();} OnPause ()/onresume () must be-sent to Unityplayer-enable pause and resource recreation on resume.protected void Onpau Se () {super.onpause (); Constants.mUnityPlayer.pause ();} protected void Onresume () {super.onresume (); Constants.mUnityPlayer.resume ();} public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); Constants.mUnityPlayer.configurationChanged (newconfig);} public void Onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus); Constants.mUnityPlayer.windowFocusChanged (Hasfocus);} public boolean dispatchkeyevent (KeyEvent event) {if (event.getaction () = = Keyevent.action_multiple) return Constants.mUnityPlayer.onKeyMultiple (Event.getkeycode (), Event.getrepeatcount (), event), return Super.dispatchkeyevent (event);}}
The above is the writing of activity.
/** * Display U3D interface * @author Gavin * */public class U3dfragment extends Fragment {private Activity context; View Playerview; @Overridepublic void Onattach (activity activity) {//TODO auto-generated method Stubsuper.onattach ( activity); context = activity;} @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedinstancestate) {//TODO auto-generated Method Stubplayerview = Constants.mUnityPlayer.getView (); Layoutparams LP = new Layoutparams (layoutparams.fill_parent, layoutparams.fill_parent);p Layerview.setlayoutparams ( LP); if (playerview.getparent () = null) {((ViewGroup) playerview.getparent ()). Removeallviews (); return Playerview;} @Overridepublic void OnDestroy () {//TODO auto-generated method Stub//constants.munityplayer.quit (); Super.ondestroy () ;} @Overridepublic void OnPause () {//TODO auto-generated method Stubsuper.OnPause (); Constants.mUnityPlayer.pause ();} @Overridepublic void Onresume () {//TODO auto-generated method Stubsuper.onresume (); Constants.mUnityPlayer.resume ();}}
Above is the notation of the fragment that shows the U3d view. Note that there are the following points: 1. Unityplayer This object is defined for static variables, initialized in Mainactivity, and used in fragment to obtain the Unity3d view. 2. In U3dfragment, the Quit () method is not called in OnDestroy. 3. In the Oncreateview () method in U3dfragment, the following processing is performed.
((ViewGroup) playerview.getparent ()). Removeallviews ();
For the above three questions, the following three questions will be raised if not handled accordingly. 1. The fragment of the U3d view cannot display the animated effect of Unity3d. 2. The following error will be reported when switching the page: 10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativevideoframecallback:vilii
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesoftinputclosed:v
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesettouchdeltay:vf
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesetinputstring:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesetinputcanceled:vz
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesetextras:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesetdefaultdisplay:vi
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativeresume:v
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativeresize:viiii
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativerequestedaa:i
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativerequested32bitdisplaybuffer:z
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativerender:z
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativerecreategfxstate:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativepause:z
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativekeyspressed:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativeinjectevent:zl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativeinitwww:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativefocuschanged:vz
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativefile:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativedone:v
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativeactivityindicatorstyle:i
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Initjni:vl
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Unitysendmessage:vlll
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesetlocationstatus:vi
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativesetlocation:vffffdf
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativeforwardeventstodalvik:vz
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lcom/unity3d/player/unityplayer;. Nativedeviceorientation:vi
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lorg/fmod/fmodaudiodevice;. Fmodunblockstreaming:i
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lorg/fmod/fmodaudiodevice;. Fmodprocess:il
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lorg/fmod/fmodaudiodevice;. Fmodinitjni:i
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lorg/fmod/fmodaudiodevice;. Fmodgetinfo:ii
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lorg/fmod/fmodaudiodevice;. Fmodblockstreaming:i
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method Lorg/fmod/fmodaudiodevice;. Fmodprocessmicdata:ili
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method lcom/unity3d/player/reflectionhelper;. Nativeproxyinvoke:lill
10-16 13:26:44.927:D/DALVIKVM (8170): Unregistering JNI method lcom/unity3d/player/reflectionhelper;. Nativeproxyfinalize:vi3. The following error is reported:caused by:java.lang.IllegalStateException:The specified child already have a parent. You must call Removeview () on the child ' s parent first.
Now, the function is realized, but there are many things that can be improved and not understood, so we can discuss it together.


Android interacts with fragment, fragment displays Unity3d view.

Related Article

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.