Resources.Load()加载的是Resources文件夹下的预制体资源。建议只加载预制体资源或一些Object对象,因为Prefab会自动过滤掉对象上不需要的资源,减小包体大小。注意:Resources文件下的资源在发布时会打成一个包。
代码实现:
//加载物体挂载的父物体
private GameObject Cube;
private void Start()
{
Cube = GameObject.Find("Cube");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
LoadCube();
}
}
void LoadCube()
{
//加载Resources文件夹下名字叫做display的预制物体
GameObject pre = (GameObject)Resources.Load("Sphere");
//实例化到场景中
GameObject sphere = Instantiate(pre);
//将物体绑定到父物体下面
sphere.transform.parent = Cube.transform;
//给物体缩放赋值
sphere.transform.localScale = new Vector3(1, 1, 1);
//给物体坐标赋值
sphere.transform.localPosition = new Vector3(1, 0, 0);
//给物体角度赋值
Quaternion quaternion = Quaternion.Euler(90, 0, 0);
sphere.transform.localRotation = quaternion;
}