增加了基础工具包
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@ charset = utf-8
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
tab_width = 4
|
||||
|
||||
|
||||
|
||||
# if (...) {
|
||||
# ...
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3a9f0035e541db4b84669311dd168a1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2293d2090267954bb2e45486f1a6fc9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 资源模块
|
||||
/// </summary>
|
||||
public class Assets<Data> : Single<Assets<Data>> {
|
||||
protected List<Data> datas = new List<Data>();
|
||||
|
||||
/// <summary> 更改事件 </summary>
|
||||
public virtual event Action<Assets<Data>> OnChange;
|
||||
/// <summary> 数据列表 </summary>
|
||||
public virtual List<Data> Datas => datas;
|
||||
/// <summary> 数据计数 </summary>
|
||||
public virtual int Count => Datas.Count;
|
||||
/// <summary> 数据操作 </summary
|
||||
public virtual Data this[int index] => Datas[index];
|
||||
|
||||
/// <summary> 添加数据 </summary>
|
||||
public virtual void Add(Data data) { Datas.Add(data); OnChange?.Invoke(this); }
|
||||
/// <summary> 添加数据 </summary>
|
||||
public virtual void Add(IList<Data> data) { Datas.AddRange(data); OnChange?.Invoke(this); }
|
||||
/// <summary> 删除数据 </summary>
|
||||
public virtual void Remove(Data data) { Datas.Remove(data); OnChange?.Invoke(this); }
|
||||
/// <summary> 清除数据 </summary>
|
||||
public virtual void Clear() { Datas.Clear(); OnChange?.Invoke(this); }
|
||||
|
||||
/// <summary> 保存数据 </summary>
|
||||
public virtual void Save() { throw new NotImplementedException(); }
|
||||
/// <summary> 加载数据 </summary>
|
||||
public virtual void Load() { throw new NotImplementedException(); }
|
||||
|
||||
/// <summary> 循环列表 </summary>
|
||||
public virtual void ForEach(Action<Data> action) => Datas.ForEach(action);
|
||||
}
|
||||
/// <summary>
|
||||
/// 资源模块工具
|
||||
/// </summary>
|
||||
public static class AssetsTool {
|
||||
/// <summary> 头尾循环标准化索引 </summary>
|
||||
public static Data LoopIndex<Data>(this Assets<Data> assets, int index) {
|
||||
return assets[LoopIndex(index, assets.Count)];
|
||||
}
|
||||
/// <summary> 头尾循环标准化索引 </summary>
|
||||
public static Data LoopIndex<Data>(this List<Data> list, int index) {
|
||||
return list[LoopIndex(index, list.Count)];
|
||||
}
|
||||
/// <summary> 头尾循环标准化索引 </summary>
|
||||
public static Data LoopIndex<Data>(this Data[] array, int index) {
|
||||
return array[LoopIndex(index, array.Length)];
|
||||
}
|
||||
/// <summary> 头尾循环标准化索引 </summary>
|
||||
public static int LoopIndex(int index, int maxIndex) {
|
||||
return index % maxIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f240e58226b9d1440b23f97be93c4f00
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 事件处理器
|
||||
/// </summary>
|
||||
public class Handle<Data> : Single<Assets<Data>> {
|
||||
/// <summary> 数据 </summary>
|
||||
protected Data value;
|
||||
|
||||
/// <summary> 当前数据 </summary>
|
||||
public virtual Data Current => value;
|
||||
/// <summary> 当前数据是否有效 </summary>
|
||||
public virtual bool IsValid => Current != null;
|
||||
|
||||
/// <summary> 改变当前数据 Event </summary>
|
||||
public virtual event Action<Data> OnChange;
|
||||
/// <summary> 改变当前数据 </summary>
|
||||
public virtual void Change() => OnChange?.Invoke(value);
|
||||
/// <summary> 改变当前数据 </summary>
|
||||
public virtual void Change(Data value) { this.value = value; Change(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e27b368a7807c74987bd3cb5287968a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "MuHua"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b88cc9b1727cc3a45a42964e99e119c1
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf2bc427dc33d6249adc8ab937d37331
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 单例基类
|
||||
/// </summary>
|
||||
public class Single<T> where T : Single<T>, new() {
|
||||
/// <summary> 模块单例 </summary>
|
||||
public static T I => Instantiate();
|
||||
|
||||
protected static T instance;
|
||||
protected static T Instantiate() => instance == null ? instance = new T() : instance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 864d5767e91be034eb47a0561aba685a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 单例行为
|
||||
/// </summary>
|
||||
public abstract class SingleBehaviour<T> : MonoBehaviour where T : SingleBehaviour<T> {
|
||||
/// <summary> 模块单例 </summary>
|
||||
public static T I => instance;
|
||||
/// <summary> 模块单例 </summary>
|
||||
protected static T instance;
|
||||
/// <summary> 初始化 </summary>
|
||||
protected abstract void Awake();
|
||||
|
||||
/// <summary> 替换,并且设置切换场景不销毁 </summary>
|
||||
protected virtual void Replace(bool isDontDestroy = true) {
|
||||
if (instance != null) { Destroy(instance.gameObject); }
|
||||
instance = (T)this;
|
||||
if (isDontDestroy) { DontDestroyOnLoad(gameObject); }
|
||||
}
|
||||
/// <summary> 不替换,并且设置切换场景不销毁 </summary>
|
||||
protected virtual void NoReplace(bool isDontDestroy = true) {
|
||||
if (isDontDestroy) { DontDestroyOnLoad(gameObject); }
|
||||
if (instance == null) { instance = (T)this; }
|
||||
else { Destroy(gameObject); }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 103d1480bcf841b40858282d95c83e99
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dc33220895840b14eb2d7030498033b0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace MuHua {
|
||||
public static class FileTool {
|
||||
#region 选择文件路径
|
||||
public static bool OpenFile(string title, out string path, params string[] type) {
|
||||
FileDialog fd = new FileDialog();
|
||||
fd.structSize = Marshal.SizeOf(fd);
|
||||
fd.filter = FileType(type);
|
||||
fd.file = new string(new char[256]);
|
||||
fd.maxFile = fd.file.Length;
|
||||
fd.fileTitle = new string(new char[64]);
|
||||
fd.maxFileTitle = fd.fileTitle.Length;
|
||||
fd.initialDir = "C:/";
|
||||
fd.title = title;
|
||||
fd.defExt = type[0];
|
||||
fd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
|
||||
bool result = GetOpenFileName(fd);
|
||||
path = fd.file;
|
||||
return result;
|
||||
}
|
||||
public static string FileType(params string[] array) {
|
||||
string type = "";
|
||||
foreach (var item in array) {
|
||||
type += "文件(*." + item + ")\0*." + item + "\0";
|
||||
}
|
||||
return type;
|
||||
}
|
||||
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
|
||||
public static extern bool GetOpenFileName([In, Out] FileDialog fileDialog);
|
||||
#endregion
|
||||
#region 打开文件夹
|
||||
[DllImport("Shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
|
||||
private static extern IntPtr SHBrowseForFolder([In, Out] BrowseFolder browseFolder);
|
||||
[DllImport("Shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
|
||||
public static extern bool SHGetPathFromIDList([In] IntPtr dlist, [In] char[] path);
|
||||
public static string BrowseForFolder(string title = "Path") {
|
||||
BrowseFolder temp = new BrowseFolder();
|
||||
temp.pszDisplayName = new string(new char[2048]);
|
||||
temp.lpszTitle = title;
|
||||
temp.ulFlags = 0x00000040;
|
||||
IntPtr a = SHBrowseForFolder(temp);
|
||||
char[] path = new char[2048];
|
||||
for (int i = 0; i < 2048; i++) { path[i] = '\0'; }
|
||||
SHGetPathFromIDList(a, path);
|
||||
string res = new string(path);
|
||||
return res.Substring(0, res.IndexOf('\0'));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
public class FileDialog {
|
||||
public int structSize = 0;
|
||||
public IntPtr dlgOwner = IntPtr.Zero;
|
||||
public IntPtr instance = IntPtr.Zero;
|
||||
public string filter = null;//筛选文件类型
|
||||
public string customFilter = null;
|
||||
public int maxCustFilter = 0;
|
||||
public int filterIndex = 0;
|
||||
public string file = null;
|
||||
public int maxFile = 0;
|
||||
public string fileTitle = null;
|
||||
public int maxFileTitle = 0;
|
||||
public string initialDir = null;//默认路径
|
||||
public string title = null;
|
||||
public int flags = 0;
|
||||
public short fileOffset = 0;
|
||||
public short fileExtension = 0;
|
||||
public string defExt = null;
|
||||
public IntPtr custData = IntPtr.Zero;
|
||||
public IntPtr hook = IntPtr.Zero;
|
||||
public string templateName = null;
|
||||
public IntPtr reservedPtr = IntPtr.Zero;
|
||||
public int reservedInt = 0;
|
||||
public int flagsEx = 0;
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
public class BrowseFolder {
|
||||
public IntPtr hwndOwner = IntPtr.Zero;
|
||||
public IntPtr pidlRoot = IntPtr.Zero;
|
||||
public String pszDisplayName = null;
|
||||
public String lpszTitle = null;
|
||||
public UInt32 ulFlags = 0;
|
||||
public IntPtr lpfn = IntPtr.Zero;
|
||||
public IntPtr lParam = IntPtr.Zero;
|
||||
public int iImage = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd18523b9fa12c14fb26cb8e7fbe6713
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>json解析与保存</summary>
|
||||
public static class JsonTool {
|
||||
/// <summary> 内部包装类 </summary>
|
||||
private class Pack<T> { public T data; }
|
||||
/// <summary> 把对象转换为Json字符串 </summary>
|
||||
/// <param name="obj">对象</param>
|
||||
public static string ToJson<T>(T obj) {
|
||||
if (obj == null) return "null";
|
||||
if (obj.GetType().GetInterface("IList") != null) {
|
||||
Pack<T> pack = new Pack<T>();
|
||||
pack.data = obj;
|
||||
string json = JsonUtility.ToJson(pack);
|
||||
return json.Substring(8, json.Length - 9);
|
||||
}
|
||||
return JsonUtility.ToJson(obj);
|
||||
}
|
||||
/// <summary> 解析Json </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="json">Json字符串</param>
|
||||
public static T FromJson<T>(string json) {
|
||||
if (json == "null" && typeof(T).IsClass) return default(T);
|
||||
if (typeof(T).GetInterface("IList") != null) {
|
||||
json = "{\"data\":{data}}".Replace("{data}", json);
|
||||
Pack<T> Pack = JsonUtility.FromJson<Pack<T>>(json);
|
||||
return Pack.data;
|
||||
}
|
||||
return JsonUtility.FromJson<T>(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a0d672eab72d3942b6646e769595b33
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,120 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua {
|
||||
public static class RayTool {
|
||||
public static RaycastHit hitInfo;
|
||||
public static readonly LayerMask DefaultLayerMask = ~(1 << 0) | 1 << 0;
|
||||
|
||||
/// <summary> 鼠标坐标转世界坐标 </summary>
|
||||
public static bool GetMouseToWorldPosition(out Vector3 position) {
|
||||
return GetScreenToWorldPosition(Input.mousePosition, out position);
|
||||
}
|
||||
/// <summary> 鼠标坐标转世界坐标 </summary>
|
||||
public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position) {
|
||||
return GetScreenToWorldPosition(camera, Input.mousePosition, out position);
|
||||
}
|
||||
/// <summary> 鼠标坐标转世界坐标 </summary>
|
||||
public static bool GetMouseToWorldPosition(out Vector3 position, LayerMask planeLayerMask) {
|
||||
return GetScreenToWorldPosition(Input.mousePosition, out position, planeLayerMask);
|
||||
}
|
||||
/// <summary> 鼠标坐标转世界坐标 </summary>
|
||||
public static bool GetMouseToWorldPosition(Camera camera, out Vector3 position, LayerMask planeLayerMask) {
|
||||
return GetScreenToWorldPosition(camera, Input.mousePosition, out position, planeLayerMask);
|
||||
}
|
||||
|
||||
/// <summary> 屏幕坐标转世界坐标 </summary>
|
||||
public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position) {
|
||||
return GetScreenToWorldPosition(screen, out position, DefaultLayerMask);
|
||||
}
|
||||
/// <summary> 屏幕坐标转世界坐标 </summary>
|
||||
public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position) {
|
||||
return GetScreenToWorldPosition(camera, screen, out position, DefaultLayerMask);
|
||||
}
|
||||
/// <summary> 屏幕坐标转世界坐标 </summary>
|
||||
public static bool GetScreenToWorldPosition(Vector3 screen, out Vector3 position, LayerMask planeLayerMask) {
|
||||
return GetScreenToWorldPosition(Camera.main, screen, out position, planeLayerMask);
|
||||
}
|
||||
/// <summary> 屏幕坐标转世界坐标 </summary>
|
||||
public static bool GetScreenToWorldPosition(Camera camera, Vector3 screen, out Vector3 position, LayerMask planeLayerMask) {
|
||||
Ray ray = camera.ScreenPointToRay(screen);
|
||||
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
position = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
|
||||
if (hitInfo.transform != null) { position = hitInfo.point; }
|
||||
return hitInfo.transform != null;
|
||||
}
|
||||
|
||||
/// <summary> 从鼠标坐标获取对象 </summary>
|
||||
public static bool GetMouseToWorldObject<T>(out T value) where T : Object {
|
||||
return GetScreenToWorldObject(Input.mousePosition, out value);
|
||||
}
|
||||
/// <summary> 从鼠标坐标获取对象 </summary>
|
||||
public static bool GetMouseToWorldObject<T>(Camera camera, out T value) where T : Object {
|
||||
return GetScreenToWorldObject(camera, Input.mousePosition, out value);
|
||||
}
|
||||
/// <summary> 从鼠标坐标获取对象 </summary>
|
||||
public static bool GetMouseToWorldObject<T>(out T value, LayerMask planeLayerMask) where T : Object {
|
||||
return GetScreenToWorldObject(Input.mousePosition, out value, planeLayerMask);
|
||||
}
|
||||
/// <summary> 从鼠标坐标获取对象 </summary>
|
||||
public static bool GetMouseToWorldObject<T>(Camera camera, out T value, LayerMask planeLayerMask) where T : Object {
|
||||
return GetScreenToWorldObject(camera, Input.mousePosition, out value, planeLayerMask);
|
||||
}
|
||||
|
||||
/// <summary> 从屏幕坐标获取对象 </summary>
|
||||
public static bool GetScreenToWorldObject<T>(Vector3 screen, out T value) where T : Object {
|
||||
return GetScreenToWorldObject(screen, out value, DefaultLayerMask);
|
||||
}
|
||||
/// <summary> 从屏幕坐标获取对象 </summary>
|
||||
public static bool GetScreenToWorldObject<T>(Camera camera, Vector3 screen, out T value) where T : Object {
|
||||
return GetScreenToWorldObject(camera, screen, out value, DefaultLayerMask);
|
||||
}
|
||||
/// <summary> 从屏幕坐标获取对象 </summary>
|
||||
public static bool GetScreenToWorldObject<T>(Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object {
|
||||
return GetScreenToWorldObject(Camera.main, screen, out value, planeLayerMask);
|
||||
}
|
||||
/// <summary> 从屏幕坐标获取对象 </summary>
|
||||
public static bool GetScreenToWorldObject<T>(Camera camera, Vector3 screen, out T value, LayerMask planeLayerMask) where T : Object {
|
||||
Ray ray = camera.ScreenPointToRay(screen);
|
||||
Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
value = hitInfo.transform?.GetComponent<T>();
|
||||
return value != null;
|
||||
}
|
||||
|
||||
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
|
||||
public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo) {
|
||||
return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo);
|
||||
}
|
||||
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
|
||||
public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo) {
|
||||
return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo);
|
||||
}
|
||||
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
|
||||
public static bool GetMouseToWorldHitInfo(out RaycastHit hitInfo, LayerMask planeLayerMask) {
|
||||
return GetScreenToWorldHitInfo(Input.mousePosition, out hitInfo, planeLayerMask);
|
||||
}
|
||||
/// <summary> 从鼠标坐标获取碰撞信息 </summary>
|
||||
public static bool GetMouseToWorldHitInfo(Camera camera, out RaycastHit hitInfo, LayerMask planeLayerMask) {
|
||||
return GetScreenToWorldHitInfo(camera, Input.mousePosition, out hitInfo, planeLayerMask);
|
||||
}
|
||||
|
||||
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
|
||||
public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo) {
|
||||
return GetScreenToWorldHitInfo(screen, out hitInfo, DefaultLayerMask);
|
||||
}
|
||||
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
|
||||
public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo) {
|
||||
return GetScreenToWorldHitInfo(camera, screen, out hitInfo, DefaultLayerMask);
|
||||
}
|
||||
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
|
||||
public static bool GetScreenToWorldHitInfo(Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) {
|
||||
return GetScreenToWorldHitInfo(Camera.main, screen, out hitInfo, planeLayerMask);
|
||||
}
|
||||
/// <summary> 从屏幕坐标获取碰撞信息 </summary>
|
||||
public static bool GetScreenToWorldHitInfo(Camera camera, Vector3 screen, out RaycastHit hitInfo, LayerMask planeLayerMask) {
|
||||
Ray ray = camera.ScreenPointToRay(screen);
|
||||
return Physics.Raycast(ray, out hitInfo, 200, planeLayerMask);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 735bce90ef65acd4c91d821d00bfbcf2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MuHua
|
||||
{
|
||||
public static class SaveTool
|
||||
{
|
||||
/// <summary>默认扩展名</summary>
|
||||
public const string EXTENSION = "Json";
|
||||
/// <summary>各平台本地保存路径</summary>
|
||||
public static string PATH
|
||||
{
|
||||
#if UNITY_IOS
|
||||
get { return Application.persistentDataPath;}
|
||||
#elif UNITY_ANDROID
|
||||
get { return Application.persistentDataPath;}
|
||||
#else
|
||||
get { return Application.streamingAssetsPath; }
|
||||
#endif
|
||||
}
|
||||
#region 字符串保存
|
||||
/// <summary>保存字符串到本地文件夹</summary>
|
||||
/// <param name="directory">文件夹</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <param name="saveString">保存内容</param>
|
||||
public static void SaveText(string directory, string fileName, string saveString)
|
||||
{
|
||||
string filePath = directory + fileName;
|
||||
if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); }
|
||||
File.WriteAllText(filePath, saveString);
|
||||
}
|
||||
/// <summary>读取文件返回字符串</summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns>读取内容</returns>
|
||||
public static string LoadText(string filePath)
|
||||
{
|
||||
if (File.Exists(filePath)) { return File.ReadAllText(filePath); }
|
||||
else { return null; }
|
||||
}
|
||||
#endregion
|
||||
#region 编码字符串保存
|
||||
/// <summary>保存编码字符串到本地文件夹</summary>
|
||||
/// <param name="directory">文件夹</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <param name="saveString">保存内容</param>
|
||||
public static void SaveEncodingString(string directory, string fileName, string saveString, Encoding encodeType)
|
||||
{
|
||||
string filePath = directory + fileName;
|
||||
if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); }
|
||||
byte[] byteArray = encodeType.GetBytes(saveString);
|
||||
string base64 = Convert.ToBase64String(byteArray);
|
||||
File.WriteAllText(filePath, base64);
|
||||
}
|
||||
/// <summary>读取编码文件返回字符串</summary>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns>读取内容</returns>
|
||||
public static string LoadEncodingString(string filePath, Encoding encodeType)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string base64 = File.ReadAllText(filePath);
|
||||
byte[] byteArray = Convert.FromBase64String(base64);
|
||||
return encodeType.GetString(byteArray);
|
||||
}
|
||||
else { return null; }
|
||||
}
|
||||
#endregion
|
||||
#region 对象转Json的保存与加载
|
||||
/// <summary>保存Object为Json文件</summary>
|
||||
/// <param name="directory">文件夹</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <param name="saveObject">保存数据类</param>
|
||||
public static void SaveObjectToJson<TSaveObject>(string directory, string fileName, TSaveObject saveObject)
|
||||
{
|
||||
SaveText(directory, fileName, JsonTool.ToJson(saveObject));
|
||||
}
|
||||
/// <summary>加载Class</summary>
|
||||
/// <typeparam name="TSaveObject">读取的类型</typeparam>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <returns>读取数据类</returns>
|
||||
public static TSaveObject LoadJsonToObject<TSaveObject>(string filePath)
|
||||
{
|
||||
string json = LoadText(filePath);
|
||||
if (json != null) { return JsonTool.FromJson<TSaveObject>(json); }
|
||||
else { return default(TSaveObject); }
|
||||
}
|
||||
#endregion
|
||||
#region 对象Encoding的保存与加载
|
||||
/// <summary>编码保存Object</summary>
|
||||
/// <param name="directory">文件夹</param>
|
||||
/// <param name="fileName">文件名</param>
|
||||
/// <param name="saveObject">保存数据类</param>
|
||||
/// <param name="encodeType">编码类型</param>
|
||||
public static void SaveEncodingObject<TSaveObject>(string directory, string fileName, TSaveObject saveObject, Encoding encodeType)
|
||||
{
|
||||
SaveEncodingString(directory, fileName, JsonTool.ToJson(saveObject), encodeType);
|
||||
}
|
||||
/// <summary>加载编码Object</summary>
|
||||
/// <typeparam name="TSaveObject">读取的类型</typeparam>
|
||||
/// <param name="filePath">文件路径</param>
|
||||
/// <param name="encodeType">编码类型</param>
|
||||
/// <returns>读取数据类</returns>
|
||||
public static TSaveObject LoadEncodingObject<TSaveObject>(string filePath, Encoding encodeType)
|
||||
{
|
||||
string json = LoadEncodingString(filePath, encodeType);
|
||||
if (json != null) { return JsonTool.FromJson<TSaveObject>(json); }
|
||||
else { return default(TSaveObject); }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc926ac96d06fc54da01608d8821318d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "muhua-tools",
|
||||
"version": "1.0.0",
|
||||
"displayName": "MuHua Tools",
|
||||
"description": "\u57fa\u7840\u5de5\u5177\u5305",
|
||||
"author": {
|
||||
"name": "MuHua",
|
||||
"email": "muhua233@qq.com"
|
||||
},
|
||||
"type": "tool"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 803db43165f26c84f96ee139c8fda321
|
||||
PackageManifestImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -233,6 +233,12 @@
|
||||
"source": "embedded",
|
||||
"dependencies": {}
|
||||
},
|
||||
"muhua-tools": {
|
||||
"version": "file:Tools",
|
||||
"depth": 0,
|
||||
"source": "embedded",
|
||||
"dependencies": {}
|
||||
},
|
||||
"muhua-ui-tool": {
|
||||
"version": "file:UITool",
|
||||
"depth": 0,
|
||||
|
||||
Reference in New Issue
Block a user