Unity 与 Android融合

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// 实现Unity所开发工程融入到Android工程并实现简单跳转功能
/// </summary>
public class Test : MonoBehaviour {

    public Text my_test;
    public void Input_Button()
    {
        AndroidJavaClass And_Class = new AndroidJavaClass("com.Coany.ProductName.UnityPlayerActivity");//"包名+主Activity"
        var obj = And_Class.GetStatic<AndroidJavaObject>("currentActivity");
        obj.Call("Test_1");//Android工程中的方法名
    }
    /*
     * 注:
     * 在Unity5.1.2中,"var obj = And_Class.GetStatic<AndroidJavaObject>("currentActivity");"中的"currentActivity"调试出错,
     * 需要在Android工程中的UnityPlayerActivity类中添加:
     * 
     * public static Activity currentActivity;
     * 
     * 并在OnCreate()函数中初始化:
     * 
     * currentActivity = this;
     * 
     * 
     * 调用方法:
     * 
     * public void Test_1() {
		Log.d("abc", "test");
		Intent intent = new Intent(UnityPlayerActivity.this,
				MainActivity.class);
		startActivity(intent);
	}
     */
}

对于跨平台的Bluetooth LE开发,在Unity中可以使用第三方插件来实现,例如Unity Bluetooth LE Asset,它提供了跨平台的Bluetooth LE API,支持iOS、Android、Windows和macOS平台。使用该插件,您可以使用简单的C#代码来连接和通信Bluetooth LE设备。以下是示例代码: ``` // 初始化插件 BluetoothLEHardwareInterface.Initialize(true, false, () => Debug.Log("初始化成功"), (error) => Debug.Log("初始化失败: " + error)); // 扫描设备 BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, (address, deviceName) => { Debug.Log("扫描到设备:Address=" + address + ", Name=" + deviceName); }); // 连接设备 string deviceAddress = "XX:XX:XX:XX:XX:XX"; BluetoothLEHardwareInterface.ConnectToPeripheral(deviceAddress, null, null, (address) => { Debug.Log("连接成功:" + address); }, (address, error) => { Debug.Log("连接失败:" + address + ", error=" + error); }); // 向设备发送数据 byte[] data = new byte[] {0x01, 0x02, 0x03}; BluetoothLEHardwareInterface.WriteCharacteristic(deviceAddress, serviceUUID, characteristicUUID, data, data.Length, true, (characteristicUUID) => { Debug.Log("发送数据成功:" + BitConverter.ToString(data)); }, (characteristicUUID, error) => { Debug.Log("发送数据失败:" + error); }); // 从设备读取数据 BluetoothLEHardwareInterface.ReadCharacteristic(deviceAddress, serviceUUID, characteristicUUID, (characteristicUUID, data) => { Debug.Log("读取数据成功:" + BitConverter.ToString(data)); }, (characteristicUUID, error) => { Debug.Log("读取数据失败:" + error); }); ``` 请注意,使用该插件需要在Unity项目中添加相应的平台插件,例如在iOS平台上需要添加iOS Bluetooth LE插件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值