# resource/visualasset (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/resource/visualasset/

Visual asset. (Reads a document file saved by the visual editor and makes a Visual)

## VisualAsset extends Asset

Visual asset. (Reads a document file saved by the visual editor and makes a Visual)
- The file is the UIDocument JSON saved by the visual editor (tools/visualeditor). (Scene *.scene.json, prefab *.prefab.json, { version, root, frameSize? })
- load fetches the text, checks that it is a JSON object with a root object, stores a Visual holding that text, and counts as loaded.
  A failed response (404 and so on) or a malformed document is rejected and stays unloaded.
- Visual.createNode builds the node tree. (A new tree on every call)

Example: Read a visual editor prefab and build nodes

```js
import { VisualAsset, UIDocument } from "./build/vanilla.js";

const documentData = {
	version: 1,
	root: { type: "WorldNode", name: "Panel", contentSize: [200, 120], components: [], children: [] },
};
const documentUrl = "data:application/json," + encodeURIComponent(JSON.stringify(documentData));
const visualAsset = new VisualAsset();
await visualAsset.load(documentUrl);
const visual = visualAsset.getVisual();
const panelNode = visual.createNode(new UIDocument());
console.log(visualAsset.getAssetType(), visualAsset.isLoaded(), panelNode.getName());
```

- `constructor()` (constructor, since 0.0.9): Constructor.
- `async load(assetPath)` (method, since 0.0.9): Loads the asset asynchronously.
  - `assetPath` string: Path of a document file saved by the visual editor. (Does nothing when already loaded)
  - returns Promise<void>: Promise that resolves after reading and storing the Visual. (Rejected for a failed response (404 and so on) or a malformed document)
- `unload()` (method, since 0.4.0): Unload the asset. (Also clears the stored Visual)
  - returns Promise<void> | undefined: An already resolved promise if not loaded, undefined if it was loaded.
- `static createVisualFromText(documentText)` (static, since 0.4.0): Make a Visual from document text. (Static)
  - `documentText` string: UIDocument JSON text saved by the visual editor.
  - returns Visual: Visual holding that text. (Throws when it is not a JSON object or has no root object)

  Example: Make one straight from document text

  ```js
  import { VisualAsset, UIDocument } from "./build/vanilla.js";
  
  const documentText = JSON.stringify({ version: 1, root: { type: "WorldNode", name: "Icon", components: [], children: [] } });
  const visual = VisualAsset.createVisualFromText(documentText);
  const iconNode = visual.createNode(new UIDocument());
  console.log(iconNode.getName());
  ```

- `setVisual(visual)` (method, since 0.4.0): Sets the visual.
  - `visual` Visual | null: Visual to store. (null clears it, does not change the loaded state)
- `getVisual()` (method, since 0.4.0): Returns the visual. (null if none)
  - returns Visual | null: Stored visual. (The one holding the document read by load, or the one set with setVisual)
