Unity calls activity and layout

Unity calls the activity and its layout method
If I just call the methods in android and don't involve the layout, I only need to export the related Jar package, but if I want to call the android activity and related layout, it is not enough to export the Jar. If we want to mix the android layout and the unity layout, imagine a requirement that the user needs to render images in Unity, and the image list and selection need to be done in android, and then pass the selected path to unity. Explain according to this requirement: the premise is to copy the android class under unity to android according to the conventional jar method.

1. First complete the Unity scene ChoiceScene, this scene adds two buttons, video selection, picture selection.
2. Whenever these two buttons are clicked, the method in android is called:
   public static void StartPanoramaActivity(Context context)
	    {
	    	Intent intent = new Intent(context, PanoramaImageListActivity.class);
	    	context.startActivity(intent);
	    }
	 
	 public static  void StartLocalVideoActivity(Context context)
	    {
	    	Intent intent = new Intent(context, FullViewVideoListActivity.class);
	    	context.startActivity(intent);	
	    }
Through the code, we can see that this is to start two activities under android. The above two methods are placed under the static class CommonUtil and called by unity:

public static void StartPanoramaActivity() {
      
         AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
         AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

        AndroidJavaClass CommonUtil = new AndroidJavaClass("com.goertek.play360.CommonUtil");
         CommonUtil.CallStatic("StartPanoramaActivity", jo);

        
    }

    public static void StartLocalVideoActivity() {

        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

        AndroidJavaClass CommonUtil = new AndroidJavaClass("com.goertek.play360.CommonUtil");
        CommonUtil.CallStatic("StartLocalVideoActivity", jo);

    }
This is no different from a normal jar package, but if you only call it through a jar package, the program crashes when you click the button, because listview=(ListView) findViewById(R.id.list);
		listview.setAdapter(mImageListAdapter);
At this time, it is said that the object is empty. If only the android program is run, it is completely normal. At this time, I copied res into unity, and the problem was solved, but I couldn't find R.layout. At this time, I selected both res and gen when generating the jar package, and an error was reported. Or I directly import bin into jar, because res and class are included under bin, but it has not been solved yet. That is to say, export the jar through export, and choose different options or select all when exporting.

3. The method I have tested and passed is: tick IsLibrary in the android attribute of the program, then clean (I have not tested it without clean, this is my habit), and then run, at this time, a jar will be generated under the bin, I I think this method is better than our own guide, after all, it is done by the system, and then put the gen, res, and jar packages into unity android at the same time. Test passed.
4. It is best to replace the new gen every time you generate a new jar. I have not tested the situation without changing it. If you only add methods, but do not add or delete layouts.
5. There is also a problem found in manifest.xml under unity:
    <activity android:name="com.google.unity.GoogleUnityActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="landscape"
                  android:launchMode="singleTask"
                  android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="com.google.intent.category.CARDBOARD" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>

Android manifest:
<activity
            android:name="com.goertek.play360.MainActivity"
             android:screenOrientation="portrait"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  
MainActivity also inherits UnityActivity. I did not copy the xml under android to unity, but only added the two activities that unity needs to start to the xml under unity. At this time, the program runs the same, even if I did not add android in unityxml :name="com.goertek.play360.MainActivity", this is still started before GoogleUnityActivity, GoogleUnityActivity is not started. Therefore, the xml under android does not need to be consistent with unity. It only needs to add the permissions to be used, the activity to be started, etc. From the above, it can be observed that unity will find the relevant UnityActivity to start.

If in android use UnityPlayer.UnitySendMessage("Sphere360", "RefreshParaanaFilePath", imagePath);
He is telling an object called Sphere360 in a scene to execute the RefreshParanaFilePath method, but even if my scene is edited in the Unity runtime environment, but it has not been run, then the information from android will not be received. This is similar to android's dynamic broadcast mechanism, which is not executed and not received.



 The attachment is the same as the above, there is no test code, because I tested it in the actual project

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326593129&siteId=291194637