240 lines
7.1 KiB
C#
240 lines
7.1 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
using UnityEngine.EventSystems;
|
||
|
||
public class UIBase : MonoBehaviour
|
||
{
|
||
public static UIBase _instance;
|
||
|
||
|
||
void Start()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
|
||
void Awake()
|
||
{
|
||
_instance = this;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
|
||
}
|
||
/// <summary>
|
||
/// 杀死进程
|
||
/// </summary>
|
||
/// <param name="_exePathName">进程名称,不带路径及后缀</param>
|
||
public void CloseExe(string _exePathName)
|
||
{
|
||
System.Diagnostics.Process[] progress = System.Diagnostics.Process.GetProcessesByName(_exePathName);
|
||
UnityEngine.Debug.Log("progress个数:" + progress.Length);
|
||
for (int i = 0; i < progress.Length; i++)
|
||
{
|
||
progress[i].Kill();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public bool IsPointerOverUIObject()
|
||
{//判断是否点击的是UI,有效应对安卓没有反应的情况,true为UI
|
||
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
|
||
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
|
||
List<RaycastResult> results = new List<RaycastResult>();
|
||
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
|
||
return results.Count > 0;
|
||
}
|
||
/// <summary>
|
||
/// 读取数据
|
||
/// </summary>
|
||
/// <param name="pathName">数据地址。文件名</param>
|
||
public string JsonLoad(string pathName)
|
||
{
|
||
string filePath = Application.streamingAssetsPath + "/" + pathName + ".json";
|
||
return File.ReadAllText(filePath);//, Encoding.GetEncoding("gb2312"));//转码 中文乱码问题
|
||
|
||
}
|
||
|
||
public float ReturnTwoDotsAngle(Vector3 a,Vector3 b) {
|
||
a = a.normalized;
|
||
b = b.normalized;
|
||
float result = Vector3.Dot(a,b);
|
||
float radians=Mathf.Acos(result);
|
||
float angle= radians * Mathf.Rad2Deg;
|
||
Vector3 c = Vector3.Cross(a, b);
|
||
Vector3 normal = new Vector3(1,1,1);
|
||
normal = normal.normalized;
|
||
float value = Vector3.Dot(c,normal);
|
||
if (value<0) {
|
||
angle *= -1;
|
||
}
|
||
Debug.LogError("====="+angle);
|
||
return angle;
|
||
|
||
}
|
||
|
||
|
||
public void MyDot(Vector3 a, Vector3 b)
|
||
{
|
||
//点乘结果
|
||
float _result1 = Vector3.Dot(a, b);
|
||
|
||
//角度[0-180]
|
||
float _angle1 = Vector3.Angle(a, b);
|
||
|
||
//也可通过 单位向量 来计算角度
|
||
//_result2 为夹角余弦值
|
||
float _result2 = Vector3.Dot(a.normalized, b.normalized);
|
||
//反余弦函数来求对应的弧度
|
||
float _temp = Mathf.Acos(_result2);
|
||
//弧度转角度[0,90]
|
||
float _angel2 = Mathf.Rad2Deg * _temp;
|
||
|
||
|
||
Debug.Log(string.Format("角度1:{0},角度2:{1}", _angle1, _angel2));
|
||
}
|
||
|
||
|
||
//叉乘
|
||
public void MyCross(Vector3 a, Vector3 b)
|
||
{
|
||
//叉乘结果
|
||
Vector3 _result3 = Vector3.Cross(a, b);
|
||
//获取两个向量夹角(弧度)
|
||
float _temp = Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(a.normalized, b.normalized)));
|
||
float _angel3 = Mathf.Rad2Deg * _temp;
|
||
|
||
Debug.Log("角度3:" + _angel3);
|
||
|
||
// 判断顺时针、逆时针方向,是在 2D 平面内的,所以需指定一个平面,
|
||
//下面以X、Z轴组成的平面为例 , (Y 轴为纵轴),
|
||
// 在 X、Z 轴平面上,判断 b 在 a 的顺时针或者逆时针方向,
|
||
if (_result3.y > 0)
|
||
{
|
||
// b 在 a 的顺时针方向
|
||
Debug.Log("b 在 a 的顺时针方向");
|
||
}
|
||
else if (_result3.y == 0)
|
||
{
|
||
// b 和 a 方向相同(平行)
|
||
Debug.Log("b 和 a 方向相同");
|
||
}
|
||
else
|
||
{
|
||
// b 在 a 的逆时针方向
|
||
Debug.Log("b 在 a 的逆时针方向");
|
||
}
|
||
}
|
||
|
||
|
||
//点积
|
||
private void TestDot(Vector3 a, Vector3 b)
|
||
{
|
||
// 计算 a、b 点积结果
|
||
float result = Vector3.Dot(a, b);
|
||
|
||
// 通过向量直接获取两个向量的夹角(默认为 角度), 此方法范围 [0 - 180]
|
||
float angle = Vector3.Angle(a, b);
|
||
|
||
// 计算 a、b 单位向量的点积,得到夹角余弦值,|a.normalized|*|b.normalized|=1;
|
||
result = Vector3.Dot(a.normalized, b.normalized);
|
||
// 通过反余弦函数获取 向量 a、b 夹角(默认为 弧度)
|
||
float radians = Mathf.Acos(result);
|
||
// 将弧度转换为 角度
|
||
angle = radians * Mathf.Rad2Deg;
|
||
}
|
||
|
||
//叉乘
|
||
private void TestCross(Vector3 a, Vector3 b)
|
||
{
|
||
//计算向量 a、b 的叉积,结果为 向量
|
||
Vector3 c = Vector3.Cross(a, b);
|
||
|
||
// 通过反正弦函数获取向量 a、b 夹角(默认为弧度)
|
||
float radians = Mathf.Asin(Vector3.Distance(Vector3.zero, Vector3.Cross(a.normalized, b.normalized)));
|
||
float angle = radians * Mathf.Rad2Deg;
|
||
|
||
// 判断顺时针、逆时针方向,是在 2D 平面内的,所以需指定一个平面,
|
||
//下面以X、Z轴组成的平面为例 , (Y 轴为纵轴),
|
||
// 在 X、Z 轴平面上,判断 b 在 a 的顺时针或者逆时针方向,
|
||
if (c.y > 0)
|
||
{
|
||
// b 在 a 的顺时针方向
|
||
}
|
||
else if (c.y == 0)
|
||
{
|
||
// b 和 a 方向相同(平行)
|
||
}
|
||
else
|
||
{
|
||
// b 在 a 的逆时针方向
|
||
}
|
||
}
|
||
|
||
// 获取两个向量的夹角 Vector3.Angle 只能返回 [0, 180] 的值
|
||
// 如真实情况下向量 a 到 b 的夹角(80 度)则 b 到 a 的夹角是(-80)
|
||
// 通过 Dot、Cross 结合获取到 a 到 b, b 到 a 的不同夹角
|
||
public void GetAngle(Vector3 a, Vector3 b)
|
||
{
|
||
Vector3 c = Vector3.Cross(a, b);
|
||
float angle = Vector3.Angle(a, b);
|
||
|
||
// b 到 a 的夹角
|
||
float sign = Mathf.Sign(Vector3.Dot(c.normalized, Vector3.Cross(a.normalized, b.normalized)));
|
||
float signed_angle = angle * sign;
|
||
|
||
Debug.Log("b -> a :" + signed_angle);
|
||
|
||
// a 到 b 的夹角
|
||
sign = Mathf.Sign(Vector3.Dot(c.normalized, Vector3.Cross(b.normalized, a.normalized)));
|
||
signed_angle = angle * sign;
|
||
|
||
Debug.Log("a -> b :" + signed_angle);
|
||
}
|
||
|
||
public void DirectionJudge(Vector3 a, Vector3 b)
|
||
{
|
||
a = a.normalized;
|
||
b = b.normalized;
|
||
Vector3 dir = a - b;
|
||
dir.z = 0f;
|
||
var magiDir = Vector3.Magnitude(dir);//取模
|
||
float v = Vector3.Dot(Vector3.up, dir) / magiDir;//点乘判断前后
|
||
float h = Vector3.Cross(Vector3.forward, dir).y;//叉乘判断左右
|
||
if (v >= 0.5f)
|
||
{
|
||
Debug.Log("向前");
|
||
}
|
||
else if (v < -0.5f)
|
||
{
|
||
Debug.Log("向后");
|
||
}
|
||
else if (h > 0)
|
||
{
|
||
Debug.Log("向右");
|
||
}
|
||
else if (h < 0)//如果写else,点原地时会卡一个left的trigger进去,不行
|
||
Debug.Log("向左");
|
||
}
|
||
|
||
public void ClearChilds(Transform trans) {
|
||
for (int i = 0; i < trans.childCount; i++)
|
||
{
|
||
Destroy(trans.GetChild(i).gameObject);
|
||
}
|
||
}
|
||
}
|