本文共 966 字,大约阅读时间需要 3 分钟。
using UnityEngine;////// 加载预制体,必须放在Resources文件夹/// public class AssetsGoblinWizard : MonoBehaviour{ //////定义预制体Transform /// private Transform gob; void Start() { //通过名字查到到预制体,类型为Transform,进行转换,接收 Transform GoblinWizard = Resources.Load ("GoblinWizard", typeof(Transform)) as Transform; //实例化当前预制体并且获取 gob = Instantiate(GoblinWizard); } void Update() { //控制游戏对象的移动 float hor = Input.GetAxis("Horizontal"); float ver = Input.GetAxis("Vertical"); gob.Translate(Vector3.forward * ver * Time.deltaTime * 10); gob.Rotate(Vector3.up * hor * Time.deltaTime * 120); } ////// 2:加载一张图片,Start执行,脚本挂载到空物体即可 /// void Start() { //实例化空的游戏对象 GameObject pic = new GameObject("Gray"); //加载图片,进行接收 Sprite spr = Resources.Load("Gray"); //添加加载到的图片资源到2D精灵组件 pic.AddComponent ().sprite = spr; }}
转载地址:http://fomhl.baihongyu.com/