示例#1
0
        //----------------------------------------------------------------------------------------------------

        static void DownloadFileWWW(RequestAdapter req, string filepath)
        {
#if UNITY_2017_3_OR_NEWER
            var www     = UnityWebRequest.Get(filepath);
            var asyncOp = www.SendWebRequest();

            while (!asyncOp.isDone)
            {
                Thread.Sleep(10);
            }

#if UNITY_2020_1_OR_NEWER
            if (www.result != UnityWebRequest.Result.Success)
#else
            if (www.isNetworkError || www.isHttpError)
#endif
            {
                req.Reject(ResponseCode.InternalServerError);
            }
            else
            {
                req.SetContentType(GetMimeType(filepath));
                req.Respond(www.downloadHandler.data);
            }
#else
            var data = new WWW(filepath);

            while (!data.isDone)
            {
                Thread.Sleep(10);
            }

            if (string.IsNullOrEmpty(data.error))
            {
                req.SetContentType(GetMimeType(filepath));
                req.Respond(data.bytes);
            }
            else
            {
                req.Reject(ResponseCode.InternalServerError);
            }
#endif
        }
示例#2
0
        //----------------------------------------------------------------------------------------------------

        static void DownloadFileWWW(RequestAdapter req, string filepath)
        {
            var data = new WWW(filepath);

            while (!data.isDone)
            {
                Thread.Sleep(10);
            }

            if (string.IsNullOrEmpty(data.error))
            {
                req.SetContentType(GetMimeType(filepath));
                req.Respond(data.bytes);
            }
            else
            {
                req.Reject(ResponseCode.InternalServerError);
            }
        }
示例#3
0
        //----------------------------------------------------------------------------------------------------

        static void DownloadFile(RequestAdapter req, string filepath)
        {
#if UNITY_ANDROID
            /*
             * var data = new WWW( filepath );
             *
             * while( !data.isDone )
             * {
             *  Thread.Sleep( 10 );
             * }
             *
             * if( string.IsNullOrEmpty( data.error ) )
             * {
             *  req.SetContentType( GetMimeType( filepath ) );
             *  req.Respond( data.bytes );
             * }
             * else
             * {
             *  req.Reject( ResponseCode.InternalServerError );
             * }
             */
            req.Reject(ResponseCode.InternalServerError);
#else
            // System.IO

            if ((File.GetAttributes(filepath) & FileAttributes.Directory) == FileAttributes.Directory)
            {
                // list contents of directory

                var files = from c in Directory.GetFileSystemEntries(filepath) select Path.GetFileName(c);

                req.Respond(JsonReflector.Reflect(files.ToArray()));
            }
            else
            {
                // dump bytes

                req.SetContentType(GetMimeType(filepath));
                req.Respond(File.ReadAllBytes(filepath));
            }
#endif
        }
示例#4
0
        //----------------------------------------------------------------------------------------------------

        static void DownloadFileNative(RequestAdapter req, string filepath)
        {
            // System.IO

            if ((File.GetAttributes(filepath) & FileAttributes.Directory) == FileAttributes.Directory)
            {
                // list contents of directory

                var files = from c in Directory.GetFileSystemEntries(filepath) select Path.GetFileName(c);

                req.Respond(JsonReflector.Reflect(files.ToArray()));
            }
            else
            {
                // dump bytes

                req.SetContentType(GetMimeType(filepath));
                req.Respond(File.ReadAllBytes(filepath));
            }
        }
示例#5
0
        static IEnumerator TakeScreenshot(RequestAdapter req, string path)
        {
            // render cameras to render texture

            var screenshot = new RenderTexture(Screen.width, Screen.height, 24);

            if (screenshot == null)
            {
                req.Reject(ResponseCode.InternalServerError);
                yield break;
            }

            screenshot.filterMode = FilterMode.Point; // no filtering as we are using the same resolution as the screen

            yield return(new WaitForEndOfFrame());

            for (int i = 0; i < Camera.allCamerasCount; ++i)
            {
                var camera       = Camera.allCameras[i];
                var previousRTex = camera.targetTexture;

                camera.targetTexture = screenshot;
                camera.Render();
                camera.targetTexture = previousRTex;
            }

            yield return(null);


            // read from render texture to memory (ReadPixels will read from current active render texture)

            var pixels = new Texture2D(screenshot.width, screenshot.height, TextureFormat.RGB24, false);

            if (pixels == null)
            {
                req.Reject(ResponseCode.InternalServerError);
                yield break;
            }

            var previousActiveRTex = RenderTexture.active;

            RenderTexture.active = screenshot;

            pixels.ReadPixels(new Rect(0, 0, screenshot.width, screenshot.height), 0, 0);
            pixels.Apply();

            RenderTexture.active = previousActiveRTex;

            // return png

            var bytes = pixels.EncodeToPNG();

            if (bytes != null)
            {
                req.SetContentType("image/png");
                req.Respond(bytes);
            }
            else
            {
                req.Reject(ResponseCode.InternalServerError);
            }
        }
示例#6
0
 public static void DebugOutput(RequestAdapter req, string path)
 {
     req.SetContentType("text/plain");
     req.Respond(string.Join(Environment.NewLine, sLog.ToArray()));
 }
示例#7
0
        static IEnumerator TakeScreenshot(RequestAdapter req)
        {
            var query = Util.ParseQueryString(req.Query);
            var width = query["width"] != null?Int32.Parse(query["width"])  : -1;

            var height = query["height"] != null?Int32.Parse(query["height"]) : -1;

            var scale = query["scale"] != null?float.Parse(query["scale"], System.Globalization.CultureInfo.InvariantCulture) : -1.0f;

            // render cameras to render texture

            int texWidth  = width > 0 ? width : Screen.width;
            int texHeight = height > 0 ? height : Screen.height;

            if (scale > 0.0f)
            {
                texWidth  = (int)(texWidth * scale);
                texHeight = (int)(texHeight * scale);
            }

            var screenshot = new RenderTexture(texWidth, texHeight, 24);

            if (screenshot == null)
            {
                req.Reject(ResponseCode.InternalServerError);
                yield break;
            }

            screenshot.filterMode = FilterMode.Point; // no filtering as we are using the same resolution as the screen

            yield return(new WaitForEndOfFrame());

            for (int i = 0; i < Camera.allCamerasCount; ++i)
            {
                var camera       = Camera.allCameras[i];
                var previousRTex = camera.targetTexture;

                camera.targetTexture = screenshot;
                camera.Render();
                camera.targetTexture = previousRTex;
            }

            yield return(null);


            // read from render texture to memory (ReadPixels will read from current active render texture)

            var pixels = new Texture2D(screenshot.width, screenshot.height, TextureFormat.RGB24, false);

            if (pixels == null)
            {
                req.Reject(ResponseCode.InternalServerError);
                yield break;
            }

            var previousActiveRTex = RenderTexture.active;

            RenderTexture.active = screenshot;

            pixels.ReadPixels(new Rect(0, 0, screenshot.width, screenshot.height), 0, 0);
            pixels.Apply();

            RenderTexture.active = previousActiveRTex;

            screenshot.Release();
            UnityEngine.Object.Destroy(screenshot);

            // return png

            var bytes = pixels.EncodeToPNG();

            if (bytes != null)
            {
                req.SetContentType("image/png");
                req.Respond(bytes);
            }
            else
            {
                req.Reject(ResponseCode.InternalServerError);
            }

            UnityEngine.Object.Destroy(pixels);
        }