예제 #1
0
        } //END Start


        //---------------------------------//
        private IEnumerator DownloadTexture( string url )
        //---------------------------------//
        {
            if( url.Contains( "StreamingAssets" ) )
            {
                url = DatabaseStringHelper.CreateStreamingAssetsPath( url.Replace( "StreamingAssets/", "" ), DatabaseStringHelper.StringStyle.WithEscapeUriAndSystemPathCombine );
            }

            WWW www = new WWW( url );

            while( !www.isDone )
            {
                yield return new WaitForSeconds( .1f );
            }

            if( string.IsNullOrEmpty( www.error ) )
            {
                //Debug.Log( "DownloadTexture() url = " + url + ", Works! Width = " + www.textureNonReadable.width + ", Height = " + www.textureNonReadable.height );

                Texture2D texture = new Texture2D( www.textureNonReadable.width, www.textureNonReadable.height, WWWHelper.GetPlatformPreferredTextureFormat(), false );
                texture = www.textureNonReadable;

                if( material != null ) { material.mainTexture = texture; }
                if( rawImage != null ) { rawImage.texture = texture; }
            }
            else
            {
                //Debug.Log( "DownloadTexture() url = " + url + ", error = " + www.error );
            }

        } //END DownloadTexture
예제 #2
0
        } //END SetResourcesLoadedTexture

        //-----------------------------//
        private IEnumerator SetTextureFromStreamingAssetsBytes( CameraType cameraType, CubeSide cubeSide, ImageType imageType )
        //-----------------------------//
        {
            if( showDebug ) { Debug.Log( "ImageFactory.cs SetStreamingAssetsLoadedTexture() start" ); }

            WWW www;

            //First we need to get the actual image files that are stored in the StreamingAssets folder as byte files
            string texturePath = DatabaseStringHelper.CreateStreamingAssetsPath( "Textures/" + imageType.ToString() + "/" + cameraType.ToString() + "/" + cubeSide.ToString() + ".bytes", DatabaseStringHelper.StringStyle.WithEscapeUriAndSystemPathCombine );
            string existsPath = DatabaseStringHelper.CreateStreamingAssetsPathForFileExistsCheck( "Textures/" + imageType.ToString() + "/" + cameraType.ToString() + "/" + cubeSide.ToString() + ".bytes" );


            bool exists = false;

            //Check if the image exists as bytes, this check is different by platform
            if( Application.platform == RuntimePlatform.Android )
            {
                //Android can only check using www
                www = new WWW( existsPath );

                yield return www;

                exists = string.IsNullOrEmpty( www.error );
            }
            else
            {
                //All other platforms can check using System.IO
                exists = System.IO.File.Exists( existsPath );
            }



            //If the texture does not exist in bytes format, try to load it as a .jpg file
            if( !exists )
            {
                //Debug.Log( "ImageFactory.cs image does not exist as bytes, trying as jpg... existsPath = " + existsPath );
                texturePath = "Textures/" + imageType.ToString() + "/" + cameraType.ToString() + "/" + cubeSide.ToString() + ".jpg";
                texturePath = DatabaseStringHelper.CreateStreamingAssetsPath( texturePath, DatabaseStringHelper.StringStyle.WithEscapeUriAndSystemPathCombine );
            }
            else
            {
                //Debug.Log( "ImageFactory.cs image exists as bytes! ... existsPath = " + existsPath );
            }

            //Download the image bytes from the StreamingAssets folder
            www = new WWW( texturePath );

            //Continue with the main thread until this finishes
            while( !www.isDone )
            {
                yield return www;
            }

            //Setup the appropriate amount of texture memory for the bytes
            SetLoadedTextureScaleAndSettings( cameraType, cubeSide, WWWHelper.GetPlatformPreferredTextureFormat(), false, www.textureNonReadable.width, www.textureNonReadable.height );

            //The bytes are loaded and the texture memory is set, let's turn the bytes into a texture in OpenGL
            if( cameraType == CameraType.Left )
            {
                if( cubeSide == CubeSide.front )
                {
                    texture_Left_front = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.back )
                {
                    texture_Left_back = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.left )
                {
                    texture_Left_left = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.right )
                {
                    texture_Left_right = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.top )
                {
                    texture_Left_top = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.bottom )
                {
                    texture_Left_bottom = www.textureNonReadable;
                }
            }
            else if( cameraType == CameraType.Right )
            {
                if( cubeSide == CubeSide.front )
                {
                    texture_Right_front = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.back )
                {
                    texture_Right_back = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.left )
                {
                    texture_Right_left = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.right )
                {
                    texture_Right_right = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.top )
                {
                    texture_Right_top = www.textureNonReadable;
                }
                else if( cubeSide == CubeSide.bottom )
                {
                    texture_Right_bottom = www.textureNonReadable;
                }
            }

            //Clear the existing bytes from memory
            www.Dispose();

            //Now that the texture is set in OpenGL, make sure the textures are set to Clamped (Hides seams between textures)
            SetLoadedTextureWrapMode( cameraType, cubeSide, TextureWrapMode.Clamp );



            //The texture is now loaded! Set the appropriate boolean to true
            SetLoadedBool( cameraType, cubeSide, true );

        } //END SetStreamingAssetsLoadedTexture
        } //END Start



        //---------------------------------------------//
        public IEnumerator CopyVideoToPersistentDataPath( string urlWithPath )
        //---------------------------------------------//
        {

            string fileName = DatabaseStringHelper.GetFileNameWithExtension( urlWithPath );
            string persistentPath = DatabaseStringHelper.CreatePersistentDataPath( fileName );

            string streamingPath = urlWithPath;
            if( urlWithPath.Contains( "StreamingAssets" ) ) { streamingPath = DatabaseStringHelper.CreateStreamingAssetsPath( urlWithPath.Replace( "StreamingAssets/", "" ), DatabaseStringHelper.StringStyle.WithEscapeUriAndSystemPathCombine ); }


            if( ShowDebug ) Debug.Log( "CopyVideoToPersistent( " + urlWithPath + " ) ... filename = " + fileName + ", persistentPath = " + persistentPath + ", streamingPath = " + streamingPath );


            //If the video does not exist in the persistent data path folder, we need to copy all of it's bytes over
            if( !File.Exists( persistentPath ) )
            {
                WWW www = new WWW( streamingPath );

                while( !www.isDone ) { yield return new WaitForSeconds( .1f ); }

                if( string.IsNullOrEmpty( www.error ) )
                {
                    if( ShowDebug ) Debug.Log( "CopyVideoToPersistent( " + urlWithPath + " ) we found the video in StreamingAssetsPath = " + streamingPath );

                    if( www.bytes != null && www.bytes.Length > 0 )
                    {
                        if( ShowDebug ) Debug.Log( "CopyVideoToPersistent( " + urlWithPath + " ) we found the www.bytes = " + www.bytes.Length.ToString() );


                        //We found the video, copy it's data over over time (not all at once to prevent a crash)
                        Stream stream = new MemoryStream( www.bytes );

                        using( FileStream output = new FileStream( persistentPath, FileMode.Create, FileAccess.Write ) )
                        {
                            byte[] buffer = new byte[ 32 * 1024 ]; //a 32KB-sized buffer is the most efficient
                            int bytesRead;

                            while( ( bytesRead = stream.Read( buffer, 0, buffer.Length ) ) > 0 )
                            {
                                output.Write( buffer, 0, bytesRead );
                            }

                            output.Flush();
                        }

                        //We found the video, copy it's data over
                        //File.WriteAllBytes( persistentPath, www.bytes );

                    }
                    else
                    {
                        if( ShowDebug ) Debug.Log( "CopyVideoToPersistent( " + urlWithPath + " ) we couldn't find any www.bytes = " + www.bytes.Length.ToString() );
                    }
                }
                else
                {
                    if( ShowDebug ) Debug.Log( "CopyVideoToPersistent( " + urlWithPath + " ) Getting the video from StreamingAssets had an error = " + www.error + ", streamingAssetsPath = " + streamingPath );
                }

                www.Dispose();
                www = null;
            }
            else
            {
                if( ShowDebug ) Debug.Log( "CopyVideoToPersistent( " + urlWithPath + " ) the video already exists" );
            }

        } //END CopyVideoToPersistentDataPath