Learn - asset bundle master

A

Setting up the file system

The file system is responsible for uploading the asset bundles from the prefab system. The system comes with two supported hosting services that you can use; Amazon S3 and FTP.

filesystem_activeserviceTo choose what service to actively use, both at runtime and in the editor, select it from the drop down menu under "Active Service". 

The file system is a versatile system and can be used in other areas than the prefab system.
1

FTP Settings

  • filesystem_ftpsettignsHost Name Host name or IP address to the FTP server.
  • Port FTP server port
  • Admin Username Username for an admin account. This is used when uploading files.
  • Admin Password  Password for the admin account.
  • Client Username Username for a client account. This will be used at runtime for downloading files.
  • Client password Password for the client account.
  • Use HTTP for download Determinces if the system should use the HTTP address when generating download urls instead of the FTP address.
  • HTTP Host Name Host name or IP address used when generating HTTP download urls.
2

Amazon S3 Settings

Amazon S3 official documentation can be found here.

  • Access Key filesystem_s3settings Your Amazon S3 Access Key
  • Secret Key Your Amazon S3 Secret Key
  • Bucket Name The name of the bucket the file system should operate in. You'll need to make sure the bucket exist before using the file system.
  • Endpoint region The Amazon region to operate in. More information about regions can be found here.
  • URL Expiration Minutes When generating a url with the Amazon S3 service, you have to specify how long that url should be valid from the time it was created. The value is set in whole minutes.
  • Ignore Certificate Valid Add INFO HERE
B

The Prefab Identifier

prefabidentifier_inspectorThe prefab identifier script contains all the data necessary for a single prefab. This is one of the core classes of the prefab system and you will need to attach this script to ALL prefab game objects that is to be handled by the system.

When the identifier is attached to a prefab game object. The system will use it to detect any changes made to that prefab.

1

Local or remote prefab

prefabidentifier_islocal

Assuming that your file system settings is set up properly, all you have to do to switch between storing a file locally and remotely is to toggle the "Is Local" field in the inspector view.

You will need to resort the prefabs in the prefab system window before the changes take effect.

2

Unique ID

Important! If you duplicate a prefab with the PrefabIdentifier script already attached to it, then you need to press "Generate New ID" to create a unique ID.
"Copy ID" copies the ID into the clipboard.
prefabidentifier_actions
C

Prefab System

Sorting prefabs is done before the build is performed. The system finds all prefabs with the Prefab Identifier script attached to it from a specified folder and sorts them into a separate workspace folder.

When building, the prefab system takes all the information in the workspace folder and creates the remote and local prefab assets used at runtime.

1

Setting up the Prefab system

prefabsystem_menu

You can find all the necessary settings in the Prefab System window. Navigate to Twoorb -> Prefab System -> Prefab System Wizard.

2

Sorting Settings

prefabsystem_sorting
  • Relative Prefab Folder Relative path where all your runtime prefabs resides. This is the folder the system will search for prefabs with the Prefab Identifier script attached.
  • Prefabs per remote folder Sets the desired max cap for number of prefabs per asset bundle. Useful if you want your bundles divided in small chunks.
  • Force sort Checking this will forcefully resort the whole system. If unchecked, the system will only sort and update the bundles updated since the last sort.
3

Generation Settings

prefabsystem_generation
  • Update Local Prefabs This will decide if the local prefab data should be updated and transferred to the runtime data.
  • Update Remote Prefabs This will decide if asset bundles should be built and the resulting data transferred to the runtime data.
  • Force Update All By default, only the updated asset bundles will be built. With this checked, the system will rebuild all the asset bundles.
4

Asset Bundle Settings

prefabsystem_bundlesettings
  • Standalone Build asset bundles compatible with standalone and web player builds.
  • IOS Build asset bundles compatible with IOSbuilds.
  • Android Build asset bundles compatible with Android builds.

Note: If you don't have the target platform set in the build settings of your editor, the unity system will switch platform for you.

5

Actions

