Example #1
0
        public static void ForceReadable(this Texture texture)
        {
            string texturePath = AssetDatabase.GetAssetPath(texture);

            if (!String.IsNullOrEmpty(texturePath))
            {
                var importTool = new BabylonTextureImporter(texturePath);
                if (!importTool.IsReadable())
                {
                    importTool.SetReadable();
                    importTool.ForceUpdate();
                }
            }
        }
        public static bool IsSRGB(this Texture2D texture)
        {
            bool   result         = true;
            string srcTexturePath = AssetDatabase.GetAssetPath(texture);

            if (!String.IsNullOrEmpty(srcTexturePath))
            {
                var importTool = new BabylonTextureImporter(srcTexturePath);
                if (importTool.textureImporter != null)
                {
                    result = importTool.textureImporter.sRGBTexture;
                }
            }
            return(result);
        }
        public static Color[] GetSafePixels(this Texture2D texture, Rect?crop = null)
        {
            Color[]   pixels     = null;
            Texture2D source     = texture;
            bool      isReadable = false;
            BabylonTextureImporter importTool = null;
            string srcTexturePath             = AssetDatabase.GetAssetPath(texture);

            if (srcTexturePath.IndexOf("unity_builtin_extra", StringComparison.OrdinalIgnoreCase) >= 0)
            {
                source = texture.RenderToTexture();
            }
            else
            {
                importTool = new BabylonTextureImporter(srcTexturePath);
                isReadable = importTool.IsReadable();
                if (!isReadable)
                {
                    importTool.SetReadable();
                }
            }
            try
            {
                if (crop != null && crop.HasValue)
                {
                    pixels = source.GetPixels((int)crop.Value.x, (int)crop.Value.y, (int)crop.Value.width, (int)crop.Value.height);
                }
                else
                {
                    pixels = source.GetPixels();
                }
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogException(ex);
            }
            finally
            {
                if (importTool != null && isReadable == false)
                {
                    importTool.ForceUpdate();
                }
            }
            return(pixels);
        }