unity制作预制体,动态加载预制体,实用资源的导出 您所在的位置:网站首页 unity代码添加预制件 unity制作预制体,动态加载预制体,实用资源的导出

unity制作预制体,动态加载预制体,实用资源的导出

2024-07-16 03:35| 来源: 网络整理| 查看: 265

1,制作预制体

可参考  创建预制体Prefab

新建一个物体拖到Assets下的任何一个文件夹下即可,预制体的颜色会变蓝色。

2 如果制作的预制体上有脚本,当预制体被动态加载时脚本也是可以加载的。

Rotate.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Rotate : MonoBehaviour { private float speed = 0.5f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { this.transform.Rotate (Vector3.up * speed); } }

4, 动态加载预制体

LoadCube.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; public class LoadCube : MonoBehaviour { // Use this for initialization void Start () { GameObject hp_bar = (GameObject)Resources.Load("Cube"); hp_bar = Instantiate(hp_bar); hp_bar.name = "Cube"; } // Update is called once per frame void Update () { } }

5 运行结果

 

 

6,经验告诉我,可以把一个工程里面常用的多个物体以及脚本制作为一个预制体。以后用会很方便,比如机械产品的装配体,在unity中把位置装好后,如果工程出了问题,里面的装配关系要全重新做,在写程序时出错等时很常见的,很有可能会做重复无用的多次装配。这时预制体的好处就大有用途了。

unity资源包package的导入导出

 

记得要把制作预制体的所有用的东西如材质,脚本,预制体的母预制体都要一起拿过来在新的工程中用,不然会出错。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有