Files
PrinceOfGlory/Assets/代码/通用工具/GameTools.cs
kridoo 6e91a0c7f0 111
2025-09-15 17:32:08 +08:00

300 lines
8.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class GameTools : MonoBehaviour
{
#region
private static GameTools share;
static bool quit;
public static GameTools Share
{
get
{
if (quit == true)
{
return null;
}
if (share == null)
{
share = FindObjectOfType<GameTools>();
if (share == null)
{
var obj = new GameObject("踏狗묏야");
share = obj.AddComponent<GameTools>();
DontDestroyOnLoad(obj);
}
}
return share;
}
}
public void OnDestroy()
{
quit = true;
}
#endregion
#region
/// <summary>
/// 속潼暠튬
/// </summary>
public static void LoadImage(Image image, string Path)
{
try
{
byte[] imgData = File.ReadAllBytes(Path);
Texture2D tex = new Texture2D(1920, 1080);
tex.LoadImage(imgData);
image.sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
/// <summary>
/// 속潼暠튬
/// </summary>
public static void LoadImage(string url, Action<Texture2D> complete)
{
Share.StartCoroutine(Share.loadImageHander(url, complete));
}
IEnumerator loadImageHander(string url, Action<Texture2D> complete)
{
using (var request = UnityWebRequestTexture.GetTexture(url))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
{
complete(null);
}
else
{
complete(DownloadHandlerTexture.GetContent(request));
}
}
}
/// <summary>
/// 姦렴暠튬
/// </summary>
public static void ReleaseImage(Image image)
{
if (image.sprite != null)
{
var tex = image.sprite.texture;
if (tex != null)
{
Resources.UnloadAsset(tex);
}
image.sprite = null;
}
}
/// <summary>
/// 姦렴暠튬
/// </summary>
public static void ReleaseImage(RawImage image)
{
var tex = image.texture;
if (tex != null)
{
Resources.UnloadAsset(tex);
image.texture = null;
}
}
/// <summary>
/// 속潼匡굶
/// </summary>
public static void LoadHtml(string url, Action<string> complete)
{
Share.StartCoroutine(Share.loadHtmlHander(url, complete));
}
IEnumerator loadHtmlHander(string url, Action<string> complete)
{
using (var request = UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
{
complete(null);
}
else
{
complete(request.downloadHandler.text);
}
}
}
/// <summary>
/// 속潼mp3
/// </summary>
public static void LoadAudio(string url, Action<AudioClip> complete)
{
Share.StartCoroutine(Share.loadAudioHander(url, complete));
}
IEnumerator loadAudioHander(string url, Action<AudioClip> complete)
{
using (var request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG))
{
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
{
complete(null);
}
else
{
complete(DownloadHandlerAudioClip.GetContent(request));
}
}
}
/// <summary>
/// 警속莉숭
/// </summary>
public static T LoadComponent<T>(GameObject target) where T : Component
{
var cat = target.GetComponent<T>();
if (cat == null)
{
cat = target.AddComponent<T>();
}
return cat;
}
/// <summary>
/// 警속渡齡竟
/// </summary>
public static T LoadPrefab<T>(string prefab, Transform parent) where T : Component
{
var pre = Resources.Load<T>(prefab);
if (pre != null)
{
var temp = Instantiate(pre);
if (parent != null)
{
temp.transform.SetParent(parent, false);
}
return temp.GetComponent<T>();
}
return default(T);
}
#endregion
#region
/// <summary>
/// 눼쉔커쩌匡숭셸 唐앎꼇눼쉔
/// </summary>
public static void CreateDirectory(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
if (!File.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
}
}
/// <summary>
/// 畇흙匡숭
/// </summary>
/// <param name="filePath"></param>
/// <param name="bytes"></param>
public static void WriteFile(string filePath, byte[] bytes)
{
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
fs.SetLength(0);
fs.Write(bytes, 0, bytes.Length);
}
}
/// <summary>
/// 셨崎畇흙匡숭
/// </summary>
/// <param name="filePath"></param>
/// <param name="bytes"></param>
public static void AppendWriteFile(string filePath, byte[] bytes)
{
using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
fs.Write(bytes, 0, bytes.Length);
}
}
/// <summary>
/// 뗍혤匡숭
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static byte[] ReadFile(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] infbytes = new byte[fs.Length];
fs.Read(infbytes, 0, infbytes.Length);
return infbytes;
}
}
/// <summary>
/// 盧땡匡숭
/// </summary>
public static void MoveFiles(string oldPath, string newPath)
{
if (!Directory.Exists(oldPath))
return;
if (!Directory.Exists(newPath))
Directory.CreateDirectory(newPath);
string[] files = Directory.GetFiles(oldPath);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string destinationPath = Path.Combine(newPath, fileName);
File.Move(file, destinationPath);
}
}
/// <summary>
/// <20>뇜커쩌
/// </summary>
public static void DeleteDirectory(string directoryPath)
{
try
{
if (Directory.Exists(directoryPath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
foreach (FileInfo file in directoryInfo.GetFiles())
{
file.Attributes = FileAttributes.Normal;
file.Delete();
}
foreach (DirectoryInfo subdirectory in directoryInfo.GetDirectories())
{
DeleteDirectory(subdirectory.FullName);
}
directoryInfo.Delete();
Debug.Log("커쩌<ECBBA4>뇜냥묘" + directoryPath);
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
throw;
}
}
#endregion
#region
public void Delay(float delay, Action action)
{
StartCoroutine(DelayToDo(delay, action));
}
IEnumerator DelayToDo(float delay, Action action)
{
// 된덤寧땍돨儺넨珂쇌
yield return new WaitForSeconds(delay);
action();
}
#endregion
}