unity的一些杂货

UGUI切换层级

int allCount = this.transform.childCount;//获取当前容器中所有image的数量
_gob.transform.SetSiblingIndex(allCount-1);//count-1指把child物体_gob在当前子物体列表的顺序设置为最后一个(置顶),0为第一个

transform.SetAsLastSibling();//置顶

transform.SetAsFirstSibling();//置底

编辑模式下实时刷新数据

    [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);
}
}

 

InputField自动更改为激活状态


inputField.ActivateInputField();

插值的基础应用

    float speed = 0.2f;
    float startTime = 1f;//开始时间
    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行

数字格式化

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安装路径UnityEditorDataUnityExtensionsUnityGUISystemEditor 下的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)
        {
            try
            {
                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;
    }
原文地址:https://www.cnblogs.com/lingLuoChengMi/p/12793627.html