网页链接拉起Unity Android和iOS APP

1、安卓端代码

package cn.net.xuefei.schemedemo;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
 
public class MainActivity extends UnityPlayerActivity {

    private String launchInfo="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        launchInfo = onLaunchInfo();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        launchInfo = onLaunchInfo();
    }

    private String onLaunchInfo()
    {
        String info="";
        Uri uri = getIntent().getData();
        if (uri != null) {
            // 完整的url信息
            String url = uri.toString();

            info = url;
            Log.e("Unity", "url: " + url);
        }
        return info;

    }

    public void getLaunchInfo()
    {
        UnityPlayer.UnitySendMessage("Main Camera", "OnLaunchInfo", launchInfo);
        launchInfo="";
    }
}

2、Unity中 AndroidManifest.xml




  

    
      
        
        
      
      
        
        
        
        
      
      
    
   

3、Unity中代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if UNITY_IOS
using System.Runtime.InteropServices;
#endif

public class SchemeDemo : MonoBehaviour
{
    public Text text;

    // Use this for initialization
    void Start()
    {
        GetInfo();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnLaunchInfo(string launchInfo)
    {
        Debug.LogError("launchInfo:" + launchInfo);
        text.text = launchInfo;
    }

    public void GetInfo()
    {
        
#if UNITY_ANDROID
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic("currentActivity");
        jo.Call("getLaunchInfo");
#elif UNITY_IOS
        _GetLaunchInfo();
#endif
    }

    private void OnApplicationFocus(bool focus)
    {
        Debug.LogError("focus:" + focus);
        if (true)
        {
            GetInfo();
        }
    }

#if UNITY_IOS
    [DllImport("__Internal")]
    private static extern void _GetLaunchInfo();
#endif
}

4、设置Unity iOS URL Schemes,修改导出的Xcode工程中的UnityAppController.mm

#import "UnityAppController.h"

NSString *URLString = @"";

// 向Unity传递参数;
extern void UnitySendMessage(const char *, const char *, const char *);

//添加的代码
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
    URLString = [url absoluteString]; 
    return YES;
}

- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
	//添加的代码
	URLString = [url absoluteString]; 
	return YES;
}

extern "C"  
{  
    void _GetLaunchInfo();  
}  
  
void _GetLaunchInfo()  
{    
    UnitySendMessage( "Main Camera", [@"OnLaunchInfo" UTF8String], [URLString UTF8String] );  
    // 清空,防止造成干扰;  
    URLString = @"";  
}  

5、网页




	
	
	Document


	
	


网页链接拉起Unity Android和iOS APP_第1张图片

工程地址https://gitee.com/awnuxcvbn/SchemeDemo

你可能感兴趣的:(Unity,Android,iOS,C#)