152 lines
4.5 KiB
C#
152 lines
4.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using FairyGUI;
|
|
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Net;
|
|
using taurus.unity;
|
|
using System.Globalization;
|
|
|
|
namespace Game
|
|
{
|
|
public static class Utils
|
|
{
|
|
|
|
public static GameObject CopyPrefab(GameObject prefab)
|
|
{
|
|
var obj = GameObject.Instantiate<GameObject>(prefab);
|
|
obj.transform.localPosition = Vector3.zero;
|
|
obj.transform.localRotation = Quaternion.identity;
|
|
obj.transform.localScale = new Vector3(100, 100, 100);
|
|
return obj;
|
|
}
|
|
|
|
public static void SetNativeObject(GObject node, string path)
|
|
{
|
|
if (node.asGraph.displayObject == null)
|
|
{
|
|
var obj = ResourcesManager.LoadObject<GameObject>(path);
|
|
var tem = CopyPrefab(obj);
|
|
node.asGraph.SetNativeObject(new GoWrapper(tem));
|
|
}
|
|
}
|
|
|
|
public static string GetTime(long time)
|
|
{
|
|
DateTime s = new DateTime(1970, 1, 1);
|
|
s = s.AddSeconds(time);
|
|
s = TimeZone.CurrentTimeZone.ToLocalTime(s);
|
|
return s.ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
|
|
public static bool IsDoubleClick(EventContext context)
|
|
{
|
|
return context.inputEvent.isDoubleClick;
|
|
}
|
|
|
|
private static readonly string _fileDir = Application.persistentDataPath + "/UserData/";
|
|
|
|
/// <summary>
|
|
/// 写入本地数据
|
|
/// </summary>
|
|
/// <param name="data"></param>
|
|
|
|
public static void SaveLocalFile(string key, string data)
|
|
{
|
|
if (!Directory.Exists(_fileDir))
|
|
{
|
|
Directory.CreateDirectory(_fileDir);
|
|
}
|
|
File.WriteAllText(_fileDir + key, data);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 读取本地数据
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string LoadLocalFile(string key)
|
|
{
|
|
var file = _fileDir + key;
|
|
if (!File.Exists(file)) return null;
|
|
return File.ReadAllText(file);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取本地数据
|
|
/// </summary>
|
|
public static void ClearLocalFile(long uid)
|
|
{
|
|
var file = _fileDir + uid;
|
|
if (File.Exists(file))
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存图片到本地,并设置尺寸
|
|
/// </summary>
|
|
/// <param name="path">保存路径</param>
|
|
/// <param name="texture">图层</param>
|
|
/// <param name="heigth">高度</param>
|
|
/// <param name="width">宽度</param>
|
|
public static Texture2D SaveScaledPicture(string path, Texture2D texture, int heigth, int width)
|
|
{
|
|
Texture2D scaled_pic = new Texture2D(width, heigth, texture.format, false);
|
|
for (int i = 0; i < scaled_pic.height; ++i)
|
|
{
|
|
for (int j = 0; j < scaled_pic.width; ++j)
|
|
{
|
|
Color newColor = texture.GetPixelBilinear((float)j / (float)scaled_pic.width, (float)i / (float)scaled_pic.height);
|
|
scaled_pic.SetPixel(j, i, newColor);
|
|
}
|
|
}
|
|
scaled_pic.Apply();
|
|
var data = scaled_pic.EncodeToJPG(60);
|
|
File.WriteAllBytes(path, data);
|
|
return scaled_pic;
|
|
}
|
|
|
|
public static void LocalAddress(LuaInterface.LuaFunction callback)
|
|
{
|
|
GPSAddress.LocalAddress(callback);
|
|
}
|
|
|
|
|
|
static public bool IsGPSEnable()
|
|
{
|
|
return GPSAddress.IsGPSEnable();
|
|
}
|
|
|
|
public static void DownloadFile(string url, string path, LuaInterface.LuaFunction callback)
|
|
{
|
|
BestHTTP.HTTPManager.SendRequest(url, (request, response) =>
|
|
{
|
|
if (request.State == BestHTTP.HTTPRequestStates.Finished && request.Response.IsSuccess)
|
|
{
|
|
var data = request.Response.Data;
|
|
File.WriteAllBytes(path, data);
|
|
callback.Call();
|
|
callback.Dispose();
|
|
}
|
|
});
|
|
}
|
|
|
|
public static string TextOmit(string str, int num)
|
|
{
|
|
StringInfo info = new StringInfo(str);
|
|
|
|
if (info.LengthInTextElements > num)
|
|
{
|
|
return info.SubstringByTextElements(0, 6) + "...";
|
|
}
|
|
|
|
return str;
|
|
}
|
|
}
|
|
}
|
|
|