Unity3D 运行时实例化预设 要点
更新:HHH   时间:2023-1-7


效果图

实现步骤要点:

1、添加Cube游戏

2、创建预设对象

3、创建空游戏对象,用于绑定执行预设代码

4、选中gameObject在Inspector检查器中添加和绑定以下代码

using UnityEngine;
using System.Collections;
public class prefab : MonoBehaviour {
    public GameObject ViewCenter;
    private float i=-10.0f;
    // Use this for initialization
    void Start () {
    }
                                                                                                                                             
    // Update is called once per frame(Pre, transform.position, transform.rotation);
    void Update () {
        if (i < 1f) {
            i++;
                }
        GameObject instance=Instantiate(ViewCenter,transform.position*i,transform.rotation) as GameObject;
    }
}

方法一:通过检查器直接获取预设对象


方法二:代码获取,把代码修改

using UnityEngine;
using System.Collections;
public class prefab : MonoBehaviour
{
    public GameObject ViewCenter;
    private float i = -10.0f;
    // Use this for initialization
    void Start ()
    {
        ViewCenter = GameObject.Find ("gameObject");
    }
      
    // Update is called once per frame(Pre, transform.position, transform.rotation);
    void Update ()
    {
        if (i < 1f) {
            i++;
        }
        GameObject instance = Instantiate (ViewCenter, transform.position * i, transform.rotation) as GameObject;
    }
}


返回游戏开发教程...