为什么在单击guilayout.button时将按钮的文本从红色变为绿色需要很多时间?

时间:2018-08-10 22:44:36

标签: c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;

public class ScriptsManager : EditorWindow
{
    static Vector2 scrollPos;
    List<GUIStyle> styles = new List<GUIStyle>();

    [MenuItem("Tools/Scripts")]
    static void Scripts()
    {
        EditorWindow.GetWindow<ScriptsManager>();
        GetWindow<ScriptsManager>().minSize = new Vector2(900, 500);
        GetWindow<ScriptsManager>().maxSize = new Vector2(900, 500);
        GetWindow<ScriptsManager>().maximized = false;
    }

    private void OnGUI()
    {
        string[] assetPaths = AssetDatabase.GetAllAssetPaths();

        if (styles.Count() < assetPaths.Length)
        {
            for (int y = 0; y < assetPaths.Length; y++)
            {
                styles.Add(new GUIStyle(GUI.skin.button));
                styles[y].normal.textColor = Color.red;
                styles[y].fontSize = 18;
            }
        }

        EditorGUILayout.BeginVertical();
        scrollPos =
            EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(900), GUILayout.Height(500));

        for (int i = 0; i < assetPaths.Length; i++)
        {
            if (assetPaths[i].Contains(".cs"))
            {
                var x = assetPaths[i].LastIndexOf("/");
                var t = assetPaths[i].Substring(x + 1);

                if (GUILayout.Button(t, styles[i], GUILayout.Width(1000), GUILayout.Height(50)))
                {
                    Event.current.type = EventType.Repaint;

                    if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
                    {
                        OnMouseOver(i, assetPaths[i]);
                    }
                }
            }
        }

        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
    }

    private void OnMouseOver(int buttinIndex, string file)
    {
        styles[buttinIndex].normal.textColor = Color.green;
    }
}

仅在我执行以下操作时:

Event.current.type = EventType.Repaint;

它将为我单击的当前按钮着色为绿色。 问题是,当我单击第一个或两个按钮时,它会先将颜色更改为绿色,然后再单击ext按钮,我单击它所花费的时间越来越长,然后在某些情况下,甚至需要几秒钟甚至将近10秒钟才能将颜色更改为绿色。

如果我删除该行,它将可以快速运行,但不会将按钮文本着色为绿色。

0 个答案:

没有答案