summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/VRWorldToolkit/Scripts/Editor/EditorTextureUtil.cs
blob: 737d99d4ecf1cf4ceb00acde0894986cebbf91d4 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Reflection;
using UnityEngine;
using UnityEngine.Assertions;

/// <summary>
/// <see cref="UnityEditor.TextureUtil"/> Accessor
/// </summary>
/// <author>Seibe TAKAHASHI</author>
/// <remarks>
/// (c) 2017 Seibe TAKAHASHI.
/// This code is released under the MIT License.
/// http://opensource.org/licenses/mit-license.php
/// </remarks>

namespace VRWorldToolkit
{
    public static class EditorTextureUtil
    {
        private static readonly System.Type cType;
        private static MethodInfo mMethod_GetMipmapCount;
        private static MethodInfo mMethod_GetTextureFormat;
        private static MethodInfo mMethod_GetRuntimeMemorySizeLong;
        private static MethodInfo mMethod_GetStorageMemorySizeLong;
        private static MethodInfo mMethod_IsNonPowerOfTwo;

        static EditorTextureUtil()
        {
            cType = Assembly.Load("UnityEditor.dll").GetType("UnityEditor.TextureUtil");
            Assert.IsNotNull(cType);
        }

        public static int GetMipmapCount(Texture texture)
        {
            if (mMethod_GetMipmapCount == null)
                mMethod_GetMipmapCount = cType.GetMethod("GetMipmapCount", BindingFlags.Static | BindingFlags.Public);

            Assert.IsNotNull(mMethod_GetMipmapCount);
            return (int) mMethod_GetMipmapCount.Invoke(null, new[] {texture});
        }

        public static TextureFormat GetTextureFormat(Texture texture)
        {
            if (mMethod_GetTextureFormat == null)
                mMethod_GetTextureFormat = cType.GetMethod("GetTextureFormat", BindingFlags.Static | BindingFlags.Public);

            Assert.IsNotNull(mMethod_GetTextureFormat);
            return (TextureFormat) mMethod_GetTextureFormat.Invoke(null, new[] {texture});
        }

        public static long GetRuntimeMemorySize(Texture texture)
        {
            if (mMethod_GetRuntimeMemorySizeLong == null)
                mMethod_GetRuntimeMemorySizeLong = cType.GetMethod("GetRuntimeMemorySizeLong", BindingFlags.Static | BindingFlags.Public);

            Assert.IsNotNull(mMethod_GetRuntimeMemorySizeLong);
            return (long) mMethod_GetRuntimeMemorySizeLong.Invoke(null, new[] {texture});
        }

        public static long GetStorageMemorySize(Texture texture)
        {
            if (mMethod_GetStorageMemorySizeLong == null)
                mMethod_GetStorageMemorySizeLong = cType.GetMethod("GetStorageMemorySizeLong", BindingFlags.Static | BindingFlags.Public);

            Assert.IsNotNull(mMethod_GetStorageMemorySizeLong);
            return (long) mMethod_GetStorageMemorySizeLong.Invoke(null, new[] {texture});
        }

        public static bool IsNonPowerOfTwo(Texture2D texture)
        {
            if (mMethod_IsNonPowerOfTwo == null)
                mMethod_IsNonPowerOfTwo = cType.GetMethod("IsNonPowerOfTwo", BindingFlags.Static | BindingFlags.Public);

            Assert.IsNotNull(mMethod_IsNonPowerOfTwo);
            return (bool) mMethod_IsNonPowerOfTwo.Invoke(null, new[] {texture});
        }
    }
}