Unity調(diào)用IOS和Android

Author :JerryYang
Create by 2020.12.07

環(huán)境:
Unity:2019.4.2f1
Xcode:12.2
Android Studio:4.1.1


在用unity進(jìn)行移動端開發(fā)時,有時會需要和原生進(jìn)行通訊。接下來以unity調(diào)用原生震動為例
unity自己有震動的接口:

Handheld.Vibrate();

這個接口調(diào)用一次震動時長為0.5s,不能修改,并且手機(jī)關(guān)閉震動的情況下無效。
那么我們就需要在IOS和安卓端分別寫好接口給unity調(diào)用。

一、unity和iOS通訊

  1. 首先打開Xcode,新建一個工程Vibrator,然后新建一個類VibratorTool繼承NSObject,具體代碼如下:
VibratorTool.h文件:
//
//  VibratorTool.h
//  Vibrator
//
//  Created by 飛楊 on 02.12.20.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface VibratorTool : NSObject

@end

NS_ASSUME_NONNULL_END

VibratorTool.m文件:
//
//  VibratorTool.m
//  Vibrator
//
//  Created by JerryYang on 02.12.20.
//

#import "VibratorTool.h"
#import <UIKit/UIImpactFeedbackGenerator.h>

@implementation VibratorTool

#if defined (__cplusplus)
extern "C"
{
#endif
     void setVibrator(float Intensity){
        UIImpactFeedbackGenerator * generator;
        if (@available(iOS 13.0, *)) {
            generator = [[UIImpactFeedbackGenerator alloc]init];
            [generator impactOccurredWithIntensity:Intensity];
        } else {
            if (Intensity< 0.33) {
                generator = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleLight];
            }else if(Intensity >=0.33 && Intensity < 0.66){
                generator = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleMedium];
            }else{
                generator = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleHeavy];
            }
            [generator impactOccurred];
        }
    }
    
#if defined (__cplusplus)
}
#endif

@end

  1. 在unity隨便個文件夾,把這兩個文件拖進(jìn)去,勾選iOS平臺


    勾選對應(yīng)平臺.png
  2. 在unity引入命名空間:

using System.Runtime.InteropServices;

新建類 Visrator.cs 具體的代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

/// <summary>
/// 震動反饋
/// Author JerryYang
/// Create by 2020.12.03
/// </summary>

public class Visrator
{
#if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    private static extern void setVibrator(float Intensity);
#endif


    public void SetVibrator(float Intensity = 1)
    {
#if UNITY_IOS && !UNITY_EDITOR
           setVibrator(Intensity);
#endif
    }

}

二、unity和android通訊

  1. 首先打開Android,新建一個工程Vibrator,然后新建一個類VibratorTool,具體代碼如下:
package com.example.vibrate;
import android.content.Context;
import android.os.Vibrator;

public class VibratorTool {
    public void setVibrator(Context act, long t){
        Vibrator vibrator = (Vibrator) act.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(t);
    }
}

unity默認(rèn)是加了震動權(quán)限,如果沒有權(quán)限,請?jiān)?AndroidManifest.xml 文件中添加:

<uses-permission android:name="android.permission.VIBRATE" />
  1. 在unity隨便個文件夾,把這個文件拖進(jìn)去,勾選Android平臺


    勾選對應(yīng)平臺.png
  2. 在 Visrator.cs 文件中修改SetVibrator方法為:
public void SetVibrator(float Intensity = 1)
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        AndroidJavaObject jo = new AndroidJavaObject("com.example.vibrate.VibratorTool");
        AndroidJavaClass act = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        var actObj = act.GetStatic<AndroidJavaObject>("currentActivity");
        long l = (long)(Intensity * 10);
        jo.Call("setVibrator", actObj,l);
#elif UNITY_IOS && !UNITY_EDITOR
           setVibrator(Intensity);
#endif
    }

這樣簡單的通訊功能就完成了,同時用到了OC,C#,Java,C++。

注意:只有真機(jī)才能測試哦!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容