Unity问题(知识点)记录

1.Canvas遮挡问题

2.LayerMask To Layer

参考:https://forum.unity.com/threads/get-the-layernumber-from-a-layermask.114553/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class LayerMaskExtension
{
    public static bool IsInLayer(int _gameObjectLayer, LayerMask _mask)
    {
        return ((1 << _gameObjectLayer) & _mask) != 0;
    }
 
    /// <summary>
    /// Returns a value between [0;31].
    /// Important: This will only work properly if the LayerMask is one created in the inspector and not a LayerMask
    /// with multiple values.
    /// </summary>
    public static int GetLayerNumber(this LayerMask _mask)
    {
        var bitmask = _mask.value;
        int result = bitmask > 0 ? 0 : 31;
        while (bitmask > 1)
        {
            bitmask = bitmask >> 1;
            result++;
        }
        return result;
    }
 
    /// <summary>
    /// Returns a list of values between [0;31].
    /// </summary>
    public static List<int> GetAllLayerNumbers(this LayerMask _mask)
    {
         List<int> layers = new List<int>();
        var bitmask = _mask.value;
        for (int i = 0; i < 32; i++) {
            if (((1 << i) & bitmask) != 0) {
                layers.Add(i);
            }
        }
        return layers;
    }

    /// <summary>
    /// Returns a list of values between [0;31].
    /// </summary>
    public static List<string> GetAllLayerNames(this LayerMask _mask)
    {
         List<string> layers = new List<string>();
        var bitmask = _mask.value;
        for (int i = 0; i < 32; i++) {
            if (((1 << i) & bitmask) != 0) {
                layers.Add(LayerMask.LayerToName(i));
            }
        }
        return layers;
    }
}

3.UniyEditor脚本进度条(注意2020.1版本用法不同了!)

Editor脚本运行,卡住的情况下,比如处理几万几十万条数据,将处理结果关联到模型上,长时间运行没有进度条和打印信息。

参考:https://blog.csdn.net/liqiangeastsun/article/details/42174339

结果:没有效果啊。EditorUtility.DisplayProgressBar

官方文档:https://docs.unity3d.com/2019.4/Documentation/ScriptReference/EditorUtility.DisplayProgressBar.html

using UnityEditor;
using UnityEngine;
using System.Collections;

// Simple Editor Script that fills a bar in the given seconds.
public class EditorUtilityDisplayProgressBar : EditorWindow
{
    public float secs = 10f;
    public float startVal = 0f;
    public float progress = 0f;
    [MenuItem("Examples/Progress Bar Usage")]
    static void Init()
    {
        UnityEditor.EditorWindow window = GetWindow(typeof(EditorUtilityDisplayProgressBar));
        window.Show();
    }

    void OnGUI()
    {
        secs = EditorGUILayout.FloatField("Time to wait:", secs);
        if (GUILayout.Button("Display bar"))
        {
            if (secs < 1)
            {
                Debug.LogError("Seconds should be bigger than 1");
                return;
            }
            startVal = (float)EditorApplication.timeSinceStartup;
        }
        if (progress < secs)
            EditorUtility.DisplayProgressBar("Simple Progress Bar", "Shows a progress bar for the given seconds", progress / secs);
        else
            EditorUtility.ClearProgressBar();
        progress = (float)(EditorApplication.timeSinceStartup - startVal);
    }

    void OnInspectorUpdate()
    {
        Repaint();
    }
}

也不行。

突然想到,我的版本是2020.1啊,自从更新到这个版本后,ProgressBar上就自带了个时间。

切换官方文档的版本后突然看到不一样的代码!

https://docs.unity3d.com/2020.1/Documentation/ScriptReference/EditorUtility.DisplayProgressBar.html

using System.Threading;
using UnityEditor;
using UnityEngine;

// Shows a progress bar for the given number of seconds.
public class EditorUtilityDisplayProgressBar : EditorWindow
{
    public float secs = 5f;
    [MenuItem("Examples/Progress Bar Usage")]
    static void Init()
    {
        var window = GetWindow(typeof(EditorUtilityDisplayProgressBar));
        window.Show();
    }

    void OnGUI()
    {
        secs = EditorGUILayout.Slider("Time to wait:", secs, 1.0f, 20.0f);
        if (GUILayout.Button("Display bar"))
        {
            var step = 0.1f;
            for (float t = 0; t < secs; t += step)
            {
                EditorUtility.DisplayProgressBar("Simple Progress Bar", "Doing some work...", t / secs);
                // Normally some actual computation would be here;
                // for this example just sleep.
                Thread.Sleep((int)(step * 1000.0f));
            }
            EditorUtility.ClearProgressBar();
        }
    }
}

虽然,DisplayProgressBar,还是一样的,但是,感觉使用上相当于是支持多线程了。而且不是在不同的OnGUI调用过程中不断的显示进度,而是在一个OnGUI中显示进度的。

可以简单的在我们自己的耗时计算的前后、中间都加上ProgressBar,就能显示出相应的进度了。

还找到一个自己写的ProgressBar,https://forum.unity.com/threads/custom-progress-bar-not-using-editorutility-displayprogressbar.179853/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值