因爲安卓的某些權限
在Windows支持的File.Copy 不支持在安卓端進行拷貝
所以可以使用WWW加載的方式對文件進行拷貝 粘貼了 部分代碼
using Data;
using EditorTool;
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using XCharts;
public class TestLine_chart : MonoBehaviour
{
//保存json文件路徑
string JsonPath()
{
return Application.persistentDataPath + "/JsonTest.txt";
}
//玩家信息
[Serializable]
public class Player
{
public int id;
public string name;
public int damage;
}
LineChart chart = null;
private void Awake()
{
FirstLogin();
chart = gameObject.GetComponentInChildren<LineChart>();
}
void Start()
{
Invoke("refreshChart", 1);
}
void FirstLogin()
{
if (PlayerPrefs.HasKey("FirstLogin2"))
{
Debug.Log("不是第一次運行");
}
else
{
StartCoroutine(Get());
Debug.Log("第一次");
}
}
IEnumerator Get()
{
UnityWebRequest webRequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/JsonTest.txt");
yield return webRequest.SendWebRequest();
//異常處理,很多博文用了error!=null這是錯誤的,請看下文其他屬性部分
if (webRequest.isHttpError || webRequest.isNetworkError)
Debug.Log(webRequest.error);
else
{
// Debug.Log(webRequest.downloadHandler.text);
byte[] bytes = webRequest.downloadHandler.data;
//創建文件
CreatFile(Application.persistentDataPath + "/JsonTest.txt", bytes);
}
}
public static void CreatFile(string filePath, byte[] bytes)
{
FileInfo file = new FileInfo(filePath);
Stream stream = file.Create();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
}
private void LateUpdate()
{
PlayerPrefs.SetInt("FirstLogin2", 10);
}
public void refreshChart()
{
//設置標題:
chart.title.show = true;
chart.title.text = "Line Simple";
//設置提示框和圖例是否顯示:
chart.tooltip.show = true;
chart.legend.show = false;
//設置是否使用雙座標軸和座標軸類型:
chart.xAxises[0].show = true;
chart.xAxises[1].show = false;
chart.yAxises[0].show = true;
chart.yAxises[1].show = false;
chart.xAxises[0].type = Axis.AxisType.Category;
chart.yAxises[0].type = Axis.AxisType.Value;
//設置座標軸分割線:
chart.xAxises[0].splitNumber = 10;
chart.xAxises[0].boundaryGap = true;
//清空數據,添加Line類型的Serie用於接收數據:
chart.RemoveData();
chart.AddSerie(SerieType.Line);
if (!File.Exists(JsonPath()))
{
Debug.Log("讀取的文件不存在!");
return;
}
string json = File.ReadAllText(JsonPath());
Player[] skillArray = JsonMapper.ToObject<Player[]>(json);
foreach (Player item in skillArray)
{
chart.AddXAxisData(item.name);
chart.AddData(0, item.damage);
}
}
/// <summary>
/// 保存截屏圖片,並且刷新相冊 Android
/// </summary>
/// <param name="name">若空就按照時間命名</param>
public void CaptureScreenshot()
{
string _name = "Screenshot_" + GetCurTime() + ".png";
StartCoroutine(CutImage(_name));
//在手機上顯示路徑
}
//截屏並保存
IEnumerator CutImage(string name)
{
//圖片大小
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
tex.Apply();
yield return tex;
byte[] byt = tex.EncodeToPNG();
string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.IndexOf("Android"));
File.WriteAllBytes(path + "/DCIM/Camera/" + name, byt); //保存到 安卓手機的 DCIM/下的Camera 文件夾下
// File.WriteAllBytes(path + "/截屏/" + name, byt); //保存到安卓手機的 文件管理下面的 《截屏》文件夾下
string[] paths = new string[1];
paths[0] = path;
ScanFile(paths);
}
//刷新圖片,顯示到相冊中
void ScanFile(string[] path)
{
using (AndroidJavaClass PlayerActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject playerActivity = PlayerActivity.GetStatic<AndroidJavaObject>("currentActivity");
using (AndroidJavaObject Conn = new AndroidJavaObject("android.media.MediaScannerConnection", playerActivity, null))
{
Conn.CallStatic("scanFile", playerActivity, path, null, null);
}
}
}
/// <summary>
/// 獲取當前年月日時分秒,如20181001444
/// </summary>
/// <returns></returns>
string GetCurTime()
{
return DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
}
}