MIF_E31230837/Assets/Script/DwonloadMarker.cs

121 lines
3.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO;
using TMPro;
public class DownloadMarker : MonoBehaviour
{
[Header("Settings")]
public string fileName = "marker.pdf";
[Header("UI (opsional)")]
public TextMeshProUGUI statusText;
public Button downloadButton;
private string sourcePath;
private string destPath;
void Start()
{
sourcePath = Path.Combine(Application.streamingAssetsPath, fileName);
#if UNITY_ANDROID && !UNITY_EDITOR
string externalDir = GetAndroidExternalStorage();
destPath = Path.Combine(externalDir, fileName);
#else
destPath = Path.Combine(Application.persistentDataPath, fileName);
#endif
}
public void OnClickDownload()
{
if (downloadButton != null) downloadButton.interactable = false;
SetStatus("Menyiapkan marker...");
StartCoroutine(CopyAndOpenPDF());
}
IEnumerator CopyAndOpenPDF()
{
if (!File.Exists(destPath))
{
SetStatus("Mengunduh file...");
using (UnityEngine.Networking.UnityWebRequest req =
UnityEngine.Networking.UnityWebRequest.Get(sourcePath))
{
yield return req.SendWebRequest();
if (req.result != UnityEngine.Networking.UnityWebRequest.Result.Success)
{
SetStatus("Gagal memuat: " + req.error);
if (downloadButton != null) downloadButton.interactable = true;
yield break;
}
File.WriteAllBytes(destPath, req.downloadHandler.data);
Debug.Log("[DownloadMarker] Saved to: " + destPath);
}
}
SetStatus("Membuka marker...");
OpenPDF();
yield return new WaitForSeconds(3f);
SetStatus("");
if (downloadButton != null) downloadButton.interactable = true;
}
void OpenPDF()
{
#if UNITY_EDITOR
System.Diagnostics.Process.Start(destPath);
#elif UNITY_ANDROID
try
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject file = new AndroidJavaObject("java.io.File", destPath);
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uri = uriClass.CallStatic<AndroidJavaObject>("fromFile", file);
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent");
intent.Call<AndroidJavaObject>("setAction", "android.intent.action.VIEW");
intent.Call<AndroidJavaObject>("setDataAndType", uri, "application/pdf");
intent.Call<AndroidJavaObject>("addFlags", 0x00000001);
activity.Call("startActivity", intent);
}
catch (System.Exception e)
{
SetStatus("Tidak ada app PDF. File ada di: " + destPath);
Debug.LogError("[DownloadMarker] Error: " + e.Message);
}
#endif
}
string GetAndroidExternalStorage()
{
try
{
AndroidJavaClass envClass = new AndroidJavaClass("android.os.Environment");
AndroidJavaObject downloadsDir = envClass.CallStatic<AndroidJavaObject>(
"getExternalStoragePublicDirectory",
envClass.GetStatic<AndroidJavaObject>("DIRECTORY_DOWNLOADS")
);
return downloadsDir.Call<string>("getAbsolutePath");
}
catch
{
return Application.persistentDataPath;
}
}
void SetStatus(string msg)
{
if (statusText != null) statusText.text = msg;
Debug.Log("[DownloadMarker] " + msg);
}
}