EditorTool3D/Assets/ZXL/Scripts/Helper/CommonHelper.cs

145 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using Object = UnityEngine.Object;
namespace ZXL.Helper
{
public static class CommonHelper
{
/// <summary>
/// 遍历更改所有子物体的layer
/// </summary>
/// <param name="self"></param>
/// <param name="layer"></param>
public static void SetWithChildLayer(this Transform self, LayerMask layer)
{
List<Transform> findDeep = new List<Transform>();
FindDeeps(self, ref findDeep);
if (findDeep.Count == 0)
{
Debug.LogError("未找到此组件");
}
foreach (var gameObject in findDeep)
{
gameObject.gameObject.layer = layer;
}
}
/// <summary>
/// 遍历查找并返回名字对应的物体
/// </summary>
/// <param name="self"></param>
/// <param name="name"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T FindChildDeep<T>(this Transform self, string name) where T : Object
{
Transform findDeep = null;
FindDeep(self, name, ref findDeep);
if (findDeep == null)
{
Debug.LogError("未找到此组件");
}
var component = findDeep.GetComponent<T>();
return component;
}
static void FindDeep(Transform tran, string name, ref Transform transform)
{
if (tran.name == name)
{
transform = tran;
return;
}
for (var i = 0; i < tran.childCount; i++)
{
FindDeep(tran.GetChild(i), name, ref transform);
}
}
public static List<T> FindChildDeeps<T>(this Transform self) where T : Object
{
List<T> list = new List<T>();
FindDeeps(self, ref list);
if (list.Count <= 0)
{
Debug.LogError("未找到此组件");
}
return list;
}
static void FindDeeps<T>(Transform tran, ref List<T> list)
{
var component = tran.GetComponent<T>();
if (component != null)
list.Add(component);
for (var i = 0; i < tran.childCount; i++)
{
FindDeeps(tran.GetChild(i), ref list);
}
}
/// <summary>
/// 判断是否在某个物体范围内范围型判断超出返回false
/// 基于MeshRender的实现方式
/// </summary>
/// <param name="tran"></param>
/// <param name="self"></param>
/// <returns></returns>
public static bool IsInRangeAutoFix(Transform tran, Transform self)
{
var meshRenderer = tran.GetComponent<MeshRenderer>();
Bounds rendererBounds = self.GetComponent<MeshRenderer>().bounds;
Bounds colliderBounds = meshRenderer.bounds;
bool rendererIsInsideBox = colliderBounds.Intersects(rendererBounds);
return rendererIsInsideBox;
}
/// <summary>
/// 调用win的消息弹窗
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="okAction"></param>
/// <param name="cancelAction"></param>
/// <param name="type"></param>
public static void MessageBox(string title, string content, Action okAction, Action cancelAction,
MessageBoxType type = MessageBoxType.)
{
var messageBox = MessageBox(IntPtr.Zero, content, title, (uint)type);
Debug.Log(messageBox);
if (messageBox == 1)
{
okAction?.Invoke();
return;
}
cancelAction?.Invoke();
}
[DllImport("User32.dll", CharSet = CharSet.Unicode, EntryPoint = "MessageBox")]
static extern int MessageBox(
IntPtr hWnd,
[MarshalAs(UnmanagedType.LPTStr)] string lpText,
[MarshalAs(UnmanagedType.LPTStr)] string lpCaption,
uint uType
);
public enum MessageBoxType : uint
{
= (uint)0x00000000L,
= (uint)0x00000001L,
= (uint)0x00000004L,
= (uint)0x00000003L,
}
}
}