Esempio n. 1
0
    /// <summary>Saves a Texture2D as a PNG or JPG, depending on the file extension used for the filePath.</summary>
    /// <param name="texture">The Texture2D we want to save as a JPG or PNG.</param>
    /// <param name="settings">The settings we want to use to override the default settings.</param>
    public static void SaveImage(Texture2D texture, ES3Settings settings)
    {
        // Get the file extension to determine what format we want to save the image as.
        string extension = ES3IO.GetExtension(settings.path).ToLower();

        if (string.IsNullOrEmpty(extension))
        {
            throw new System.ArgumentException("File path must have a file extension when using ES3.SaveImage.");
        }
        byte[] bytes;
        if (extension == ".jpg" || extension == ".jpeg")
        {
            bytes = texture.EncodeToJPG();
        }
        else if (extension == ".png")
        {
            bytes = texture.EncodeToPNG();
        }
        else
        {
            throw new System.ArgumentException("File path must have extension of .png, .jpg or .jpeg when using ES3.SaveImage.");
        }

        ES3.SaveRaw(bytes, settings);
    }
Esempio n. 2
0
    /// <summary>Loads an audio file as an AudioClip. Note that MP3 files are not supported on standalone platforms and Ogg Vorbis files are not supported on mobile platforms.</summary>
    /// <param name="imagePath">The relative or absolute path of the audio file we want to load as an AudioClip.</param>
    /// <param name="settings">The settings we want to use to override the default settings.</param>
    public static AudioClip LoadAudio(string audioFilePath, ES3Settings settings)
    {
        if (Application.platform == RuntimePlatform.WebGLPlayer)
        {
            Debug.LogError("You cannot use ES3.LoadAudio with Unity Web Player");
        }

        string extension = ES3IO.GetExtension(audioFilePath).ToLower();

        if (extension == ".mp3" && (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.OSXPlayer))
        {
            throw new System.InvalidOperationException("You can only load Ogg, WAV, XM, IT, MOD or S3M on Unity Standalone");
        }

        if (extension == ".ogg" && (Application.platform == RuntimePlatform.IPhonePlayer ||
                                    Application.platform == RuntimePlatform.Android ||
                                    Application.platform == RuntimePlatform.WSAPlayerARM))
        {
            throw new System.InvalidOperationException("You can only load MP3, WAV, XM, IT, MOD or S3M on Unity Standalone");
        }

        var newSettings = new ES3Settings(audioFilePath, settings);

                #if UNITY_2017_1_OR_NEWER
        WWW www = new WWW(newSettings.FullPath);
                #else
        WWW www = new WWW("file://" + newSettings.FullPath);
                #endif
        while (!www.isDone)
        {
            // Wait for it to load.
        }

        if (!string.IsNullOrEmpty(www.error))
        {
            throw new System.Exception(www.error);
        }

                #if UNITY_2017_3_OR_NEWER
        return(www.GetAudioClip(true));
                #elif UNITY_5_6_OR_NEWER
        return(WWWAudioExtensions.GetAudioClip(www));
                #else
        return(www.audioClip);
                #endif
    }