Caching查看窗口

https://www.cnblogs.com/sifenkesi/p/3530224.html

闲来无事,做了一个简约的Caching查看窗口,可以方便的查看本地缓存的使用情况:

下面的URL和VersionNum用来查看某个特定资源的特定版本是否存在,分别输入所需信息,点击“检测”,即可在下面显示出结果。

代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

using UnityEditor;

using UnityEngine;

using System.Collections;

public class CachingViewer : EditorWindow

{

    static CachingViewer window;

    [MenuItem("WZQYEditor/CachingViewer")]

    static void Execute()

    {

        if (window == null)

        {

            window = (CachingViewer)GetWindow(typeof(CachingViewer));

        }

        window.Show();

    }

    string url = "";

    string version = "";

    bool exist = false;

    void OnGUI()

    {

        // 信息显示区域

        GUI.BeginGroup(new Rect(10f, 10f, 280f, 250f), "", "box");

        EditorGUILayout.BeginVertical();

        GUILayout.Label("是否激活: " + Caching.enabled);

        GUILayout.Label("是否准备好: " + Caching.ready);

        GUILayout.Label("缓存总容量: " + Caching.maximumAvailableDiskSpace / (1024f * 1024f) + "M");

        GUILayout.Label("已使用: " + Caching.spaceOccupied / (1024f * 1024f) + "M");

        GUILayout.Label("还剩余: " + Caching.spaceFree / (1024f * 1024f) + "M");

        GUILayout.Label("空闲剩余时间: " + (Caching.expirationDelay / 3600f) + "h");

        // 特定资源检索

        GUILayout.Label("------------------------------------------------");

        GUILayout.Label("URL");

        url = EditorGUILayout.TextField(url, GUILayout.Width(200), GUILayout.Height(20));

        GUILayout.Label("VersionNum");

        version = EditorGUILayout.TextField(version, GUILayout.Width(200), GUILayout.Height(20));

        if (GUI.Button(new Rect(210f, 150f, 50f, 50f), "检测"))

        {

            exist = Caching.IsVersionCached(url, System.Convert.ToInt32(version));

        }

        GUI.Label(new Rect(0, 210, 200, 20), "该资源是否存在: " + exist);

        GUI.Label(new Rect(0, 230, 300, 20), "------------------------------------------------");

        EditorGUILayout.EndVertical();

        GUI.EndGroup();

        if (GUI.Button(new Rect(10, 280f, 280f, 50f), "清空缓存"))

        {

            Caching.CleanCache();

        }

    }

}

注意:在下载资源时,如果本地缓存空间不足,Unity会根据LRU(least-recently-used最近最久未使用算法)置换算法自动删除一些旧资源,以腾出一些空间用来放置新资源。

LRU算法的答题思路是:给缓存中的每个资源加一个计数器,每次访问时,将被访问资源计数器设为0,并将其他计数器加1,当需要置换资源时将计数器最大的淘汰出缓存,这种算法效率和命中率还是很高的。

猜你喜欢

转载自blog.csdn.net/qq_31967569/article/details/83106093