304 lines
9.8 KiB
C#
304 lines
9.8 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine.UI;
|
||
using System.Net.NetworkInformation;
|
||
using System.Net.Sockets;
|
||
using System.IO;
|
||
using DG.Tweening;
|
||
using DG.Tweening.Core;
|
||
using DG.Tweening.Plugins.Options;
|
||
|
||
namespace BF
|
||
{
|
||
public static class Utils
|
||
{
|
||
private const uint HASH_SEED = 31;
|
||
public const string FORWARD_SLASH = "/";
|
||
|
||
private static Regex RegexMatchPath;
|
||
|
||
private static bool c_free_flag = false;
|
||
private static bool c_config_flag = false;
|
||
private static string DefaultSpecialPatternStr = "[【】\\[\\]\\^*×―%@@##$…&¥=<>《》::•`·~;;\\\\/\'\"{}‘’“”]";
|
||
public static string CustomSpecialPatternStr = "[【】\\[\\]\\^*×―%@@##$…&¥=<>《》::•`·~;;\\\\/\'\"{}‘’“”]";
|
||
|
||
public static char[] GetFreeChar()
|
||
{
|
||
if (!c_free_flag)
|
||
{
|
||
c_free_flag = true;
|
||
for (int i = 0; i < 16; ++i)
|
||
{
|
||
GameConst.C_FREE_CHAR[i] = (char)(GameConst.C_FREE_CHAR[i] ^ (0x29 - i));
|
||
}
|
||
}
|
||
return GameConst.C_FREE_CHAR;
|
||
}
|
||
|
||
public static char[] GetConfigChar()
|
||
{
|
||
if (!c_config_flag)
|
||
{
|
||
c_config_flag = true;
|
||
for (int i = 0; i < 13; ++i)
|
||
{
|
||
GameConst.C_CONFIG_CHAR[i] = (char)(GameConst.C_CONFIG_CHAR[i] ^ (0x32 - i));
|
||
}
|
||
}
|
||
return GameConst.C_CONFIG_CHAR;
|
||
}
|
||
|
||
public static bool IsNull(UnityEngine.Object o) // 或者名字叫IsDestroyed等等
|
||
{
|
||
return o == null;
|
||
}
|
||
|
||
public static Component BFGetComponent(this GameObject go, Type componentType)
|
||
{
|
||
var com = go.GetComponent(componentType);
|
||
if (com)
|
||
{
|
||
return com;
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
public static void GetTransformPath(Transform trans, Transform parentTrans, ref string path)
|
||
{
|
||
if (path == "")
|
||
path = trans.name;
|
||
else
|
||
path = trans.name + FORWARD_SLASH + path;
|
||
if (trans.parent != null && trans != parentTrans)
|
||
GetTransformPath(trans.parent, parentTrans, ref path);
|
||
}
|
||
|
||
public static T GetLuaInPath<T>(string path)
|
||
{
|
||
var ret = BFMain.Instance.LuaMgr.luaEnv.Global.GetInPath<T>(path);
|
||
return ret;
|
||
}
|
||
|
||
public static string GetAssetPathByFullPath(string fullPath)
|
||
{
|
||
if (null == RegexMatchPath)
|
||
RegexMatchPath = new Regex("(.+?)(Assets/.+)");
|
||
|
||
fullPath = fullPath.Replace("\\", "/");
|
||
return RegexMatchPath.Replace(fullPath, "$2");
|
||
}
|
||
|
||
public static Vector2 RectTransformScreenPointToLocalPointInRectangle(RectTransform rt, float x, float y, Camera cam)
|
||
{
|
||
Vector2 lp;
|
||
RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, new Vector2(x, y), cam, out lp);
|
||
return lp;
|
||
}
|
||
|
||
public static Vector3 LocalPointInRectangleToWorld(RectTransform rt, float planeZ, Camera uiCam, Camera main)
|
||
{
|
||
var screenPoint = uiCam.WorldToScreenPoint(rt.transform.position);
|
||
Vector3 position = new Vector3(screenPoint.x, screenPoint.y, 0);
|
||
Vector3 result = main.ScreenToWorldPoint(position);
|
||
result.z = planeZ;
|
||
return result;
|
||
}
|
||
|
||
public static bool RectangleContainsScreenPoint(RectTransform rt, float x, float y, Camera cam)
|
||
{
|
||
return RectTransformUtility.RectangleContainsScreenPoint(rt, new Vector2(x, y), cam);
|
||
}
|
||
|
||
public static int KillDOTween(int id, bool complete = false)
|
||
{
|
||
return DG.Tweening.DOTween.Kill(id, complete);
|
||
}
|
||
|
||
public static void SetDOTweenTimeScale(int id, float timeScale)
|
||
{
|
||
var list = DG.Tweening.DOTween.TweensById(id);
|
||
if (!ReferenceEquals(list, null))
|
||
{
|
||
int count = list.Count;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
list[i].timeScale = timeScale;
|
||
}
|
||
}
|
||
}
|
||
|
||
public static int NameToLayer(string name)
|
||
{
|
||
return LayerMask.NameToLayer(name);
|
||
}
|
||
|
||
public static void SetLayoutAlignment(LayoutGroup layout, int enum_type)
|
||
{
|
||
if (layout != null)
|
||
{
|
||
layout.childAlignment = (TextAnchor)enum_type;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取Transform的世界坐标
|
||
/// </summary>
|
||
/// <param name="rectTransform"></param>
|
||
/// <param name="canvas"></param>
|
||
/// <returns></returns>
|
||
public static Vector3 TransformPoint2World(Transform transform, Vector3 point)
|
||
{
|
||
return transform.localToWorldMatrix.MultiplyPoint(point);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取Transform的本地坐标
|
||
/// </summary>
|
||
/// <param name="transform"></param>
|
||
/// <param name="point"></param>
|
||
/// <returns></returns>
|
||
public static Vector3 TransformWorld2Point(Transform transform, Vector3 point)
|
||
{
|
||
return transform.worldToLocalMatrix.MultiplyPoint(point);
|
||
}
|
||
|
||
public static string GetIP()
|
||
{
|
||
var output = "";
|
||
foreach (var item in NetworkInterface.GetAllNetworkInterfaces())
|
||
{
|
||
foreach (var ip in item.GetIPProperties().UnicastAddresses)
|
||
{
|
||
//IPv4
|
||
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
|
||
{
|
||
output = ip.Address.ToString();
|
||
}
|
||
}
|
||
}
|
||
return output;
|
||
}
|
||
|
||
public static List<string> GetStringConfigPathList()
|
||
{
|
||
var result = new List<string>();
|
||
#if UNITY_EDITOR
|
||
var luaPath = "Assets/developer/lua/app/config/strings";
|
||
var dirInfo = new DirectoryInfo(luaPath);
|
||
var fileInfos = dirInfo.GetFiles("*.lua", SearchOption.AllDirectories);
|
||
foreach (var file in fileInfos)
|
||
{
|
||
var name = file.FullName.Replace("\\", "/");
|
||
var index = name.IndexOf("Assets/developer/lua/");
|
||
name = name.Substring(index).Replace(".lua", "").Replace("Assets/developer/lua/", "");
|
||
result.Add(name);
|
||
}
|
||
#endif
|
||
return result;
|
||
}
|
||
|
||
public static bool RayCast(Ray ray, float distance, int layer, out RaycastHit raycastHit)
|
||
{
|
||
if (Physics.Raycast(ray, out RaycastHit result, distance, layer))
|
||
{
|
||
raycastHit = result;
|
||
return true;
|
||
}
|
||
raycastHit = new RaycastHit();
|
||
return false;
|
||
}
|
||
|
||
public static uint BKDRHash(string name)
|
||
{
|
||
uint h = 0;
|
||
for (int i = 0; i < name.Length; ++i)
|
||
{
|
||
h = h * HASH_SEED + (byte)name[i];
|
||
}
|
||
return h;
|
||
}
|
||
|
||
public static Color ParseHtmlString(string htmlString)
|
||
{
|
||
Color newCol;
|
||
ColorUtility.TryParseHtmlString(htmlString, out newCol);
|
||
return newCol;
|
||
}
|
||
|
||
public static int GetCurrentAnimationHash(Animator animator, int layer = 0)
|
||
{
|
||
return animator.GetCurrentAnimatorStateInfo(layer).shortNameHash;
|
||
}
|
||
|
||
public static int GetEnvironmentTickCount()
|
||
{
|
||
int tickCount = System.Environment.TickCount;
|
||
return tickCount;
|
||
}
|
||
|
||
public static string GetDataSecretKey()
|
||
{
|
||
string rsaKey = NetUtility.GetRandomString(16);
|
||
return rsaKey;
|
||
}
|
||
|
||
public static long GetRewardNumber(string str)
|
||
{
|
||
var bytes = System.Convert.FromBase64String(str);
|
||
var index = 0;
|
||
var bytesCount = bytes.Length;
|
||
var key = GetConfigChar();
|
||
var keyLength = key.Length;
|
||
for (int i = 0; i < bytesCount; i++)
|
||
{
|
||
bytes[i] = (byte)(bytes[i] ^ (key[index % key.Length]));
|
||
index++;
|
||
if (index >= keyLength) {
|
||
index = 0;
|
||
}
|
||
}
|
||
var result = System.Text.Encoding.UTF8.GetString(bytes);
|
||
return Convert.ToInt64(result);
|
||
}
|
||
|
||
public static TweenerCore<float, float, FloatOptions> DOBFSliderValue(BFSlider target, float endValue, float duration, bool snapping = false)
|
||
{
|
||
TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.value, x => target.value = x, endValue, duration);
|
||
t.SetOptions(snapping).SetTarget(target);
|
||
return t;
|
||
}
|
||
|
||
// 从Sha1证书签名中提取keyhash给facebook等sdk用
|
||
// 需要去GooglePlayConsle后台查看证书的sha1
|
||
public static string GetAndroidSignKeyHash()
|
||
{
|
||
byte[] sha1 = {
|
||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19
|
||
};
|
||
return Convert.ToBase64String(sha1);
|
||
}
|
||
|
||
public static bool IsSpecialChar(string str, bool useDefaultPattern)
|
||
{
|
||
Regex regExp;
|
||
if (useDefaultPattern)
|
||
{
|
||
regExp = new Regex(DefaultSpecialPatternStr);
|
||
}
|
||
else
|
||
{
|
||
regExp = new Regex(CustomSpecialPatternStr);
|
||
}
|
||
if(regExp.IsMatch(str)){
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
}
|