The analysis of the interface switching of Unity IOS mixed development _ios

Source: Internet
Author: User
Tags mixed

Ideas

The previous article only talked about unity and the integration of the iOS project, and did not talk about the handover of iOS and Unity interface, here to talk about ideas, unity export of the IOS project inside the structure is roughly the same, there is a window,window on a unityview, But there is no controller, there is no root controller, although in the export of iOS Project Classes folder in the Unityappcontroller in the Rootcontroller attribute, but the above is also labeled empty ~ So, the idea is only one, Since there is a window in unity-exported iOS that doesn't have a controller, well, hybrid development we'll do two windows, one window to show the unity of several sides, and the other window to showcase the iOS APP native interface ~ This is the idea of switching windows. Mixed development This way the idea of switching windows should be very common. The following direct code, the company's important code has been deleted, the core code are in, should not affect your reading ~

Unity interface Switch to iOS interface

Unity part of the code ~

<summary>
///Stop Unity
///</summary>
[DllImport ("__internal")]
private static extern void Stopunity ();
Close UNITY interface public
void Closeunity ()
{
#if unity_android
androidjavaclass JC = new Androidjavaclass (" Com.unity3d.player.UnityPlayer ");
Androidjavaobject Jo = JC. Getstatic<androidjavaobject> ("currentactivity");
Jo. Call ("stopunity");
#elif Unity_iphone | | Unity_ios
stopunity ();
#endif
}

Unity This code is when the unity of the interface click a button to close unity and jump to the iOS interface when executed, the above declares an inline function, this inline function can link to iOS C language functions. In unity, when you execute a method and call the inline function, you execute the inline function that iOS has already written, and the iOS part of the code is as follows.

iOS Part code ~

extern "C" {
void stopunity () {

Unityappcontroller *unityde = (Unityappcontroller *) [uiapplication Sharedapplication].delegate;
if (UnityDe.window.windowLevel = = uiwindowlevelnormal) {
unityDe.window.windowLevel = UIWindowLevelNormal-1;
}
[Unityde.appwindow makekeyandvisible];
}

This code is written in the iOS. mm file, wrapped in extern "C" {}, life is the inline function of C. When Unity calls the Stopunity () method, the Stopunity () method in ios extern "C" {} is invoked ... This is how iOS performs a switching interface in stopunity.

iOS interface switch to Unity interface

The iOS interface clicks a button to switch to the Unity interface.

iOS Part code ~

Click the button to switch to the Unity interface ~
-(void) Didclickbutton {
Unityappcontroller *unityde = (Unityappcontroller *) [ UIApplication sharedapplication].delegate;
if (UnityDe.window.windowLevel = = UIWindowLevelNormal-1) {
unityDe.window.windowLevel = uiwindowlevelnormal;
}
unitysendmessage ("Nativemanager", "nstartunity", "1");
[Unityde.window makekeyandvisible];
}

There is a button in the iOS interface, click the button to switch to the Unity interface, where Unitysendmessage ("Nativemanager", "nstartunity", "1"); The meaning of this code is to send a message to unity, the message sent to unity inside Nativemanager This object, tell Nativemanager this object invoke Nstartunity this method, and pass parameter 1. Ioser See here a bit perhaps puzzled ... Now look at the unity part of the code

Unity scene

There is a nativemanager in the scene, that is, the object that iOS sends, the Nativemanger hangs a script, Yxunityapi, the code in this script is as follows:

Unity part of the code:

Using Unityengine;
Using System.Collections;
Using Vuforia;
Using Cframework;
<summary>
///This script only handles interactions, does not do functional processing ~
///</summary> public
class Yxunityapi:monobehaviour
{
#region new API Interface
///<summary>
///Open Unity, show the first few scenes
///</summary>
/// <param name= "num" > Open scene number </param> public
void Nstartunity (string scensenum)
{
int num = int . Parse (scensenum);
Switch (num) {case
1:
UnityEngine.SceneManagement.SceneManager.LoadScene ("YXMJ");
break;
Case 2:
UnityEngine.SceneManagement.SceneManager.LoadScene ("Gryo");
break;
Default: Break
;
}
Yxunityapihandler.instance (). Startunity (num);
}
#endregion
}

If Bo Friend is a ioser or a unityer, you can and your company another small partner to read the above code, after all, part of the iOS code, part of the unity of the Code, well, the above ideas will be able to achieve unity and the iOS interface switch. If you still have questions ~ Welcome message.

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.