Resources.Load 加载

static function Load (path : string) : Object

Description描述

Loads an asset stored at path in the Resources folder.

加载储存在Resources文件夹中path处的资源。

Returns the asset at path if it can be found otherwise returns null. The path is relative to the Resources folder, extensions must be omitted. The Resources folder can be anywhere inside the Assets folder.

如果发现,返回所在path处的资源,否则返回null。只有type类型的物体将被返回. Path相对于Resources文件夹,扩展名必须被忽略。Resouces文件夹可以在Assets文件夹中的任何位置。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Start() {
		GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane);
		go.renderer.material.mainTexture = Resources.Load("glass");
	}
}
// Assigns a texture named "Assets/Resources/glass" to a Plane.
//指定一个名为"Assets/Resources/glass" 的纹理到一个平面

function Start () {
	var go = new GameObject.CreatePrimitive(PrimitiveType.Plane);
	go.renderer.material.mainTexture = Resources.Load("glass");
}

另一个例子:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Start() {
		GameObject instance = Instantiate(Resources.Load("enemy"));
	}
}
// Instantiates a prefab at the path "Assets/Resources/enemy".
//在路径 "Assets/Resources/enemy"实例一个预设

function Start () {
	var instance : GameObject = Instantiate(Resources.Load("enemy"));
}

•static function Load (path : string, type : Type) : Object

Description描述

Loads an asset stored at path in the Resources folder.

加载储存在Resources文件夹中path处的资源。

Returns the asset at path if it can be found otherwise returns null. Only objects of type will be returned. The path is relative to the Resources folder, extensions must be omitted. The Resources folder can be anywhere inside the Assets folder.

如果发现,返回所在path处的资源,否则返回null,只返回type的物体。路径相对于Resources文件夹,扩展必须被省略。Resources文件夹可以在Assets文件夹内的任何地方。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Start() {
		GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
		go.renderer.material.mainTexture = Resources.Load("glass", typeof(Texture2D));
	}
}
// Assigns a texture named "glass" to a Plane.
//指定一个名为glass的纹理给平面

function Start () {
	var go = new GameObject.CreatePrimitive(PrimitiveType.Cube);
	go.renderer.material.mainTexture = Resources.Load("glass", Texture2D);
}
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	void Start() {
		GameObject instance = Instantiate(Resources.Load("enemy", typeof(GameObject)));
	}
}
// Instantiates a prefab named "Resources/enemy".
//实例一个预设名为"Resources/enemy"

function Start () {
	var instance : GameObject = Instantiate(Resources.Load("enemy", GameObject));
}
最后修改:2011年3月13日 Sunday 19:48

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。