将Prefab中的Children设置为单独的GameObjects

时间:2018-05-09 06:30:05

标签: c# unity3d gameobject

当产生预制件时,我试图让构成预制件的每个单独的GameObject运行其各自的脚本。基本上, 我希望预制品在产卵后破裂;离开个人游戏对象。

我将Prefab中的GameObjects作为Empty的子项。任何建议。

enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateFab : MonoBehaviour
{
    public Transform Spawnpoint;
    public Rigidbody Prefab;

    public void OnClick()
    {
        Rigidbody RigidPrefab;
        RigidPrefab = Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation) as Rigidbody;
    }

    public void DetachFromParent()
    {
        // Detaches the transform from its parent.
        transform.parent = null;
    }
}

多维数据集脚本

using UnityEngine;
using UnityEditor;
using System.IO;

public class WallClick : MonoBehaviour
{
    string path;
    public MeshRenderer mRenderer;


    public void OpenExplorer()
    {
        path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
        GetImage();
    }

    void GetImage()
    {
        if (path != null)
        {
            UpdateImage();
        }
    }

    void UpdateImage()
    {
        byte[] imgByte = File.ReadAllBytes(path);
        Texture2D texture = new Texture2D(2, 2);
        texture.LoadImage(imgByte);

        mRenderer.material.mainTexture = texture;
    }
}

2 个答案:

答案 0 :(得分:0)

如果您生成预制件,请为衍生物品提供参考。

var obj = Instantiate(prefabGameobject);

然后,您可以使用衍生对象做任何您喜欢的事情

var script = obj.AddComponent<YourScript>();

然后您可以修改脚本的变量等。 你的预制件不会碰到。

答案 1 :(得分:0)

你在这里使用的"users" => array:2 [▼ 0 => "1", 1 => "3" ] 指的是你的transform.parent脚本附加到的游戏对象的变换,而不是预制件的儿童(较小的立方体)。

正确的方法是:

CreateFab.cs