0
votes

I am creating an editor for a game using Unity game engine and I want to allow a designer to make new spells. What I am trying to achieve is that a designer will download a Unity particle system (or GameObject), save it on the computer and will give the game full path to it. Then the game should load the object and use it as a particle for a new spell.

I need to be able to load a Unity GameObject (particle system) during runtime from a folder. I tried it with Asset Bundles, but I don’t know how to use Asset Bundles when I only want to download the file from the computer (local file system) not from the server. Asset bundles work perfectly in Debug but not when I build and run it (It tries to connect to some server instead). I use the default script for 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" );
    }
}

Thank you so much.

1

1 Answers

1
votes

To load Asset bundle from local system you need Local AssetBundle Server. When Local Asset Server is enabled, AssetBundles must be built and placed in a folder explicitly called AssetBundles in the root of the Project, which is on the same level as the Assets folder.

The built AssetBundles will be available to the Editor and all builds running locally that can reach the Editor on the local network.

Use Assets>AssetBundles>Local AssetBundle Server to enable the Local AssetBundle Server.

Reference to asset bundles and asset bundle manager.