UGUI切换层级
int allCount = this.transform.childCount;//获取当前容器中所有image的数量
_gob.transform.SetSiblingIndex(allCount-1);//count-1指把child物体_gob在当前子物体列表的顺序设置为最后一个(置顶),0为第一个
transform.SetAsLastSibling();//置顶
编辑模式下实时刷新数据
[SerializeField]
float nn = 0f;
[SerializeField]
Image img;
private void OnValidate()
//编辑器下每一次更改需要实时刷新;
//RefreshAll();
img.transform.localScale = new Vector3(nn,nn,nn);
以上代码可以在不运行程序的时候实时更改img的scale
将字典排序
/// <summary>
/// 将字典排序
/// </summary>
/// <param name="_dic"></param>
void dicSortFunc(ref Dictionary<int, int> _dic)
/*foreach (KeyValuePair<int, int> _pair in _dic)
Debug.Log(_pair.Key+": "+_pair.Value);
//Dictionary<int, int> dicDesc = siblingIndexDic.OrderByDescending(o => o.Value).ToDictionary(o => o.Key, p => p.Value);//降序
Dictionary<int, int> dicDesc = _dic.OrderBy(o => o.Value).ToDictionary(o => o.Key, p => p.Value);//升序
/*foreach (KeyValuePair<int, int> kvp in dicDesc)
Debug.Log("key = " + kvp.Key + ",value = " + kvp.Value);
Debug.Log("---------------------------------------");
foreach (KeyValuePair<int, int> _pair in dicDesc)
Debug.Log(_pair.Key + ": " + _pair.Value);
_dic = dicDesc;
字典的第一个值
MessageBox.Show(dict.FirstOrDefault().Key + ":" + dict.FirstOrDefault().Value.ToString());
FirstOrDefault()可以取到集合的第一项,duKey与Value代表这一项的键名称与值。注意这里没有做null的判定。如果集合没有内容,运行null,则Key与Value都为null,ToString()会报错!
将数组由两边至中间遍历
int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
//int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
int f = 0;//first
/*int e = indexList.Count - 1;//end
int center = indexList.Count / 2;*/
int e = arr.Length-1;//end
int center = arr.Length / 2;
bool boo = true;
while (boo)
Debug.Log("f: " + f);
f++;
Debug.Log("e: " + e);
e--;
if (f == e||Mathf.Abs(f-e)==1)//数组中元素个数为奇数时,最后f和e会是相同的值,所以在此进行此判断来避免这种情况
Debug.Log("f: " + f);//输出中间的两个或一个数
Debug.Log("e: " + e);
boo = false;
continue;
查找数组中与指定数字最相近的那个数
//int[] array = new int[] { 1, 2, 5, 10, 30, 60, 120, 240, 770, 1440 };
float[] array = { -0.9f, 0.6f, 0.4f, -0.3f, -0.8f, 1.6f };
//float a = 20f; //float a = 20; 指定的数字
float a = -1f; //float a = 20;
var num = array.OrderBy(n => Math.Abs(n - a)).ThenBy(n => n).First();
//Console.WriteLine(num);
Debug.Log(num);
强制将分辨率设置为指定尺寸
Screen.SetResolution(1920, 1080, FullScreenMode.ExclusiveFullScreen);
双击/单击/按下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class NaoTuMoudle : MonoBehaviour
{
[SerializeField]
GameObject click;
float clickCurrTime = 0f;
float clickTotalTime = 0.2f;
int clickNum = 0;//点击次数
bool isDown = false;
float downStartTime = 0f;//记录按下时的时间
// Start is called before the first frame update
void Start()
{
AddTriggersListener(click, EventTriggerType.PointerDown, (BaseEventData data) => {
isDown = true;
downStartTime = Time.realtimeSinceStartup;
});
AddTriggersListener(click, EventTriggerType.PointerUp, (BaseEventData data) => {
isDown = false;
});
AddTriggersListener(click, EventTriggerType.PointerClick, (BaseEventData data) => {
clickNum++;
});
}
// Update is called once per frame
void Update()
{
//Debug.Log(Time.realtimeSinceStartup);
if (clickNum != 0) {
clickCurrTime += Time.deltaTime;
if (clickCurrTime >= clickTotalTime) {
if (clickNum >= 2)
{
Debug.Log("双击");
}
else if(clickNum==1){
if (Time.realtimeSinceStartup - downStartTime <= 1f) {
Debug.Log("单击");
}
}
clickCurrTime = 0f;
clickNum = 0;
}
}
if (isDown) {
if (Time.realtimeSinceStartup - downStartTime > 2f) {
Debug.Log("按下");
clickNum = 0;
isDown = false;
}
}
}
private void AddTriggersListener(GameObject obj, EventTriggerType eventID, UnityAction<BaseEventData> action)
{
EventTrigger trigger = obj.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = obj.AddComponent<EventTrigger>();
}
if (trigger.triggers.Count == 0)
{
trigger.triggers = new List<EventTrigger.Entry>();
}
UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = eventID;
entry.callback.AddListener(callback);
trigger.triggers.Add(entry);
}
}
float startX = 50f;//起始X位置
float endX = 1000f;//结束X位置
// Start is called before the first frame update
void Start()
// Update is called once per frame
void Update()
void FixedUpdate()
float lerpValue = Mathf.Lerp(startX, endX, (Time.time - startTime) * speed);
//float lerpValue = Mathf.Lerp(startX, endX, Time.time * speed);
transform.position = new Vector3(lerpValue,0,0);
Unity中的一些关键字
[Tooltip("AAA ")] 可以在Inspector中查看该组件的注释
[Header("Default")] 在Inspector中划分模块
[Multiline(5)] 参数5 是指的是总共5行
/// <summary>
/// 确保此类中包含WebCamTextureToMatHelper类
/// </summary>
[RequireComponent(typeof(WebCamTextureToMatHelper))]
数字格式化
numText.text = string.Format("{0:p2}", 0.8961); //结果为 89.61%
text.text = string.Format("{0:D4}-{1:D2}-{2:D2} " + "{3:D2}:{4:D2}:{5:D2}", year, month, day, hour, minute, second); //结果为 2020-06-18 12:13:56
numText0.text = string.Format("{0:N0}", 56789); //结果为 56,789
发布后中文乱码的问题
将这些DLL文件放到unity里,但是放进Plugins目录中还是放进发布包里就记不清了。。。。
DLL文件链接如下:
链接:https://pan.baidu.com/s/1nK7BhFh_55fSEbIHhJhIXA
提取码:kn7g
ImageEditor类
需要手动将Unity安装路径Unity\Editor\Data\UnityExtensions\Unity\GUISystem\Editor 下的UnityEditor.UI.dll文件拷贝到当前项目的Assets文件夹下
取一定范围内的随机不重复的数字
//total 总数组长度(比如100以内随机) n 取到多少位数(取十个随机数)
public int[] GetRandomNum(int total, int n)
//随机总数组
int[] sequence = new int[total];
//取到的不重复数字的数组长度
int[] output = new int[n];
for (int i = 0; i < total; i++)
sequence[i] = i;
int end = total - 1;
for (int i = 0; i < n; i++)
//随机一个数,每随机一次,随机区间-1
int num = Random.Range(0, end + 1);
output[i] = sequence[num];
//将区间最后一个数赋值到取到数上
sequence[num] = sequence[end];
end--;
//执行一次效果如:1,2,3,4,5 取到2
//则下次随机区间变为1,5,3,4;
return output;
关闭谷歌浏览器
其实就是关闭第三方软件,可以直接关闭相应的进程名。
也可以先通过unity打开,在打开软件的同时先获取到对应的进程,然后关闭掉这个进程。
关闭第三方:
void Update()
if (Input.GetKeyDown(KeyCode.A))
UnityEngine.Debug.Log("尝试关闭");
KillProcess("chrome");
//关闭第三方软件
void KillProcess(string processName)
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
if (!process.HasExited)
if (process.ProcessName == processName)
process.Kill();
UnityEngine.Debug.Log("已关闭进程");
catch (System.InvalidOperationException ex)
UnityEngine.Debug.Log(ex);
先获取再关闭:
Scroll View组件直接跳转进度
先获取ScrollRect
如果是横向跳转,则设置scrollRect.horizontalNormalizedPosition = 0f;或scrollRect.horizontalNormalizedPosition = 1f;
如果是纵向跳转,则设置scrollRect.verticalNormalizedPosition = 0f;或scrollRect.verticalNormalizedPosition = 1f;
修改spriterender透明度
通过spriterender.color = new color(r,g,b, a) 修改,第四个参数为 alpha参数
值域为0~~1
打印变量名
using System;
using System.Linq.Expressions;
public static class MemberInfoGetting
public static string GetMemberName<T>(Expression<Func<T>> memberExpression)
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
return expressionBody.Member.Name;
string Bianlia = "123";
string nameOfTestVariable = MemberInfoGetting.GetMemberName(() => Bianlia );
通过string获取变量中的属性///利用字符串获取变量名里的值
public class test:MonoBehaviour
string str = "id";
public int num = 3;
test2 te=new test2();
private void Start()
int myNum = (int)te.GetType().GetField(str).GetValue(te);//获取te的类型,获取里面str字符串对应的信息,获取te对象里面的这个值。
myNum += num;
print(myNum);//输出为103;
public class test2
public int id=100;
获取两个物体之间的角度,并且让物体A面对物体B
//Vector3 targetDir = new Vector3(Screen.width / 2, Screen.height / 2) - transform.localPosition; // 目标坐标与当前坐标差的向量
//float angle= Vector3.Angle(transform.localPosition, targetDir); // 返回当前坐标与目标坐标的角度
//Debug.Log(angle);
Vector3 target_pos = new Vector3(Screen.width / 2, Screen.height / 2,0);
Vector3 my_pos = transform.position;
Vector3 from = Vector3.up;
Vector3 to = target_pos - my_pos;
transform.rotation = Quaternion.FromToRotation(from, to);
获取b点相对于a点的角度,也就是说a点加上这角度就会指向b点。
/// <summary>
/// 获取b点相对于a点的角度,也就是说a点加上这角度就会指向b点。
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
private float GetAngle(Vector3 a, Vector3 b)
b.x -= a.x;
b.y -= a.y;
float deltaAngle = 0;
if (b.x == 0 && b.y == 0)
return 0;
else if (b.x > 0 && b.y > 0)
deltaAngle = 0;
else if (b.x > 0 && b.y == 0)
return 90;
else if (b.x > 0 && b.y < 0)
deltaAngle = 180;
else if (b.x == 0 && b.y < 0)
return 180;
else if (b.x < 0 && b.y < 0)
deltaAngle = -180;
else if (b.x < 0 && b.y == 0)
return -90;
else if (b.x < 0 && b.y > 0)
deltaAngle = 0;
float angle = Mathf.Atan(b.x / b.y) * Mathf.Rad2Deg + deltaAngle;
return angle;
将图片等比例缩小(图片按照一定的尺寸进行自适应)
/// <summary>
/// 将图片等比例缩小
/// </summary>
/// <param name="_imgW"></param>
/// <param name="_imgH"></param>
/// <returns></returns>
Vector2 changePixel(float _imgW,float _imgH)
-- 根据屏幕分辨率宽高比,计算出新的Canvas resolutionSize,得到UI需要显示的分辨率
if (screenWidth / screenHeight) > (designWidth / designHeight) then
-- 屏幕长,对Canvas宽拉伸到屏幕比例
designWidth = screenWidth / screenHeight * designHeight
-- 屏幕高,对Canvas高拉伸到屏幕比例
designHeight = screenHeight / screenWidth * designWidth
-- 对图片缩放,图片分辨率和设计分辨率相差大的边作为缩放比,等比例缩放图片
local scaleRate = 1
if (designWidth / designHeight) > (spriteWidth / spriteHeight) then
scaleRate = designWidth / spriteWidth
scaleRate = designHeight / spriteHeight
retWidth = spriteWidth * scaleRate
retHeight = spriteHeight * scaleRate
return retWidth, retHeight
float scaleRate;
float boardW = 432f;//scene中的图片容器的宽高
float boardH = 240f;
//以短边为基准等比例缩小
/*if (boardW/ boardH > _imgW / _imgH)
scaleRate = boardW/_imgW;
scaleRate = boardH / _imgH;
//以长边为基准等比例缩小
if (boardW / boardH < _imgW / _imgH)
scaleRate = boardW / _imgW;
scaleRate = boardH / _imgH;
float newW = _imgW * scaleRate;
float newH = _imgH * scaleRate;
Vector2 newVec = new Vector2(newW, newH);
return newVec;
rawImg.texture = tex;
rawImg.SetNativeSize();
rectTransform.sizeDelta=changePixel(rectTransform.sizeDelta.x, rectTransform.sizeDelta.y);
控制粒子的一部分代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Particle0 : MonoBehaviour
public delegate void ParticleStopFunc();
public event ParticleStopFunc ParticleStopHandler;
// Start is called before the first frame update
void Start()
ParticleSystem particle = GetComponent<ParticleSystem>();
ParticleSystem.MainModule mainModule = particle.main;
mainModule.loop = false;
mainModule.stopAction = ParticleSystemStopAction.Callback;//加上这句后会触发OnParticleSystemStopped函数
particle.Play();
// Update is called once per frame
void Update()
public void OnParticleSystemStopped()
//Debug.Log("粒子停止");
if (ParticleStopHandler != null)
ParticleStopHandler();
Destroy(this.gameObject);
unity2019引入 System.Drawing.dll
从2019的安装目录下找到dll文件,路径为:D:\SoftWare\Unity2019_3_13\Unity\Editor\Data\MonoBleedingEdge\lib\mono\2.0-api
然后将System.Drawing.dll放在unity的Plugins文件夹中
之后在unity中的PlayerSetting→Player→Other Settings→Other Settings→Api Compatibility Level,设置为.NET 4.X即可
射线检测(TouchEvent版)
// 射线检测
void RayHitFunction(Vector2 pos,Pointer p)
UnityEngine.Ray ray = RayCamera.ScreenPointToRay(pos);
RaycastHit[] hits = Physics.RaycastAll(ray);
if (hits.Length > 0)
foreach (var item in hits)
GameObject gameObject = item.collider.gameObject;
if (gameObject.CompareTag("AAA") && gameObject.activeSelf)
print(item.collider.gameObject.name);
GameObject cursor;
if (!cursors.TryGetValue(p.Id, out cursor)) continue;
cursor.transform.position = item.point;
面对向量 推力
#region 面向推力前进的方向 平滑
Quaternion desiredRotation = Quaternion.LookRotation(rb3D.velocity);
transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, Time.deltaTime);
#endregion
//面向推力前进的方向
transform.rotation = Quaternion.LookRotation(rb3D.velocity);
物理运动时穿模的解决方法之一
//Rigidbody.maxDepenetrationVelocity 最大穿透速度?
//float.PositiveInfinity 正无穷大?
Physics.defaultMaxDepenetrationVelocity = float.PositiveInfinity;
防穿模 需要先写防穿透数值(就是上面的那句), 然后再用代码生成模型对象,这样才能有效防止穿模
双for循环遍历时 给对象标序号
for (int x = 0; x < grid.GetWidth(); x++)
for (int y = 0; y < grid.GetHeight(); y++)
int index = x * grid.GetHeight() + y;
Debug.Log(index);
grid.GetWidth()是外层list的count
grid.GetHeight()是里面list的count
射线检测2D
/// <summary>
/// 射线检测
/// </summary>
/// <param name="currTf">当前对象 发出射线的物体</param>
/// <param name="dir">射线向量(方向和长度?)</param>
public static void RayFind(Transform currTf, Vector2 dir)
Ray2D ray = new Ray2D(currTf.position, dir);
Vector2 target = dir + new Vector2(currTf.position.x, currTf.position.y);//将子空物体的相对坐标转换为世界坐标,求出真正射线终结点坐标
Debug.DrawLine(ray.origin, target, Color.red); //画射线,测试用,实际可去掉
RaycastHit2D info = Physics2D.Raycast(ray.origin, dir, Mathf.Sqrt(dir.x * dir.x + dir.y * dir.y));
if (info.collider != null)
if (info.transform.gameObject.CompareTag(TagState.Ground.ToString()))
Debug.Log("碰到地板");
击退/击飞
if (collision.gameObject.tag == TagState.Player.ToString())
//此示例是玩家击飞怪物
//collision.gameObject是玩家
//this.gameObject是怪
float forceN = 2000 / (this.transform.position - collision.gameObject.transform.position).magnitude;
//Vector3 _vec = collision.gameObject.transform.position - this.transform.position;
Vector3 _vec = (this.transform.position-collision.gameObject.transform.position).normalized;
//rigidbody2D.AddForce(_vec * 100f);
rigidbody2D.AddForce(_vec * forceN);
hitFunc();
判断目标物体是否在屏幕内
/// <summary>
/// 判断是否还在屏幕内
/// </summary>
/// <param name="worldPos">目标物体的坐标</param>
/// <returns></returns>
public bool IsInView(Vector3 worldPos)
Transform camTransform = Camera.main.transform;
Vector2 viewPos = Camera.main.WorldToViewportPoint(worldPos);
Vector3 dir = (worldPos - camTransform.position).normalized;
float dot = Vector3.Dot(camTransform.forward, dir); //判断物体是否在相机前面
if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
return true;
return false;
Kinect的识别顺序排序或距离排序