JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate.
In PlayCanvas, JSON assets are used to store various types of structured data. Some example use cases are:
- Configuration files
- Data for procedural generation
- Storing game settings
- Level design data
Accessing JSON Data in Scripts
To access data from a JSON asset in a script:
- Add the JSON asset to the script as an attribute.
- Access the JSON asset's resource which is the object parsed from the JSON data.
例
var JsonScript = pc.createScript('jsonScript');
// Define an attribute to hold the JSON asset
JsonScript.attributes.add('jsonAsset', { type: 'asset', assetType: 'json' });
JsonScript.prototype.initialize = function () {
if (this.jsonAsset) {
// Get the JSON asset's resource (an object)
var jsonData = this.jsonAsset.resource;
// Example: Accessing data from the JSON object
if (jsonData.someDataField) {
console.log("Data from JSON:", jsonData.someDataField);
}
}
};