Unity 使用WWW類同步載入資源,unity載入

來源:互聯網
上載者:User

Unity 使用WWW類同步載入資源,unity載入

Unity載入資源有兩種方式:1.Resources.Load(). 2.AssetBundle

前者屬於同步載入,後者一般認為屬於非同步載入,Unity官方也只提供非同步這種方式,其實也可以同步的哦。(習慣按Ctrl + S 好煩)

非同步載入方式:

IEnumerator LoadAssets(string path)

{

WWW www = new WWW(path);

yield return www;

}

void StartLoad()

{

StartCoroutine(LoadAssets(path));

}

StartCoroutine在unity3d的協助中叫做協程,意思就是啟動一個輔助的線程。在C#中協程要定義為IEnumerator (實現式介面)這個類型.它聲明實現該介面的類就可以作為一個迭代器iterator。所以說屬於非同步載入

但是在開發項目前期使用Resources(同步)載入資源,到了後期項目需要做累加式更新,那就得使用WWW類進行載入(聽說還有其他非WWW載入方式,希望大家分享下),同步改成非同步那可不是說改就能改的,很多邏輯都需要重新處理才行。其實使用WWW類進行載入並不一定是非同步,也可以處理成同步載入(也讓我糾結一上午,瀏覽網頁是突然想到可以使用IEnumerable試試,沒想到成功了,哈哈(還是自己太懶,不想大面積改代碼)),其實同學一看就明白,下面直接上代碼:

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


public class LoadAsset 
{
    private string m_sPath = string.Empty;//資源路徑
    private WWW m_www = null;

    private int m_iCountRetry = 5;//重複下載次數

    public LoadAsset(string path)
    {
        m_sPath = path;
    }

    public UnityEngine.Object StarLoad()
    { 
        UnityEngine.Object result = null;

        while (result == null)
        { 
            foreach (UnityEngine.Object obj in LoadWWW())
            {
                result = obj;
            }
            if (m_iCountRetry > 0)
            {
                if (result != null)
                {
                    break;
                }
                m_iCountRetry--;
            }
            else
            {
                break;
            }
        }
        DeInit();
        return result;
    }


    public IEnumerable<UnityEngine.Object> LoadWWW()
    {
        m_www = new WWW(m_sPath);
        yield return m_www.assetBundle.mainAsset;
    }


    private void DeInit()
    {
        if(m_www != null)
        {
            m_www.Dispose();
        }
        m_www = null;
    }
}


public class ResLoad
{
    private static ResLoad instance = null;
    public static ResLoad Instance 
    {
        get
        {
            if (instance == null)
            {
                instance = new ResLoad();
            }
            return instance;
        }
    }
    public UnityEngine.Object StarLoad(string path)
    {
        UnityEngine.Object obj = null;
        LoadAsset asset = new LoadAsset(path);
        obj = asset.StarLoad();
        return obj;
    }

    public T StarLoad<T>(string path)
    {
        LoadAsset asset = new LoadAsset(path);
        object obj = asset.StarLoad() as object;
        return ((T)obj);
    }
}

上面是我開發項目的源碼,拷貝可以直接使用!是不是很簡單,如有沒說到的地方,希望大家補充,多噴噴!

轉載請說明輸出:http://blog.csdn.net/sf575909352/article/details/44573305


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.