prefabsystem_actions
  • Sort Starts the sorting action of the system by searching through all the prefabs in the Relative prefab folder for updated prefabs. It will sort and copy all the prefabs into a special folder structure that will be used for building asset bundles and runtime use.
  • Build Bundles & Generate Service Builds all the updated bundles and updated the service used at runtime.
  • Upload All Bundles To Server Searches through the complete directory where your bundles are saved and tries to upload all of them with the settings found in the file system.
  • Sort, Build Bundles & Generate Service & Upload to Server Combines all the above actions into one single command.
D

Prefab Connections

When you want a connection between prefabs, use the PrefabConnection class. Whenever you want a connection to a prefab used in the system from a script, you should use a prefab connection, only use a direct reference to a prefab when you do not want the system to dynamically handle the prefab. Using a direct reference will also cause the prefab to be included in asset bundles but without any optimzation.
using Twoorb.PrefabSystem;

public class MySuperClass : MonoBehaviour
{
 	public PrefabConnection myConnection = null;
}
The prefab system works asynchronously with all game objects (handle by the system) and switching between local and remote prefabs is seamless. The Prefab Connection class is the front door to the system and is probably the class you will use the most from script.
1

Instantiating prefabs asynchronously

To retrieve an object from a prefab connection, call the GetGameObject() method. Instead of getting the game object itself, a specific component needs to be specified. If you don't have a specified component, pass in the Transform component.
myPrefabConnection.GetGameObject(
(Transform objectTransform) =>
{
	if (objectTransform != null)
		myInstantiatedTransform = objectTransform;
	else
		Debug.LogError("The operation failed!");
});
2

Cleanup

It is very important that objects are returned to the connection, this is necessary for the system to be able to unload unused assets. Do not destroy objects manually, all of this is handled by the prefab system automatically.

void OnDestroy()
{
	if (myInstantiatedTransform != null)
		myPrefabConnection.ReleaseGameObject(myInstantiatedTransform);
}

3

Complete Example

using System;
using Twoorb.PrefabSystem;

public class MySuperClass : MonoBehaviour
{
	public PrefabConnection		myPrefabConnection			= null;
	Transform					myInstantiatedTransform		= null;

	void Start()
	{
		myPrefabConnection.GetGameObject(
		(Transform objectTransform) =>
		{
			if (objectTransform != null)
				myInstantiatedTransform = objectTransform;
			else
				Debug.LogError("The operation failed!");
		});
	}

	public void DestroyMe()
	{
		if (myInstantiatedTransform != null)
			myPrefabConnection.ReleaseGameObject(myInstantiatedTransform);

		Destroy(gameObject);
	}
}
E

Sorting Groups

Assigning sorting groups to prefabs is an important step of optimizing the use of the Prefab System. The prefab system will look at all the remote prefabs in the same sorting group and determine if they have any assets shared between them. A dependency bundle with all the shared asset will be created, this will reduce the size of the asset bundles and lowers the load time for users.
1

Creating a new group

prefabsystem_sortinggroupTo create a new group, open the Group Manager, either from the "Manage Groups" button in the Prefab Identifier inspector or navigate to Twoorb -> Prefab System -> Group Manager. To create a new group in the group manager, type in your desired group name and press "Create Group".
2

Assigning groups

prefabsystem_sorting_selectTo assign a group to a prefab, navigate to the inspector window for that prefab and select the group from the drop down menu.

Don't forget to save your project after you've made any changes.
F

Content Groups

Similar to the sorting groups, prefabs can be divided into content groups, this feature makes it easier to preload certain assets. Use content groups to group assets that is to be used in the same context, e.g. World01, World02 etc. The content of a specific group can be retrieved during runtime. All prefabs will initially have the "Default" group assigned to them.
1

Creating a new group

prefabsystem_contentgroup To create a new group, open the Group Manager (same interface as for Sorting groups), either from the "Manage Groups" button in the Prefab Identifier inspector or navigate to Twoorb -> Prefab System -> Group Manager. To create a new group in the group manager, type in your desired group name and press "Create Group".
2

Retrieving the content at runtime

Call the GetContentGroup() method in the Runtime Prefab Service class to retrive a list of prefab IDs.
List<string> prefabList = RuntimePrefabService.GetContentGroup("WorldLevel01");