当前位置:网站首页>Unity C # e-learning (VIII) -- www

Unity C # e-learning (VIII) -- www

2022-06-26 15:05:00 Handsome_ shuai_

Unity C# E-learning ( 8、 ... and )——WWW

One .WWW class

www Primary supported protocols
1.http:// and https:// Hypertext transfer protocol
2.ftp:// Text transfer protocol ( But only anonymous downloads )
3.file:// Local file transfer protocol

1.www Class

        // establish www request 
        WWW www = new WWW("http://192.168.1.103:8080/Http_Server/123.pptx");
        // Returns a sound slice from the downloaded data AudioClip object 
        AudioClip cp = www.GetAudioClip();
        // Replace the existing one with the image in the downloaded data Texture2D object 
        Texture2D tex = new Texture2D(100, 100);
        www.LoadImageIntoTexture(tex);
        // Load from cache AB Package object , If the package is not in the cache, it will be automatically downloaded and stored in the cache , To load... Directly from the cache 
        WWW.LoadFromCacheOrDownload("http://192.168.1.103:8080/Http_Server/123.pptx", 1);
        // If the loaded data is AB package , You can get the loading result directly through this variable 
        var ab = www.assetBundle;
        // If you are loading an audio slice file , You can get the loading result directly through this variable 
        var cp1 = www.GetAudioClip();
        // Get the loaded contents in the form of byte array 
        var bytes = www.bytes;
        // Get the number of bytes downloaded in the past 
        var num = www.bytesDownloaded;
        // Return an error message , If an error occurs during the download , You can get the error information by 
        var error = www.error;
        // Determine whether the download is complete 
        var done = www.isDone;
        // Get download progress 
        var poss = www.progress;
        // If the resource is in string form 
        var str = www.text;
        // If the resource is in the form of a picture 
        var texture = www.texture;

2. utilize www Class to load and download files asynchronously

(1) download HTTP Content on the server

    private IEnumerator DownLoadHttp()
    {
    
        WWW www = new WWW("http://192.168.1.103:8080/Http_Server/xxx.jpg");

        while (!www.isDone)
        {
    
            Debug.Log(" speed of progress :" + www.progress);
            Debug.Log(" Downloaded size :" + www.bytesDownloaded);
            yield return null;
        }

        if (www.error == null && www.isDone)
        {
    
            rawImage1.texture = www.texture;
        }
        else
        {
    
            Debug.Log(www.error);
        }
    }

(2) download FTP Content on the server

  • Be careful :www about FTP The server can only be accessed anonymously , Need to be in FTP Create anonymous users on the server (Anonymous)
    private IEnumerator DownLoadFtp()
    {
    
        WWW www = new WWW("ftp://192.168.1.103/1.jpg");

        while (!www.isDone)
        {
    
            Debug.Log(" speed of progress :" + www.progress);
            Debug.Log(" Downloaded size :" + www.bytesDownloaded);
            yield return null;
        }

        if (www.error == null && www.isDone)
        {
    
            rawImage2.texture = www.texture;
        }
        else
        {
    
            Debug.Log(www.error);
        }
    }

(3) Load local content

    private IEnumerator LoadLocalFile()
    {
    
        string path = "file://" + Application.streamingAssetsPath + "/test.jpg";
        WWW www = new WWW(path);
        while (!www.isDone)
        {
    
            Debug.Log(" speed of progress :" + www.progress);
            Debug.Log(" Downloaded size :" + www.bytesDownloaded);
            yield return null;
        }
        
        if (www.error == null && www.isDone)
        {
    
            rawImage3.texture = www.texture;
        }
        else
        {
    
            Debug.Log(www.error);
        }
    }

Two . encapsulation WWWMgr Management category

1.WWWMgr

public class WWWMgr : MonoBehaviour
{
    
    private static WWWMgr instance;
    public static WWWMgr Instance => instance;

    private void Awake()
    {
    
        instance = this;
    }

    public void LoadRes<T>(string path, Action<T> action) where T : class
    {
    
        StartCoroutine(LoadResAsync(path, action));
    }

    private IEnumerator LoadResAsync<T>(string path, Action<T> action) where T : class
    {
    
        WWW www = new WWW(path);

        while (!www.isDone)
        {
    
            Debug.Log(" Download progress :" + www.progress);
            Debug.Log(" Size loaded :" + www.bytesDownloaded);
            yield return null;
        }

        if (www.error != null)
        {
    
            action?.Invoke(null);
            yield break;
        }
        if (typeof(T) == typeof(AssetBundle))
            action?.Invoke(www.assetBundle as T);
        else if (typeof(T) == typeof(AudioClip))
            action?.Invoke(www.GetAudioClip() as T);
        else if(typeof(T) == typeof(Texture2D))
            action?.Invoke(www.texture as T);
        else if(typeof(T) == typeof(string))
            action?.Invoke(www.text as T);
        else
            action?.Invoke(www.bytes as T);
    }
}

2. test

public class Lesson24 : MonoBehaviour
{
    
    [SerializeField] private AudioSource audioSource;
    private void Start()
    {
    
        if (WWWMgr.Instance == null)
        {
    
            GameObject wwwMgrObj = new GameObject("WWWMgr");
            wwwMgrObj.AddComponent<WWWMgr>();
        }
        
        WWWMgr.Instance.LoadRes<AudioClip>("http://192.168.1.103:8080/Http_Server/music.mp3", clip =>
        {
    
            audioSource.clip = clip;
            audioSource.Play();
        });
        
        WWWMgr.Instance.LoadRes("ftp://192.168.1.103/ Program instructions .txt",(string text) =>
        {
    
            Debug.Log(text);
        });
    }
}
原网站

版权声明
本文为[Handsome_ shuai_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261447242229.html