Unity3dResources加载预制体与Sprite精灵图片


using UnityEngine;

/// <summary>
/// 加载预制体,必须放在Resources文件夹
/// </summary>
public class AssetsGoblinWizard : MonoBehaviour
{
    ///<summary> 
    ///定义预制体Transform
    ///</summary>
    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);

    }

    /// <summary>
    /// 2:加载一张图片,Start执行,脚本挂载到空物体即可
    /// </summary>
    void Start()
    {
        //实例化空的游戏对象
        GameObject pic = new GameObject("Gray");
        //加载图片,进行接收
        Sprite spr = Resources.Load<Sprite>("Gray");
        //添加加载到的图片资源到2D精灵组件
        pic.AddComponent<SpriteRenderer>().sprite = spr;
    }
}


上一篇:phpmyadmin上在某数据库里创建函数


下一篇:Unity3d鼠标点击位置实例化点击动画特效-RPG游戏常用