每日一学4——Unity C# 静态类用法

学习来源:https://unity.cn/projects/cong-unitynei-bu-xia-zai-shu-ju-tu-xiang

本视频着实给了我很大的启发!!!

以前如果写一个获取网络数据的,会单独拎出来一个类处理,但也仅此而已,以前我一般会这么写:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NetWork : MonoBehaviour//这里通常会继承一个单例,方便调用
{
    public void GetData(string url) {
        StartCoroutine(IEGetData(url));
    }

    IEnumerator IEGetData(string url) {
        WWW www = new WWW(url);
        yield return www;
        if (www.error != null)
        {
            //处理出错情况
        }
        else
        {
            //处理正确情况
            Debug.Log(www.text);
        }
    }
}

IEGetData里面对结果的处理甚至会强耦合,每个字符串处理都复制出一份一样的代码出来。。。直到看到这篇视频,才发现,原来代码居然可以这么写!!!
建议跟我一样low的小伙伴们一起把视频看完,能提升一个档次。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public static class WebRequests {

    //WebRequests本身不继承MonoBehaviour,但调用IEnumerator协程需要MonoBehaviour
    //因此使用WebRequestsMonoBehaviour来调用StartCoroutine
    private class WebRequestsMonoBehaviour : MonoBehaviour { }

    private static WebRequestsMonoBehaviour webRequestsMonoBehaviour;

    //用于调用前防止未生成实例,可以通过统一的单例再优化一下
    private static void Init() {
        if (webRequestsMonoBehaviour == null) {
            GameObject gameObject = new GameObject("WebRequests");
            webRequestsMonoBehaviour = gameObject.AddComponent();
        }
    }

    //外部直接调用此处的Get函数,通过委托处理onError和onSuccess事件
    public static void Get(string url, Action onError, Action onSuccess) {
        Init();
        webRequestsMonoBehaviour.StartCoroutine(GetCoroutine(url, onError, onSuccess));
    }

    private static IEnumerator GetCoroutine(string url, Action onError, Action onSuccess) {
        //因为UnityWebRequest继承IDisposable接口,参考https://www.cnblogs.com/wyt007/p/9304564.html
        //因此需要使用using关键字来控制资源的释放
        //我们在写一些临时变量并且希望用完即释放它的时候也可以去继承IDisposable接口
        using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(url)) {
            yield return unityWebRequest.SendWebRequest();

            if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError) {
                onError(unityWebRequest.error);
            } else {
                onSuccess(unityWebRequest.downloadHandler.text);
            }
        }
    }

    public static void GetTexture(string url, Action onError, Action onSuccess) {
        Init();
        webRequestsMonoBehaviour.StartCoroutine(GetTextureCoroutine(url, onError, onSuccess));
    }

    private static IEnumerator GetTextureCoroutine(string url, Action onError, Action onSuccess) {
        using (UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(url)) {
            yield return unityWebRequest.SendWebRequest();

            if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError) {
                onError(unityWebRequest.error);
            } else {
                DownloadHandlerTexture downloadHandlerTexture = unityWebRequest.downloadHandler as DownloadHandlerTexture;
                onSuccess(downloadHandlerTexture.texture);
            }
        }
    }
}

这代码真是令我叹为观止!强烈地激起了我学习的欲望。

你可能感兴趣的:(每日一学4——Unity C# 静态类用法)