mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 16:52:43 +08:00
document for common assets (#4606)
This commit is contained in:
committed by
Weiliang Wang
parent
62c38b8198
commit
137a06624f
@@ -27,25 +27,15 @@ import {ccclass, property} from '../core/data/class-decorator';
|
||||
import { Asset } from './asset';
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Class for JSON file. When the JSON file is loaded, this object is returned.
|
||||
* The parsed JSON object can be accessed through the `json` attribute in it.<br>
|
||||
* If you want to get the original JSON text, you should modify the extname to `.txt`
|
||||
* so that it is loaded as a `TextAsset` instead of a `JsonAsset`.
|
||||
*
|
||||
* !#zh
|
||||
* JSON 资源类。JSON 文件加载后,将会返回该对象。可以通过其中的 `json` 属性访问解析后的 JSON 对象。<br>
|
||||
* 如果你想要获得 JSON 的原始文本,那么应该修改源文件的后缀为 `.txt`,这样就会加载为一个 `TextAsset` 而不是 `JsonAsset`。
|
||||
*
|
||||
* @class JsonAsset
|
||||
* @extends Asset
|
||||
* Json 资源。
|
||||
* Json 资源加载后将直接解析为对象。如果你希望获得 JSON 的原始文本,你需要使用文本资源(使用文件名后缀“.txt”)。
|
||||
*/
|
||||
@ccclass('cc.JsonAsset')
|
||||
export default class JsonAsset extends Asset {
|
||||
/**
|
||||
* @property json - The loaded JSON object.
|
||||
* 解析后的对象。
|
||||
*/
|
||||
@property()
|
||||
@property
|
||||
public json: object | null = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,36 +29,21 @@ import { Scene } from '../scene-graph';
|
||||
import { Asset } from './asset';
|
||||
|
||||
/**
|
||||
* !#en Class for scene handling.
|
||||
* !#zh 场景资源类。
|
||||
* @class SceneAsset
|
||||
* @extends Asset
|
||||
*
|
||||
* 场景资源。
|
||||
*/
|
||||
@ccclass('cc.SceneAsset')
|
||||
export default class SceneAsset extends Asset {
|
||||
/**
|
||||
* @property {Scene} scene
|
||||
* @default null
|
||||
* 场景结点。
|
||||
*/
|
||||
@property
|
||||
public scene: Scene | null = null;
|
||||
|
||||
/**
|
||||
* !#en Indicates the raw assets of this scene can be load after scene launched.
|
||||
* !#zh 指示该场景依赖的资源可否在场景切换后再延迟加载。
|
||||
* @property {Boolean} asyncLoadAssets
|
||||
* @default false
|
||||
* 指示该场景依赖的资源可否在场景切换后再延迟加载。
|
||||
*/
|
||||
@property
|
||||
public asyncLoadAssets = false;
|
||||
|
||||
//// backup prefab assets in editor
|
||||
//// {string} assetUuid: {cc.Node} rootInPrefab
|
||||
// _prefabDatas: {
|
||||
// default: null,
|
||||
// editorOnly: true
|
||||
// }
|
||||
}
|
||||
|
||||
cc.SceneAsset = SceneAsset;
|
||||
|
||||
@@ -28,12 +28,7 @@ import {ccclass} from '../core/data/class-decorator';
|
||||
import { Asset } from './asset';
|
||||
|
||||
/**
|
||||
* !#en Class for script handling.
|
||||
* !#zh Script 资源类。
|
||||
* @class _Script
|
||||
* @extends Asset
|
||||
*
|
||||
* @private
|
||||
* 脚本资源基类。
|
||||
*/
|
||||
@ccclass('cc.Script')
|
||||
export class Script extends Asset {
|
||||
@@ -41,11 +36,7 @@ export class Script extends Asset {
|
||||
cc._Script = Script;
|
||||
|
||||
/**
|
||||
* !#en Class for JavaScript handling.
|
||||
* !#zh JavaScript 资源类。
|
||||
* @class _JavaScript
|
||||
* @extends Asset
|
||||
*
|
||||
* JavaScript 脚本资源。
|
||||
*/
|
||||
@ccclass('cc.JavaScript')
|
||||
export class JavaScript extends Script {
|
||||
@@ -53,11 +44,7 @@ export class JavaScript extends Script {
|
||||
cc._JavaScript = JavaScript;
|
||||
|
||||
/**
|
||||
* !#en Class for TypeScript handling.
|
||||
* !#zh TypeScript 资源类。
|
||||
* @class TypeScript
|
||||
* @extends Asset
|
||||
*
|
||||
* Typescript 脚本资源。
|
||||
*/
|
||||
@ccclass('cc.TypeScript')
|
||||
export class TypeScript extends Script {
|
||||
|
||||
@@ -28,26 +28,18 @@ import {ccclass, property} from '../core/data/class-decorator';
|
||||
import { Asset } from './asset';
|
||||
|
||||
/**
|
||||
* !#en Class for text file.
|
||||
* !#zh 文本资源类。
|
||||
* @class TextAsset
|
||||
* @extends Asset
|
||||
* 文本资源。
|
||||
*/
|
||||
@ccclass('cc.TextAsset')
|
||||
export default class TextAsset extends Asset {
|
||||
/**
|
||||
* @property {String} text - The text contents of the resource.
|
||||
* 此资源包含的文本。
|
||||
*/
|
||||
@property
|
||||
public text = '';
|
||||
|
||||
/**
|
||||
* Returns the text content of the asset.
|
||||
*
|
||||
* JavaScript calls the toString() method automatically when an asset is to be represented as a text value or when a texture is referred to in a string concatenation.
|
||||
*
|
||||
* @method toString
|
||||
* @return {String}
|
||||
* 重载标准的 `toString()` 方法。
|
||||
*/
|
||||
public toString () {
|
||||
return this.text;
|
||||
|
||||
@@ -29,6 +29,9 @@ import { GFXDevice, GFXFeature } from '../gfx/device';
|
||||
import { Asset } from './asset';
|
||||
import { PixelFormat } from './asset-enum';
|
||||
|
||||
/**
|
||||
* 内存图像源。
|
||||
*/
|
||||
export interface IMemoryImageSource {
|
||||
_data: ArrayBufferView | null;
|
||||
_compressed: boolean;
|
||||
@@ -37,6 +40,9 @@ export interface IMemoryImageSource {
|
||||
format: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图像资源的原始图像源。可以来源于 HTML 元素也可以来源于内存。
|
||||
*/
|
||||
export type ImageSource = HTMLCanvasElement | HTMLImageElement | IMemoryImageSource;
|
||||
|
||||
function fetchImageSource (imageSource: ImageSource) {
|
||||
@@ -44,7 +50,7 @@ function fetchImageSource (imageSource: ImageSource) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ImageAsset.
|
||||
* 图像资源。
|
||||
*/
|
||||
@ccclass('cc.ImageAsset')
|
||||
export class ImageAsset extends Asset {
|
||||
@@ -59,32 +65,45 @@ export class ImageAsset extends Asset {
|
||||
this.reset(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 此图像资源的图像数据。
|
||||
*/
|
||||
get data () {
|
||||
return '_data' in this._nativeData ? this._nativeData._data : this._nativeData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 此图像资源的像素宽度。
|
||||
*/
|
||||
get width () {
|
||||
return this._nativeData.width || this._width;
|
||||
}
|
||||
|
||||
/**
|
||||
* 此图像资源的像素高度。
|
||||
*/
|
||||
get height () {
|
||||
return this._nativeData.height || this._height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 此图像资源的像素格式。
|
||||
*/
|
||||
get format () {
|
||||
return this._format;
|
||||
}
|
||||
|
||||
/**
|
||||
* 此图像资源是否为压缩像素格式。
|
||||
*/
|
||||
get isCompressed () {
|
||||
return this._format >= PixelFormat.RGB_ETC1 && this._format <= PixelFormat.RGBA_PVRTC_4BPPV1;
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The url of the texture, this could be empty if the texture wasn't created via a file.
|
||||
* !#zh
|
||||
* 图像文件的 url,当图像不是由文件创建时值可能为空
|
||||
* 此图像资源的原始图像源的 URL。当原始图像元不是 HTML 文件时可能为空。
|
||||
* @deprecated 请转用 `this.nativeUrl`。
|
||||
*/
|
||||
// TODO - use nativeUrl directly
|
||||
get url () {
|
||||
return this._url;
|
||||
}
|
||||
@@ -129,6 +148,10 @@ export class ImageAsset extends Asset {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置此图像资源使用的原始图像源。
|
||||
* @param data 新的原始图像源。
|
||||
*/
|
||||
public reset (data: ImageSource) {
|
||||
if (!(data instanceof HTMLElement)) {
|
||||
// this._nativeData = Object.create(data);
|
||||
|
||||
@@ -40,3 +40,5 @@ export { TTFFont } from './ttf-font';
|
||||
export { LabelAtlas } from './label-atlas';
|
||||
export { BitmapFont } from './bitmap-font';
|
||||
export { Font } from './font';
|
||||
import * as textureUtil from './texture-util';
|
||||
export { textureUtil };
|
||||
|
||||
@@ -29,25 +29,20 @@ import { CCObject } from '../core/data/object';
|
||||
import { isChildClassOf } from '../core/utils/js';
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* The base class for registering asset types.
|
||||
* !#zh
|
||||
* 注册用的资源基类。
|
||||
*
|
||||
* @class RawAsset
|
||||
* @extends Object
|
||||
* 原生资源的基类。内部使用。
|
||||
* @private
|
||||
*/
|
||||
@ccclass('cc.RawAsset')
|
||||
export class RawAsset extends CCObject {
|
||||
/**
|
||||
* For internal usage.
|
||||
* 内部使用。
|
||||
*/
|
||||
public static isRawAssetType (ctor: Function) {
|
||||
return isChildClassOf(ctor, cc.RawAsset) && !isChildClassOf(ctor, cc.Asset);
|
||||
}
|
||||
|
||||
/**
|
||||
* For internal usage.
|
||||
* 内部使用。
|
||||
*/
|
||||
// @ts-ignore
|
||||
public _uuid: string;
|
||||
|
||||
@@ -24,50 +24,62 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
import { assertID, error } from '../core/platform/CCDebug';
|
||||
import { LoadCallbackParams, loader } from '../load-pipeline';
|
||||
import { ImageAsset, ImageSource } from './image-asset';
|
||||
|
||||
export type LoadImageCallback<T> = (
|
||||
this: T | undefined,
|
||||
...args: LoadCallbackParams<ImageAsset>
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* cc.textureUtil is a singleton object, it can load cc.Texture2D asynchronously
|
||||
* @class textureUtil
|
||||
* @static
|
||||
* 加载指定的图像资源。
|
||||
* @param url 图像资源的链接。
|
||||
* @param callback 回调函数。
|
||||
* @param target 回调函数的 `this` 参数。
|
||||
* @returns 图像资源,返回时可能还未完成加载;加载完成或失败时会调用回调函数。
|
||||
*/
|
||||
export function loadImage<T> (url: string, callback?: LoadImageCallback<T>, target?: T) {
|
||||
assertID(!!url, 3103);
|
||||
|
||||
export function loadImage (url: string, cb, target) {
|
||||
cc.assertID(url, 3103);
|
||||
|
||||
let imageAsset = cc.loader.getRes(url);
|
||||
let imageAsset = loader.getRes<ImageAsset>(url);
|
||||
if (imageAsset) {
|
||||
if (imageAsset.loaded) {
|
||||
if (cb){
|
||||
cb.call(target, imageAsset);
|
||||
if (callback){
|
||||
callback.call(target, null, imageAsset);
|
||||
}
|
||||
|
||||
return imageAsset;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
imageAsset.once('load', () => {
|
||||
if (cb) {
|
||||
cb.call(target, imageAsset);
|
||||
if (callback) {
|
||||
callback.call(target, null, imageAsset!);
|
||||
}
|
||||
}, target);
|
||||
return imageAsset;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
imageAsset = new ImageAsset();
|
||||
imageAsset.url = url;
|
||||
cc.loader.load({ url, imageAsset }, (err, asset) => {
|
||||
loader.load({url, imageAsset}, (err, asset) => {
|
||||
if (err) {
|
||||
return cb && cb.call(target, err || new Error('Unknown error'));
|
||||
if (callback) {
|
||||
callback.call(target, err || new Error('Unknown error'));
|
||||
}
|
||||
return imageAsset;
|
||||
}
|
||||
if (cb) {
|
||||
cb.call(target, null, asset);
|
||||
if (callback) {
|
||||
callback.call(target, null, asset);
|
||||
}
|
||||
});
|
||||
return imageAsset;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存指定的图像源,为它指定链接。此后,可以通过该链接直接加载它。
|
||||
* @param url 指定的链接。
|
||||
* @param image 缓存的图像源。
|
||||
*/
|
||||
export function cacheImage (url: string, image: ImageSource) {
|
||||
if (url && image) {
|
||||
const imageAsset = new ImageAsset(image);
|
||||
@@ -78,11 +90,16 @@ export function cacheImage (url: string, image: ImageSource) {
|
||||
content: imageAsset,
|
||||
complete: false,
|
||||
};
|
||||
cc.loader.flowOut(item);
|
||||
loader.flowOut(item);
|
||||
return imageAsset;
|
||||
}
|
||||
}
|
||||
// TODO:
|
||||
|
||||
/**
|
||||
* 尝试加载图像资源的实际数据。
|
||||
* @param imageAsset 图像资源。
|
||||
* @param callback 回调函数。
|
||||
*/
|
||||
export function postLoadImage (imageAsset: ImageAsset, callback?: Function) {
|
||||
if (imageAsset.loaded) {
|
||||
if (callback) {
|
||||
@@ -97,14 +114,14 @@ export function postLoadImage (imageAsset: ImageAsset, callback?: Function) {
|
||||
return;
|
||||
}
|
||||
// load image
|
||||
cc.loader.load({
|
||||
loader.load({
|
||||
url: imageAsset.nativeUrl,
|
||||
// For image, we should skip loader otherwise it will load a new ImageAsset
|
||||
skips: imageAsset.isCompressed ? undefined : ['Loader'],
|
||||
}, (err, image) => {
|
||||
if (image) {
|
||||
if (CC_DEBUG && image instanceof cc.ImageAsset) {
|
||||
return cc.error('internal error: loader handle pipe must be skipped');
|
||||
if (CC_DEBUG && image instanceof ImageAsset) {
|
||||
return error('internal error: loader handle pipe must be skipped');
|
||||
}
|
||||
if (!imageAsset.loaded) {
|
||||
imageAsset._nativeAsset = image;
|
||||
@@ -115,9 +132,3 @@ export function postLoadImage (imageAsset: ImageAsset, callback?: Function) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
cc.textureUtil = {
|
||||
loadImage,
|
||||
cacheImage,
|
||||
postLoadImage,
|
||||
};
|
||||
|
||||
+1159
-1159
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,5 @@
|
||||
type LoadSuccessParams<T> = Parameters<(error: null, asset: T) => void>;
|
||||
|
||||
type LoadErrorParams<T> = Parameters<(error?: Error) => void>;
|
||||
|
||||
export type LoadCallbackParams<T> = LoadSuccessParams<T> | LoadErrorParams<T>;
|
||||
@@ -29,4 +29,5 @@ export {default as Downloader} from './downloader';
|
||||
export {default as Loader} from './loader';
|
||||
export {default as LoadingItems} from './loading-items';
|
||||
export {default as Pipeline} from './pipeline';
|
||||
export {default as loader} from './CCLoader';
|
||||
export {default as loader} from './CCLoader';
|
||||
export * from './callback-params';
|
||||
Reference in New Issue
Block a user