2017-05-05 136 views
0

我正在爲使用Unity遊戲引擎的遊戲創建編輯器,並且我想讓設計師製作新的法術。我試圖達到的目標是設計師將下載一個Unity粒子系統(或GameObject),將其保存在電腦上,併爲遊戲提供完整的路徑。然後遊戲應該加載對象並將其用作新咒語的粒子。Unity資產包

我需要能夠在運行時從文件夾加載Unity GameObject(粒子系統)。我使用資產包嘗試了它,但是我不知道如何使用資產包,當我只想從計算機(本地文件系統)下載文件而非服務器時。資產捆綁包在Debug中完美工作,但在構建和運行時卻沒有(它嘗試連接到某個服務器)。 我使用Asset Bundles的默認腳本:

public class LoadAssets : MonoBehaviour 
    public const string AssetBundlesOutputPath = "/AssetBundles/"; 
    public string assetBundleName; 
    public string assetName; 

    public GameObject loadedObject; 
    public bool finished; 
    public MessageWindow messWindow; 

    // Use this for initialization 
    //originally was start here might create problems 
    public IEnumerator Begin() 
    { 
     messWindow.changeMessageAndShow("Asset " + assetName + " is loading.","Loading"); 
     yield return StartCoroutine(Initialize()); 

     // Load asset. 
     yield return StartCoroutine(InstantiateGameObjectAsync (assetBundleName, assetName)); 
     finished=true; 
     messWindow.Close(); 
    } 

    public GameObject LoadObject() 
    { 
     StartCoroutine(Begin()); 
     //TODO wait for end of couroutine 
     return loadedObject; 
    } 

    // Initialize the downloading url and AssetBundleManifest object. 
    protected IEnumerator Initialize() 
    { 
     // Don't destroy this gameObject as we depend on it to run the loading script. 
     DontDestroyOnLoad(gameObject); 

     // With this code, when in-editor or using a development builds: Always use the AssetBundle Server 
     // (This is very dependent on the production workflow of the project. 
     // Another approach would be to make this configurable in the standalone player.) 
#if DEVELOPMENT_BUILD || UNITY_EDITOR 
     AssetBundleManager.SetDevelopmentAssetBundleServer(); 
#else 
     // Use the following code if AssetBundles are embedded in the project for example via StreamingAssets folder etc: 

     // Or customize the URL based on your deployment or configuration 
     //AssetBundleManager.SetSourceAssetBundleURL("http://www.MyWebsite/MyAssetBundles"); 
#endif 
     AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath + "/"); 

     // Initialize AssetBundleManifest which loads the AssetBundleManifest object. 
     var request = AssetBundleManager.Initialize(); 
     if (request != null) 
      yield return StartCoroutine(request); 
    } 

    protected IEnumerator InstantiateGameObjectAsync (string assetBundleName, string assetName) 
    { 
     // This is simply to get the elapsed time for this phase of AssetLoading. 
     float startTime = Time.realtimeSinceStartup; 

     // Load asset from assetBundle. 
     AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(assetBundleName, assetName, typeof(GameObject)); 
     if (request == null) 
      yield break; 
     yield return StartCoroutine(request); 

     // Get the asset. 
     GameObject prefab = request.GetAsset<GameObject>(); 

     if (prefab != null) 
     { 
      loadedObject = GameObject.Instantiate(prefab);    
      DontDestroyOnLoad(loadedObject); 
     } 

     // Calculate and display the elapsed time. 
     float elapsedTime = Time.realtimeSinceStartup - startTime; 
     Debug.Log(assetName + (prefab == null ? " was not" : " was")+ " loaded successfully in " + elapsedTime + " seconds"); 
    } 
} 

非常感謝。

回答

0

要從本地系統加載資源包,您需要本地AssetBundle服務器。啓用本地資產服務器時,必須構建AssetBundles並將其放置在項目根目錄中顯式名爲AssetBundles的文件夾中,該文件夾與Assets文件夾位於同一級別。

構建的AssetBundles將可供編輯人員使用,並且本地運行的所有構建都可以訪問本地網絡上的編輯器。

使用Assets>AssetBundles>Local AssetBundle Server啓用本地AssetBundle服務器。

Reference to asset bundles and asset bundle manager。

+0

我可以使用不同的Unity項目創建資產包,然後將其移動到我的項目文件夾中嗎?還是需要在我的Unity項目中創建assetbundle?我需要讓設計人員製作他們自己的Unity對象(或下載它),然後讓我的遊戲安裝它,而不需要讓設計師打開我的Unity項目並重新構建它。謝謝。 – Justeagle11

+0

@justeagle,是的,可以在統一項目中共享資產包。因此,您的設計師可以在自己的項目中創建資產捆綁包,然後您的遊戲可以使用該資產捆綁包。 –

+0

但是,如何將其發送回我的項目?我的意思是一旦我的項目建成,唯一的文件夾是Unity_data,裏面是Managed,Mono和Resources,那麼我應該在哪裏從另一個項目(當它應該在AssetBunlde文件夾內)放置AssetBundle?設計師只會收到我製作的項目,他是否還能設法將資產捆綁在遊戲中?謝謝。 – Justeagle11