CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
[Asset Support] DeployType extension

Sample description

Overview


This sample demonstrates how to add/use a custom DeployType. Press the button in the window to play/stop the Cue.

Scene information


Middleware CRI ADX (CRI Atom)
Sample Asset Support Sample
Location /cri/unity/samples/UnityProject/Assets/CriAssetSamples/Scenes/
Scene file Scene_07_CustomDeployType.unity
ACB Asset /cri/unity/samples/UnityProject/Assets/CriAssetSamples/Data/PinballMain.acb


Description of the program

By implementing the ICriAssetImplCreator interface in a class, Setting the "Deploy Type" (a CRI asset) is added.
The DeployType in this sample stores the data in the asset itself. Once downloaded, it is written to the cache so that the file can be loaded.
Attention
By using DeployType in this sample, the data is included in both the asset and the cache.
Therefore, we recommend to use CRI Addressables or your own DeployType to download or implement Non-Asset CRI data.

Implementing ICriAssetImpl

The data to be included in the CRI asset is abstracted by the ICriAssetImpl interface.
Having an instance of CRI asset inherited from ICriMemoryAssetImpl or ICriFileAssetImpl ,
makes it possible to represent the relationships with the actual data in various ways.

This sample defines a class that inherits ICriFileAssetImpl .
// To retain the information in the asset, the Serializable attribute is required
[System.Serializable]
public class Scene_07_CustomDeployType : ICriFileAssetImpl
{
// Serialize and retain the data in the asset
[SerializeField, HideInInspector]
byte[] data;
[SerializeField]
string fileName;
public Scene_07_CustomDeployType(byte[] data, string fileName)
{
this.data = data;
this.fileName = fileName;
}
public string Path => System.IO.Path.Combine(CriWare.Common.installCachePath, fileName);
public ulong Offset => 0;
public long Size { get; }
public bool IsReady { get; private set; }
public void OnEnable() {
File.WriteAllBytes(Path, data);
IsReady = true;
#if !UNITY_EDITOR
data = null;
#endif
}
public void OnDisable() => File.Delete(Path);
}
With this implementation, the data is included in the asset as an array of bytes.
By writing the data into the cache folder with the OnEnable method (called when the asset is instantiated),
the data can be loaded as a file.
This makes it possible to include streaming data as part of an AssetBundle without using Addressables.
However, in this case, the data will be added to both the asset and the cache.

Implementing ICriAssetImplCreator

The process of associating Non-Asset CRI data to an asset when importing a CRI asset is abstracted by the ICriAssetImplCreator interface.
The CreateAssetImpl method in the class inherited from ICriAssetImplCreator will handle Non-Asset CRI data.
CreateAssetImpl returns an instance of the ICriFileAssetImpl interface.
This sample returns an instance of the Scene_07_CustomDeployType class.
public ICriAssetImpl CreateAssetImpl(AssetImportContext ctx)
{
// Read all contents of the target file
var data = File.ReadAllBytes(ctx.assetPath);
// Generate a hash to be used for the path when exporting the cache
var md5 = MD5.Create();
var hash = md5.ComputeHash(data);
md5.Clear();
// Instantiate and return Scene_07_CustomDeployType with the target data.
return new Scene_07_CustomDeployType(data, BitConverter.ToString(hash).ToLower().Replace("-", ""));
}
Define a class that inherits from ICriAssetImplCreator as the Editor script.
The use of the UNITY_EDITOR macro prevents this sample to run outside of the editor (i.e., at runtime).

The display name in the importer can be specified by using the CriDisplayName attribute.
[System.Serializable, CriDisplayName("CustomDeploySample")]