danny1995

FileStreaming

Jun 23rd, 2021
756
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6.  
  7. public class Test : MonoBehaviour
  8. {
  9.     [SerializeField] Image refImage;
  10.     AudioSource refSource;
  11.  
  12.     private void Awake()
  13.     {
  14.         refSource = GetComponent<AudioSource>();
  15.     }
  16.     private void Start()
  17.     {
  18.         StartCoroutine(LoadLocalTexture(Application.streamingAssetsPath+"/img.png",refImage));
  19.         StartCoroutine(LoadLocalAudio(Application.streamingAssetsPath + "/V1C_86.wav",refSource));
  20.     }
  21.  
  22.     public  IEnumerator LoadLocalTexture(string path, Image receivingImage)
  23.     {
  24.         UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);
  25.         yield return www.SendWebRequest();
  26.  
  27.  
  28.  
  29.         if (www.isNetworkError || www.isHttpError)
  30.         {
  31.             Debug.Log(www.error);
  32.         }
  33.         else
  34.         {
  35.             Texture2D myTexture = DownloadHandlerTexture.GetContent(www);
  36.             receivingImage.sprite = Sprite.Create(myTexture, new Rect(0.0f, 0.0f, myTexture.width, myTexture.height), new Vector2(0f, 0f));
  37.             Debug.Log(receivingImage.name);
  38.         }
  39.     }
  40.  
  41.  
  42.  
  43.     public IEnumerator LoadLocalAudio(string path, AudioSource receivingAudio)
  44.     {
  45.         UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.WAV);
  46.         yield return www.SendWebRequest();
  47.  
  48.  
  49.  
  50.         if (www.isNetworkError || www.isHttpError)
  51.         {
  52.             Debug.Log(www.error);
  53.         }
  54.         else
  55.         {
  56.             var audioClip = DownloadHandlerAudioClip.GetContent(www);
  57.             audioClip.name = "Hey come on come on !";
  58.             receivingAudio.clip = audioClip;
  59.             receivingAudio.Play();
  60.             Debug.Log(receivingAudio.clip.name);
  61.         }
  62.     }
  63. }
  64.  
RAW Paste Data