package com.izaron.habr.unitytorch; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class UnityTorchActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_unity_torch); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.unity_torch, menu); return true; } }
package com.izaron.habr.unitytorch; import com.unity3d.player.UnityPlayerActivity; import android.os.Bundle; public class UnityTorchActivity extends UnityPlayerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
package com.izaron.habr.unitytorch; import com.unity3d.player.UnityPlayerActivity; import android.content.pm.PackageManager; import android.hardware.Camera; import android.hardware.Camera.Parameters; public class Torch { private Camera cam; // public UnityPlayerActivity activity; // public boolean isEnabled; // - public Torch(UnityPlayerActivity activity) { this.activity = activity; isEnabled = false; cam = Camera.open(); // } public boolean hasTorch() { // , false return (activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)); } public void turnLightOn() { if(hasTorch()) { isEnabled = true; cam = Camera.open(); Parameters p = cam.getParameters(); p.setFlashMode(Parameters.FLASH_MODE_TORCH); // cam.setParameters(p); cam.startPreview(); } } public void turnLightOff() { if(hasTorch()) { isEnabled = false; cam.stopPreview(); cam.release(); } } public boolean enabled() { return isEnabled; } }
package com.izaron.habr.unitytorch; import com.unity3d.player.UnityPlayerActivity; import android.os.Bundle; public class UnityTorchActivity extends UnityPlayerActivity { public Torch torch = new Torch(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.izaron.habr.unitytorch" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:label="@string/app_name" android:icon="@drawable/app_icon"> <activity android:name="com.izaron.habr.unitytorch.UnityTorchActivity" android:configChanges="keyboardHidden|orientation" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.WRITE_SETTINGS"/> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> </manifest>
using UnityEngine; using System.Collections; public static class TorchActivity { #if UNITY_ANDROID && !UNITY_EDITOR public static AndroidJavaClass activityClass = new AndroidJavaClass("com.izaron.habr.unitytorch.UnityTorchActivity"); public static AndroidJavaClass unityActivityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); public static AndroidJavaObject activityObj = unityActivityClass.GetStatic<AndroidJavaObject>("currentActivity"); #else public static AndroidJavaClass activityClass; public static AndroidJavaClass unityActivityClass; public static AndroidJavaObject activityObj; #endif }
using UnityEngine; using System.Collections; public static class Torch { #if UNITY_ANDROID && !UNITY_EDITOR private static AndroidJavaObject torchObj = TorchActivity.activityObj.Get<AndroidJavaObject>("torch"); #else private static AndroidJavaObject torchObj; #endif public static bool HasTorch() { if (Application.platform == RuntimePlatform.Android) return torchObj.Call<bool>("hasTorch"); else return false; } public static bool Enabled() { if (Application.platform == RuntimePlatform.Android) return torchObj.Call<bool>("enabled"); else return false; } public static void TurnLightOn() { if (Application.platform == RuntimePlatform.Android) torchObj.Call("turnLightOn"); } public static void TurnLightOff() { if (Application.platform == RuntimePlatform.Android) torchObj.Call("turnLightOff"); } }
Source: https://habr.com/ru/post/226987/
All Articles