Extending Assets for Unity
Overview
Quantum assets can be extended with Unity-specific data not relevant for the simulation like data for the UI (colors, texts, icons...). This is done with the use of partial classes.
Example
Let's take the CharacterSpec asset as an example. Its ScriptableObject-based wrapper in Unity is called CharacterSpecAsset and is the type which needs to extended.
C#
public partial class CharacterSpecAsset {
[Header("Unity")]
public Sprtie Icon;
public Color Color;
public string DisplayName;
}
The newly created partial class needs to be added to the same assembly as the original definition of CharacterSpecAsset. By default, all Unity-side Quantum code belongs to the PhotonQuantum assembly.
To ensure the partial class belongs to the correct assembly use one of the following approaches:
- Save the class in
Assets/Photon/Quantum/Userdirectory. - Save the class in any directory that has an AssemblyDefinitionReference asset pointing to the
PhotonQuantumassembly. - Delete
Assets/Photon/Quantum/PhotonQuantum.asmdef. This will make Quantum a part of the main assembly. Note that this step needs to be repeated after each Quantum SDK update.
Access at Runtime
To access the extra fields at runtime, use the UnityDB.FindAsset<T>() method.
C#
CharacterSpecAsset characterSpecAsset = UnityDB.FindAsset<CharacterSpecAsset>(guid);
Debug.Log(characterSpecAsset.DisplayName);
Alternatively, the code-generated GetUnityAsset() extension methods can be used:
C#
CharacterSpec characterSpec = frame.FindAsset<CharacterSpec>(guid);
CharacterSpecAsset characterSpecAsset = characterSpec.GetUnityAsset();
Debug.Log(characterSpecAsset.DisplayName);
Both of the approaches will result in the asset being loaded into Quantum's AssetDB using the appropriate method, as discussed here: Resources, Addressables and Asset Bundles.
Access at Edit-time
To load an asset using its path while in the Unity Editor, the UnityEditor.AssetDataBase.LoadAssetAtPath<T>() method can be used.
C#
CharacterSpecAsset characterSpecAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<CharacterSpecAsset>(path);
Debug.Log(characterSpecAsset.DisplayName);
Alternatively, the asset can be loaded using its AssetGuid via the UnityDB.FindAssetForInspector() method and casting the result to the correct type.
C#
CharacterSpecAsset characterSpecAsset = (CharacterSpecAsset)UnityDB.FindAssetForInspector(guid);
Debug.Log(characterSpecAsset.DisplayName);
Back to top