首次提交
This commit is contained in:
48
Assets/Scripts/NBC/Asset/Runtime/Utils/Mono.cs
Normal file
48
Assets/Scripts/NBC/Asset/Runtime/Utils/Mono.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public class Mono : MonoBehaviour
|
||||
{
|
||||
public static void AddUpdate(Action action)
|
||||
{
|
||||
Inst.OnUpdate += action;
|
||||
}
|
||||
|
||||
public static void RemoveUpdate(Action action)
|
||||
{
|
||||
Inst.OnUpdate -= action;
|
||||
}
|
||||
|
||||
private event Action OnUpdate;
|
||||
|
||||
private static bool IsQuiting { get; set; }
|
||||
|
||||
private static Mono _inst;
|
||||
|
||||
private static Mono Inst => _inst;
|
||||
|
||||
protected void OnApplicationQuit()
|
||||
{
|
||||
IsQuiting = true;
|
||||
}
|
||||
|
||||
protected void Awake()
|
||||
{
|
||||
if (_inst != null)
|
||||
{
|
||||
DestroyImmediate(this.gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
DontDestroyOnLoad(gameObject);
|
||||
_inst = this;
|
||||
}
|
||||
|
||||
protected void Update()
|
||||
{
|
||||
OnUpdate?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/NBC/Asset/Runtime/Utils/Mono.cs.meta
Normal file
3
Assets/Scripts/NBC/Asset/Runtime/Utils/Mono.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 65fa40d33dad48ce98cf96a06fcb1923
|
||||
timeCreated: 1676445894
|
||||
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public sealed class StreamingAssetsUtil
|
||||
{
|
||||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||||
private static AndroidJavaClass _unityPlayerClass;
|
||||
public static AndroidJavaClass UnityPlayerClass
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_unityPlayerClass == null)
|
||||
_unityPlayerClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
||||
return _unityPlayerClass;
|
||||
}
|
||||
}
|
||||
|
||||
private static AndroidJavaObject _currentActivity;
|
||||
public static AndroidJavaObject CurrentActivity
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_currentActivity == null)
|
||||
_currentActivity = UnityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
|
||||
return _currentActivity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 利用安卓原生接口查询内置文件是否存在
|
||||
/// </summary>
|
||||
public static bool FileExists(string filePath)
|
||||
{
|
||||
return CurrentActivity.Call<bool>("CheckAssetExist", filePath);
|
||||
}
|
||||
#else
|
||||
public static bool FileExists(string filePath)
|
||||
{
|
||||
return System.IO.File.Exists(System.IO.Path.Combine(Application.streamingAssetsPath, filePath));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51cf1710fc4454b43af24289d9f807d5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
108
Assets/Scripts/NBC/Asset/Runtime/Utils/Util.cs
Normal file
108
Assets/Scripts/NBC/Asset/Runtime/Utils/Util.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NBC.Asset
|
||||
{
|
||||
public static class Util
|
||||
{
|
||||
public static long GetTimestamp()
|
||||
{
|
||||
var timeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||||
return Convert.ToInt64(timeSpan.TotalSeconds);
|
||||
}
|
||||
public static DateTime TimestampToTime(long timestamp)
|
||||
{
|
||||
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||||
return startTime.AddSeconds(timestamp);
|
||||
}
|
||||
|
||||
public static string GetFriendlySize(long byteSize)
|
||||
{
|
||||
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
|
||||
int order = 0;
|
||||
long prevOrderRemainder = 0;
|
||||
while (byteSize >= 1024 && order < sizes.Length - 1)
|
||||
{
|
||||
order++;
|
||||
prevOrderRemainder = byteSize % 1024;
|
||||
byteSize /= 1024;
|
||||
}
|
||||
|
||||
double byteSizeFloat = byteSize + (double)prevOrderRemainder / 1024;
|
||||
|
||||
string result = $"{byteSizeFloat:0.##}{sizes[order]}";
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetAssetGUID(string path, Type type)
|
||||
{
|
||||
return type == null ? $"{path}[null]" : $"{path}[{type.Name}]";
|
||||
}
|
||||
|
||||
public static string NameAddHash(string name, string hash)
|
||||
{
|
||||
var ext = Path.GetExtension(name);
|
||||
var nameWithExt = name.Replace(ext, string.Empty);
|
||||
return $"{nameWithExt}_{hash}{ext}";
|
||||
}
|
||||
|
||||
public static int GetFileSize(string path)
|
||||
{
|
||||
if (!File.Exists(path)) return 0;
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
return bytes.Length;
|
||||
}
|
||||
|
||||
public static string ComputeHash(byte[] bytes)
|
||||
{
|
||||
var data = MD5.Create().ComputeHash(bytes);
|
||||
return GetHash(data);
|
||||
}
|
||||
|
||||
public static string ComputeHash(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
using (var stream = File.OpenRead(filePath))
|
||||
{
|
||||
return GetHash(MD5.Create().ComputeHash(stream));
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetHash(byte[] bytes)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var b in bytes)
|
||||
{
|
||||
sb.Append(b.ToString("x2"));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
||||
public static void WriteJson(object so, string filePath)
|
||||
{
|
||||
var json = JsonUtility.ToJson(so);
|
||||
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
|
||||
public static T ReadJson<T>(string filePath)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
var json = File.ReadAllText(filePath);
|
||||
return JsonUtility.FromJson<T>(json);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/NBC/Asset/Runtime/Utils/Util.cs.meta
Normal file
3
Assets/Scripts/NBC/Asset/Runtime/Utils/Util.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 806e9a48e3054f61acd04f83fe429b24
|
||||
timeCreated: 1675051516
|
||||
Reference in New Issue
Block a user