# Custom resources

Custom resources can be used to save the developer's game data, generally used to store some globally unique data.

The difference between the custom resource and the script Component in Prefab/Scene is that although the Script Component can also be customized to save the edited data through @property, when the Prefab and Scene are instantiated multiple times, the data is actually multiple times Created and not related to each other. When developers want to reduce memory and are sure that they are sharing the same data, they can use custom resources. After being loaded by the loader, the custom resources, like other small game resources, generate a global singleton based on the uuid. No matter it is used by any Prefab/Scene, only one resource instance is shared.

Create your own Scriptable Asset, in the tool Project window, right-click on the blank space and create a new >> ScriptableAsset, a script template will be created. It defines a class based on your file name. At this time, when you right-click to create, the name of the custom resource you just created will appear in the list, and you will see an .asset resource after creation. The data structure stored in this resource, like the Script Component, is defined according to the @property in the script. At this time, the developer clicks to select the .asset just created, and can modify the data in the Inspector. You can create multiple .asset resources based on a class, and each has different data.

@engine.decorators.CreateAssetMenu('saTest','saTest', 1)
@engine.decorators.serialize("saTest")
export default class saTest extends engine.ScriptableAsset {
  @engine.decorators.property({
    type: engine.TypeNames.String
  })
  public age: number = 10;
}

Using Scriptable Asset, the developer can first create a Script Component, and then import the Scriptable Asset just created as an attribute of their @property. In this way, you can see such a UI in the editor, and you can set the .asset resource you just created. Or use it directly with loader.load.

import saTest from'./saTest'

@engine.decorators.serialize("testScript")
export default class testScript extends engine.Script {
  @engine.decorators.property({
    type: saTest
  })
  public mySaTest: saTest
}
点击咨询小助手