因为安卓的某些权限
在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();
}
}