exdev / ex2d-dev

2D framework and pipeline for Unity3D
ex-dev.com/ex2d
GNU General Public License v3.0
6 stars 3 forks source link

Runtime instantiate prefab will never show in the scene #35

Closed jwu closed 10 years ago

jwu commented 11 years ago

I've been using ex2D for a few days now, and I'm having trouble instantiating an exSprite at runtime.

I'm setting up my exSprite, I saved it as a prefab, I delete it from the scene hierarchy, I start the game and at a certain point I instantiate that prefab using Unity's Instantiate method. I also set this new object as a child of my only layer (using transform.parent). But it seems that this new exSprite is never drawn.

Am I forgetting something ?

在我的测试中, 我是这么来做的:

我做了一个 EmptyGO, EmptyGO 底下放置了一个 exSpriteGO. 然后把这样一个层级关系的 GO 做成 prefab.

之后用以下代码 spawn

using UnityEngine;
using System.Collections;

public class Test_SimpleSpawn : MonoBehaviour {
    public GameObject prefab;
    public GameObject layer;

    // Use this for initialization
    IEnumerator Start () {
        yield return new WaitForSeconds(3.0f);
        GameObject newGO = GameObject.Instantiate (prefab) as GameObject;
        newGO.transform.parent = layer.transform;
    }

    // Update is called once per frame
    void Update () {

    }
}
jwu commented 10 years ago

需要采用 exLayer.AddChild 来添加。而不能用 transform.parent.