工具类重新整合
This commit is contained in:
parent
1a9a59ab07
commit
7ae4ddac09
@ -5,8 +5,15 @@ namespace UnityEngine
|
||||
/// <summary>
|
||||
/// <see cref="UnityEngine.Component"/>.
|
||||
/// </summary>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static class ComponentExtensions
|
||||
{
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void DestroyComponent(this Component component)
|
||||
{
|
||||
component.SafeDestroySelf();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从目标组件中获取一个组件,如果是组件类型不存在,则添加
|
||||
/// </summary>
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace UnityEngine
|
||||
{
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static class GameObjectExtensions
|
||||
{
|
||||
public static void SafeDestroySelf(
|
||||
@ -107,5 +109,120 @@ namespace UnityEngine
|
||||
}
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void RemoveChildren(this GameObject gameObject)
|
||||
{
|
||||
for (var i = gameObject.transform.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
gameObject.transform.GetChild(i).gameObject.DestroyObject();
|
||||
}
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void DestroyObject(this GameObject gameObject)
|
||||
{
|
||||
gameObject.SafeDestroySelf();
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Destroy(this GameObject gameObject)
|
||||
{
|
||||
gameObject.DestroyObject();
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject FindChildGamObjectByName(string nodeName, string sceneName = null)
|
||||
{
|
||||
Scene scene;
|
||||
if (string.IsNullOrWhiteSpace(sceneName))
|
||||
{
|
||||
scene = SceneManager.GetActiveScene();
|
||||
}
|
||||
else
|
||||
{
|
||||
scene = SceneManager.GetSceneByName(sceneName);
|
||||
if (!scene.isLoaded)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var rootObjects = scene.GetRootGameObjects();
|
||||
foreach (var rootObject in rootObjects)
|
||||
{
|
||||
var result = rootObject.FindChildGamObjectByName(nodeName);
|
||||
if (result.IsNotNull())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject FindChildGamObjectByName(this GameObject gameObject, string name)
|
||||
{
|
||||
var transform = gameObject.transform.FindChildName(name);
|
||||
if (transform.IsNotNull())
|
||||
{
|
||||
return transform.gameObject;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject Create(this Transform parent, string name)
|
||||
{
|
||||
Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null");
|
||||
var gameObject = new GameObject(name);
|
||||
gameObject.transform.SetParent(parent);
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject Create(this GameObject parent, string name)
|
||||
{
|
||||
Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null");
|
||||
return parent.transform.Create(name);
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void ResetTransform(this GameObject gameObject)
|
||||
{
|
||||
gameObject.transform.localScale = Vector3.one;
|
||||
gameObject.transform.localPosition = Vector3.zero;
|
||||
gameObject.transform.localRotation = Quaternion.identity;
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void SetSortingGroupLayer(this GameObject gameObject, string sortingLayer)
|
||||
{
|
||||
SortingGroup[] sortingGroups = gameObject.GetComponentsInChildren<SortingGroup>();
|
||||
foreach (SortingGroup sortingGroup in sortingGroups)
|
||||
{
|
||||
sortingGroup.sortingLayerName = sortingLayer;
|
||||
}
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void SetLayer(this GameObject gameObject, int layer, bool children = true)
|
||||
{
|
||||
if (gameObject.layer != layer)
|
||||
{
|
||||
gameObject.layer = layer;
|
||||
}
|
||||
|
||||
if (children)
|
||||
{
|
||||
Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
|
||||
foreach (var transform in transforms)
|
||||
{
|
||||
transform.gameObject.layer = layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace AlicizaX
|
||||
{
|
||||
/// <summary>
|
||||
/// 相机帮助类
|
||||
/// </summary>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static class CameraHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取相机快照
|
||||
/// </summary>
|
||||
/// <param name="main">相机</param>
|
||||
/// <param name="scale">缩放比</param>
|
||||
public static Texture2D GetCaptureScreenshot(Camera main, float scale = 0.5f)
|
||||
{
|
||||
Rect rect = new Rect(0, 0, Screen.width * scale, Screen.height * scale);
|
||||
string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
|
||||
RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width, (int)rect.height, 0);
|
||||
renderTexture.name = SceneManager.GetActiveScene().name + "_" + renderTexture.width + "_" + renderTexture.height + "_" + name;
|
||||
main.targetTexture = renderTexture;
|
||||
main.Render();
|
||||
|
||||
RenderTexture.active = renderTexture;
|
||||
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false)
|
||||
{
|
||||
name = renderTexture.name
|
||||
};
|
||||
screenShot.ReadPixels(rect, 0, 0);
|
||||
screenShot.Apply();
|
||||
main.targetTexture = null;
|
||||
RenderTexture.active = null;
|
||||
RenderTexture.ReleaseTemporary(renderTexture);
|
||||
return screenShot;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6f0c95931944448a8c8f1790d20059a
|
||||
timeCreated: 1663323744
|
||||
@ -1,321 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AlicizaX
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件帮助类
|
||||
/// </summary>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static class FileHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取目录下的所有文件
|
||||
/// </summary>
|
||||
/// <param name="files">文件存放路径列表对象</param>
|
||||
/// <param name="dir">目标目录</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void GetAllFiles(List<string> files, string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string[] strings = Directory.GetFiles(dir);
|
||||
foreach (string item in strings)
|
||||
{
|
||||
files.Add(item);
|
||||
}
|
||||
|
||||
string[] subDirs = Directory.GetDirectories(dir);
|
||||
foreach (string subDir in subDirs)
|
||||
{
|
||||
GetAllFiles(files, subDir);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理目录
|
||||
/// </summary>
|
||||
/// <param name="dir">目标路径</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void CleanDirectory(string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string subDir in Directory.GetDirectories(dir))
|
||||
{
|
||||
Directory.Delete(subDir, true);
|
||||
}
|
||||
|
||||
foreach (string subFile in Directory.GetFiles(dir))
|
||||
{
|
||||
File.Delete(subFile);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 目录复制
|
||||
/// </summary>
|
||||
/// <param name="srcDir">源路径</param>
|
||||
/// <param name="targetDir">目标路径</param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void CopyDirectory(string srcDir, string targetDir)
|
||||
{
|
||||
DirectoryInfo source = new DirectoryInfo(srcDir);
|
||||
DirectoryInfo target = new DirectoryInfo(targetDir);
|
||||
|
||||
if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
throw new Exception("父目录不能拷贝到子目录!");
|
||||
}
|
||||
|
||||
if (!source.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!target.Exists)
|
||||
{
|
||||
target.Create();
|
||||
}
|
||||
|
||||
FileInfo[] files = source.GetFiles();
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
|
||||
}
|
||||
|
||||
DirectoryInfo[] dirs = source.GetDirectories();
|
||||
|
||||
for (int j = 0; j < dirs.Length; j++)
|
||||
{
|
||||
CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复制文件到目标目录
|
||||
/// </summary>
|
||||
/// <param name="sourceFileName">源路径</param>
|
||||
/// <param name="destFileName">目标路径</param>
|
||||
/// <param name="overwrite">是否覆盖</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Copy(string sourceFileName, string destFileName, bool overwrite = false)
|
||||
{
|
||||
if (!File.Exists(sourceFileName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
File.Copy(sourceFileName, destFileName, overwrite);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除文件
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Delete(string path)
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断文件是否存在
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static bool IsExists(string path)
|
||||
{
|
||||
#if ENABLE_GAME_FRAME_X_READ_ASSETS
|
||||
if (IsAndroidReadOnlyPath(path, out var readPath))
|
||||
{
|
||||
return BlankReadAssets.BlankReadAssets.IsFileExists(readPath);
|
||||
}
|
||||
#endif
|
||||
return File.Exists(path);
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
private static bool IsAndroidReadOnlyPath(string path, out string readPath)
|
||||
{
|
||||
if (Application.platform == RuntimePlatform.Android)
|
||||
{
|
||||
if (Utility.Path.NormalizePath(path).Contains(Utility.Path.AppResPath))
|
||||
{
|
||||
readPath = path.Substring(Utility.Path.AppResPath.Length);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
readPath = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移动文件到目标目录
|
||||
/// </summary>
|
||||
/// <param name="sourceFileName">文件源路径</param>
|
||||
/// <param name="destFileName">目标路径</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Move(string sourceFileName, string destFileName)
|
||||
{
|
||||
if (!File.Exists(sourceFileName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Copy(sourceFileName, destFileName, true);
|
||||
Delete(sourceFileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static byte[] ReadAllBytes(string path)
|
||||
{
|
||||
#if ENABLE_GAME_FRAME_X_READ_ASSETS
|
||||
if (IsAndroidReadOnlyPath((path), out var readPath))
|
||||
{
|
||||
return BlankReadAssets.BlankReadAssets.Read(readPath);
|
||||
}
|
||||
#endif
|
||||
|
||||
return File.ReadAllBytes(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string ReadAllText(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadAllText(path, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string ReadAllText(string path)
|
||||
{
|
||||
return File.ReadAllText(path, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string[] ReadAllLines(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadAllLines(path, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string[] ReadAllLines(string path)
|
||||
{
|
||||
return File.ReadAllLines(path, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="buffer">写入内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void ReadAllLines(string path, byte[] buffer)
|
||||
{
|
||||
File.WriteAllBytes(path, buffer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="lines">写入的内容</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllLines(string path, string[] lines, Encoding encoding)
|
||||
{
|
||||
File.WriteAllLines(path, lines, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="lines">写入的内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllLines(string path, string[] lines)
|
||||
{
|
||||
File.WriteAllLines(path, lines, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="content">写入的内容</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllText(string path, string content, Encoding encoding)
|
||||
{
|
||||
File.WriteAllText(path, content, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容,UTF-8
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="content">写入的内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllText(string path, string content)
|
||||
{
|
||||
File.WriteAllText(path, content, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="buffer">写入的内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllBytes(string path, byte[] buffer)
|
||||
{
|
||||
File.WriteAllBytes(path, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 74cc512af7309484fa066b16175c6331
|
||||
timeCreated: 1474943113
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -77,15 +77,6 @@ namespace UnityEngine
|
||||
/// </summary>
|
||||
public static string GetGuid() => System.Guid.NewGuid().ToString("N");
|
||||
|
||||
/// <summary>
|
||||
/// Change Cursor States.
|
||||
/// </summary>
|
||||
public static void ShowCursor(bool locked, bool visible)
|
||||
{
|
||||
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
Cursor.visible = visible;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change the GameObject layer including all children.
|
||||
/// </summary>
|
||||
|
||||
@ -1,205 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace UnityEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 游戏对象帮助类
|
||||
/// </summary>
|
||||
public static class GameObjectHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 销毁子物体
|
||||
/// </summary>
|
||||
/// <param name="go"></param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void RemoveChildren(GameObject go)
|
||||
{
|
||||
for (var i = go.transform.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Destroy(go.transform.GetChild(i).gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁游戏物体
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void DestroyObject(this GameObject gameObject)
|
||||
{
|
||||
if (!ReferenceEquals(gameObject, null))
|
||||
{
|
||||
if (Application.isEditor && !Application.isPlaying)
|
||||
{
|
||||
Object.DestroyImmediate(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Object.Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁游戏物体
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
public static void Destroy(GameObject gameObject)
|
||||
{
|
||||
gameObject.DestroyObject();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁游戏组件
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void DestroyComponent(UnityEngine.Component component)
|
||||
{
|
||||
if (!ReferenceEquals(component, null))
|
||||
{
|
||||
if (Application.isEditor && !Application.isPlaying)
|
||||
{
|
||||
Object.DestroyImmediate(component);
|
||||
return;
|
||||
}
|
||||
|
||||
Object.Destroy(component);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在指定场景中查找特定名称的节点。
|
||||
/// </summary>
|
||||
/// <param name="sceneName">场景名称。</param>
|
||||
/// <param name="nodeName">节点名称。</param>
|
||||
/// <returns>找到的节点的GameObject实例,如果没有找到返回null。</returns>
|
||||
public static GameObject FindChildGamObjectByName(string nodeName, string sceneName = null)
|
||||
{
|
||||
UnityEngine.SceneManagement.Scene scene;
|
||||
if (sceneName.IsNullOrWhiteSpace())
|
||||
{
|
||||
scene = SceneManager.GetActiveScene();
|
||||
}
|
||||
else
|
||||
{
|
||||
scene = SceneManager.GetSceneByName(sceneName);
|
||||
if (!scene.isLoaded)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var rootObjects = scene.GetRootGameObjects();
|
||||
foreach (var rootObject in rootObjects)
|
||||
{
|
||||
var result = FindChildGamObjectByName(rootObject, nodeName);
|
||||
if (result.IsNotNull())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据游戏对象名称查询子对象
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject FindChildGamObjectByName(GameObject gameObject, string name)
|
||||
{
|
||||
var transform = gameObject.transform.FindChildName(name);
|
||||
if (transform.IsNotNull())
|
||||
{
|
||||
return transform.gameObject;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建游戏对象
|
||||
/// </summary>
|
||||
/// <param name="parent"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject Create(Transform parent, string name)
|
||||
{
|
||||
Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null");
|
||||
var gameObject = new GameObject(name);
|
||||
gameObject.transform.SetParent(parent);
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建游戏对象
|
||||
/// </summary>
|
||||
/// <param name="parent"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static GameObject Create(GameObject parent, string name)
|
||||
{
|
||||
Debug.Assert(!ReferenceEquals(parent, null), nameof(parent) + " == null");
|
||||
return Create(parent.transform, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置游戏对象的变换数据
|
||||
/// </summary>
|
||||
/// <param name="gameObject"></param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void ResetTransform(GameObject gameObject)
|
||||
{
|
||||
gameObject.transform.localScale = Vector3.one;
|
||||
gameObject.transform.localPosition = Vector3.zero;
|
||||
gameObject.transform.localRotation = Quaternion.identity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置对象的显示排序层
|
||||
/// </summary>
|
||||
/// <param name="gameObject">游戏对象</param>
|
||||
/// <param name="sortingLayer">显示层</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void SetSortingGroupLayer(GameObject gameObject, string sortingLayer)
|
||||
{
|
||||
SortingGroup[] sortingGroups = gameObject.GetComponentsInChildren<SortingGroup>();
|
||||
foreach (SortingGroup sg in sortingGroups)
|
||||
{
|
||||
sg.sortingLayerName = sortingLayer;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置对象的层
|
||||
/// </summary>
|
||||
/// <param name="gameObject">游戏对象</param>
|
||||
/// <param name="layer">层</param>
|
||||
/// <param name="children">是否设置子物体</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void SetLayer(GameObject gameObject, int layer, bool children = true)
|
||||
{
|
||||
if (gameObject.layer != layer)
|
||||
{
|
||||
gameObject.layer = layer;
|
||||
}
|
||||
|
||||
if (children)
|
||||
{
|
||||
Transform[] transforms = gameObject.GetComponentsInChildren<Transform>();
|
||||
foreach (var sg in transforms)
|
||||
{
|
||||
sg.gameObject.layer = layer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 998123e39155448eb8a773436d34eac8
|
||||
timeCreated: 1666446475
|
||||
@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AlicizaX
|
||||
{
|
||||
/// <summary>
|
||||
/// 数学帮助类
|
||||
/// </summary>
|
||||
public static class MathHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 检查两个矩形是否相交
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CheckIntersect(RectInt src, RectInt target)
|
||||
{
|
||||
int minX = Math.Max(src.x, target.x);
|
||||
int minY = Math.Max(src.y, target.y);
|
||||
int maxX = Math.Min(src.x + src.width, target.x + target.width);
|
||||
int maxY = Math.Min(src.y + src.height, target.y + target.height);
|
||||
if (minX >= maxX || minY >= maxY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形是否相交
|
||||
/// </summary>
|
||||
/// <param name="x1"></param>
|
||||
/// <param name="y1"></param>
|
||||
/// <param name="w1"></param>
|
||||
/// <param name="h1"></param>
|
||||
/// <param name="x2"></param>
|
||||
/// <param name="y2"></param>
|
||||
/// <param name="w2"></param>
|
||||
/// <param name="h2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
|
||||
{
|
||||
int minX = Math.Max(x1, x2);
|
||||
int minY = Math.Max(y1, y2);
|
||||
int maxX = Math.Min(x1 + w1, x2 + w2);
|
||||
int maxY = Math.Min(y1 + h1, y2 + h2);
|
||||
if (minX >= maxX || minY >= maxY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形是否相交,并返回相交的区域
|
||||
/// </summary>
|
||||
/// <param name="x1"></param>
|
||||
/// <param name="y1"></param>
|
||||
/// <param name="w1"></param>
|
||||
/// <param name="h1"></param>
|
||||
/// <param name="x2"></param>
|
||||
/// <param name="y2"></param>
|
||||
/// <param name="w2"></param>
|
||||
/// <param name="h2"></param>
|
||||
/// <param name="rect"></param>
|
||||
/// <returns></returns>
|
||||
private static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, out RectInt rect)
|
||||
{
|
||||
rect = default;
|
||||
int minX = Math.Max(x1, x2);
|
||||
int minY = Math.Max(y1, y2);
|
||||
int maxX = Math.Min(x1 + w1, x2 + w2);
|
||||
int maxY = Math.Min(y1 + h1, y2 + h2);
|
||||
if (minX >= maxX || minY >= maxY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
rect.x = minX;
|
||||
rect.y = minY;
|
||||
rect.width = Math.Abs(maxX - minX);
|
||||
rect.height = Math.Abs(maxY - minY);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形相交的点
|
||||
/// </summary>
|
||||
/// <param name="x1">A 坐标X</param>
|
||||
/// <param name="y1">A 坐标Y</param>
|
||||
/// <param name="w1">A 宽度</param>
|
||||
/// <param name="h1">A 高度</param>
|
||||
/// <param name="x2">B 坐标X</param>
|
||||
/// <param name="y2">B 坐标Y</param>
|
||||
/// <param name="w2">B 宽度</param>
|
||||
/// <param name="h2">B 高度</param>
|
||||
/// <param name="intersectPoints">交叉点列表</param>
|
||||
/// <returns>返回是否相交</returns>
|
||||
public static bool CheckIntersectPoints(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, int[] intersectPoints)
|
||||
{
|
||||
Vector2Int dPt = new Vector2Int();
|
||||
|
||||
if (false == CheckIntersect(x1, y1, w1, h1, x2, y2, w2, h2, out var rectInt))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < w1; i++)
|
||||
{
|
||||
for (var n = 0; n < h1; n++)
|
||||
{
|
||||
if (intersectPoints[i * h1 + n] == 1)
|
||||
{
|
||||
dPt.x = x1 + i;
|
||||
dPt.y = y1 + n;
|
||||
if (rectInt.Contains(dPt))
|
||||
{
|
||||
intersectPoints[i * h1 + n] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c10ee3753d16449f96d506192b4652c6
|
||||
timeCreated: 1670988225
|
||||
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace AlicizaX
|
||||
{
|
||||
@ -30,5 +32,32 @@ namespace AlicizaX
|
||||
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
|
||||
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取相机快照
|
||||
/// </summary>
|
||||
/// <param name="main">相机</param>
|
||||
/// <param name="scale">缩放比</param>
|
||||
public static Texture2D GetCaptureScreenshot(Camera main, float scale = 0.5f)
|
||||
{
|
||||
Rect rect = new Rect(0, 0, Screen.width * scale, Screen.height * scale);
|
||||
string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
|
||||
RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width, (int)rect.height, 0);
|
||||
renderTexture.name = SceneManager.GetActiveScene().name + "_" + renderTexture.width + "_" + renderTexture.height + "_" + name;
|
||||
main.targetTexture = renderTexture;
|
||||
main.Render();
|
||||
|
||||
RenderTexture.active = renderTexture;
|
||||
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false)
|
||||
{
|
||||
name = renderTexture.name
|
||||
};
|
||||
screenShot.ReadPixels(rect, 0, 0);
|
||||
screenShot.Apply();
|
||||
main.targetTexture = null;
|
||||
RenderTexture.active = null;
|
||||
RenderTexture.ReleaseTemporary(renderTexture);
|
||||
return screenShot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,10 @@
|
||||
namespace AlicizaX
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AlicizaX
|
||||
{
|
||||
public static partial class Utility
|
||||
{
|
||||
@ -42,6 +48,311 @@
|
||||
return length < 1024 * 1024 * 1024 ? $"{(length / 1024f / 1024f):F2} MB" : $"{(length / 1024f / 1024f / 1024f):F2} GB";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取目录下的所有文件
|
||||
/// </summary>
|
||||
/// <param name="files">文件存放路径列表对象</param>
|
||||
/// <param name="dir">目标目录</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void GetAllFiles(List<string> files, string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string[] strings = Directory.GetFiles(dir);
|
||||
foreach (string item in strings)
|
||||
{
|
||||
files.Add(item);
|
||||
}
|
||||
|
||||
string[] subDirs = Directory.GetDirectories(dir);
|
||||
foreach (string subDir in subDirs)
|
||||
{
|
||||
GetAllFiles(files, subDir);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理目录
|
||||
/// </summary>
|
||||
/// <param name="dir">目标路径</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void CleanDirectory(string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (string subDir in Directory.GetDirectories(dir))
|
||||
{
|
||||
Directory.Delete(subDir, true);
|
||||
}
|
||||
|
||||
foreach (string subFile in Directory.GetFiles(dir))
|
||||
{
|
||||
File.Delete(subFile);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 目录复制
|
||||
/// </summary>
|
||||
/// <param name="srcDir">源路径</param>
|
||||
/// <param name="targetDir">目标路径</param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void CopyDirectory(string srcDir, string targetDir)
|
||||
{
|
||||
DirectoryInfo source = new DirectoryInfo(srcDir);
|
||||
DirectoryInfo target = new DirectoryInfo(targetDir);
|
||||
|
||||
if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
throw new Exception("父目录不能拷贝到子目录!");
|
||||
}
|
||||
|
||||
if (!source.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!target.Exists)
|
||||
{
|
||||
target.Create();
|
||||
}
|
||||
|
||||
FileInfo[] files = source.GetFiles();
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
|
||||
}
|
||||
|
||||
DirectoryInfo[] dirs = source.GetDirectories();
|
||||
|
||||
for (int j = 0; j < dirs.Length; j++)
|
||||
{
|
||||
CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 复制文件到目标目录
|
||||
/// </summary>
|
||||
/// <param name="sourceFileName">源路径</param>
|
||||
/// <param name="destFileName">目标路径</param>
|
||||
/// <param name="overwrite">是否覆盖</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Copy(string sourceFileName, string destFileName, bool overwrite = false)
|
||||
{
|
||||
if (!System.IO.File.Exists(sourceFileName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
File.Copy(sourceFileName, destFileName, overwrite);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除文件
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Delete(string path)
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断文件是否存在
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static bool IsExists(string path)
|
||||
{
|
||||
#if ENABLE_GAME_FRAME_X_READ_ASSETS
|
||||
if (IsAndroidReadOnlyPath(path, out var readPath))
|
||||
{
|
||||
return BlankReadAssets.BlankReadAssets.IsFileExists(readPath);
|
||||
}
|
||||
#endif
|
||||
return System.IO.File.Exists(path);
|
||||
}
|
||||
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
private static bool IsAndroidReadOnlyPath(string path, out string readPath)
|
||||
{
|
||||
if (Application.platform == RuntimePlatform.Android)
|
||||
{
|
||||
if (Utility.Path.NormalizePath(path).Contains(Utility.Path.AppResPath))
|
||||
{
|
||||
readPath = path.Substring(Utility.Path.AppResPath.Length);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
readPath = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移动文件到目标目录
|
||||
/// </summary>
|
||||
/// <param name="sourceFileName">文件源路径</param>
|
||||
/// <param name="destFileName">目标路径</param>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void Move(string sourceFileName, string destFileName)
|
||||
{
|
||||
if (!System.IO.File.Exists(sourceFileName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Copy(sourceFileName, destFileName, true);
|
||||
Delete(sourceFileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static byte[] ReadAllBytes(string path)
|
||||
{
|
||||
#if ENABLE_GAME_FRAME_X_READ_ASSETS
|
||||
if (IsAndroidReadOnlyPath((path), out var readPath))
|
||||
{
|
||||
return BlankReadAssets.BlankReadAssets.Read(readPath);
|
||||
}
|
||||
#endif
|
||||
|
||||
return File.ReadAllBytes(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string ReadAllText(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadAllText(path, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string ReadAllText(string path)
|
||||
{
|
||||
return File.ReadAllText(path, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string[] ReadAllLines(string path, Encoding encoding)
|
||||
{
|
||||
return File.ReadAllLines(path, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static string[] ReadAllLines(string path)
|
||||
{
|
||||
return File.ReadAllLines(path, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="buffer">写入内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void ReadAllLines(string path, byte[] buffer)
|
||||
{
|
||||
File.WriteAllBytes(path, buffer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="lines">写入的内容</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllLines(string path, string[] lines, Encoding encoding)
|
||||
{
|
||||
File.WriteAllLines(path, lines, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="lines">写入的内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllLines(string path, string[] lines)
|
||||
{
|
||||
File.WriteAllLines(path, lines, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="content">写入的内容</param>
|
||||
/// <param name="encoding">编码</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllText(string path, string content, Encoding encoding)
|
||||
{
|
||||
File.WriteAllText(path, content, encoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容,UTF-8
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="content">写入的内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllText(string path, string content)
|
||||
{
|
||||
File.WriteAllText(path, content, Encoding.UTF8);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入指定路径的文件内容
|
||||
/// </summary>
|
||||
/// <param name="path">文件路径</param>
|
||||
/// <param name="buffer">写入的内容</param>
|
||||
/// <returns></returns>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public static void WriteAllBytes(string path, byte[] buffer)
|
||||
{
|
||||
File.WriteAllBytes(path, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -178,6 +178,125 @@ namespace AlicizaX
|
||||
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形是否相交
|
||||
/// </summary>
|
||||
/// <param name="src"></param>
|
||||
/// <param name="target"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CheckIntersect(RectInt src, RectInt target)
|
||||
{
|
||||
int minX = System.Math.Max(src.x, target.x);
|
||||
int minY = System.Math.Max(src.y, target.y);
|
||||
int maxX = System.Math.Min(src.x + src.width, target.x + target.width);
|
||||
int maxY = System.Math.Min(src.y + src.height, target.y + target.height);
|
||||
if (minX >= maxX || minY >= maxY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形是否相交
|
||||
/// </summary>
|
||||
/// <param name="x1"></param>
|
||||
/// <param name="y1"></param>
|
||||
/// <param name="w1"></param>
|
||||
/// <param name="h1"></param>
|
||||
/// <param name="x2"></param>
|
||||
/// <param name="y2"></param>
|
||||
/// <param name="w2"></param>
|
||||
/// <param name="h2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
|
||||
{
|
||||
int minX = System.Math.Max(x1, x2);
|
||||
int minY = System.Math.Max(y1, y2);
|
||||
int maxX = System.Math.Min(x1 + w1, x2 + w2);
|
||||
int maxY = System.Math.Min(y1 + h1, y2 + h2);
|
||||
if (minX >= maxX || minY >= maxY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形是否相交,并返回相交的区域
|
||||
/// </summary>
|
||||
/// <param name="x1"></param>
|
||||
/// <param name="y1"></param>
|
||||
/// <param name="w1"></param>
|
||||
/// <param name="h1"></param>
|
||||
/// <param name="x2"></param>
|
||||
/// <param name="y2"></param>
|
||||
/// <param name="w2"></param>
|
||||
/// <param name="h2"></param>
|
||||
/// <param name="rect"></param>
|
||||
/// <returns></returns>
|
||||
private static bool CheckIntersect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, out RectInt rect)
|
||||
{
|
||||
rect = default;
|
||||
int minX = System.Math.Max(x1, x2);
|
||||
int minY = System.Math.Max(y1, y2);
|
||||
int maxX = System.Math.Min(x1 + w1, x2 + w2);
|
||||
int maxY = System.Math.Min(y1 + h1, y2 + h2);
|
||||
if (minX >= maxX || minY >= maxY)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
rect.x = minX;
|
||||
rect.y = minY;
|
||||
rect.width = System.Math.Abs(maxX - minX);
|
||||
rect.height = System.Math.Abs(maxY - minY);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查两个矩形相交的点
|
||||
/// </summary>
|
||||
/// <param name="x1">A 坐标X</param>
|
||||
/// <param name="y1">A 坐标Y</param>
|
||||
/// <param name="w1">A 宽度</param>
|
||||
/// <param name="h1">A 高度</param>
|
||||
/// <param name="x2">B 坐标X</param>
|
||||
/// <param name="y2">B 坐标Y</param>
|
||||
/// <param name="w2">B 宽度</param>
|
||||
/// <param name="h2">B 高度</param>
|
||||
/// <param name="intersectPoints">交叉点列表</param>
|
||||
/// <returns>返回是否相交</returns>
|
||||
public static bool CheckIntersectPoints(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2, int[] intersectPoints)
|
||||
{
|
||||
Vector2Int dPt = new Vector2Int();
|
||||
|
||||
if (false == CheckIntersect(x1, y1, w1, h1, x2, y2, w2, h2, out var rectInt))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < w1; i++)
|
||||
{
|
||||
for (var n = 0; n < h1; n++)
|
||||
{
|
||||
if (intersectPoints[i * h1 + n] == 1)
|
||||
{
|
||||
dPt.x = x1 + i;
|
||||
dPt.y = y1 + n;
|
||||
if (rectInt.Contains(dPt))
|
||||
{
|
||||
intersectPoints[i * h1 + n] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace AlicizaX
|
||||
{
|
||||
@ -101,6 +104,7 @@ namespace AlicizaX
|
||||
|
||||
return PlayOneShot3D(position, clip.audioClip, clip.volume, name);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user