C# 将预置中的子对象设置为单个游戏对象
当生成一个预置时,我试图让组成预置的每个游戏对象运行各自的脚本。基本上, 我希望预制在产卵后破裂;留下单个游戏对象 我将游戏对象作为一个空游戏的子对象放在预置中。任何建议 多维数据集脚本C# 将预置中的子对象设置为单个游戏对象,c#,unity3d,gameobject,C#,Unity3d,Gameobject,当生成一个预置时,我试图让组成预置的每个游戏对象运行各自的脚本。基本上, 我希望预制在产卵后破裂;留下单个游戏对象 我将游戏对象作为一个空游戏的子对象放在预置中。任何建议 多维数据集脚本 using UnityEngine; using UnityEditor; using System.IO; public class WallClick : MonoBehaviour { string path; public MeshRenderer mRenderer;
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;
}
}
如果生成预置,请为生成的项提供引用
var obj = Instantiate(prefabGameobject);
然后,可以对生成的对象执行任何操作
var script = obj.AddComponent<YourScript>();
var script=obj.AddComponent();
然后可以修改脚本的变量等等。
您的预设将不会被触动。您在这里使用的
转换。父对象
指的是您的CreateFab.cs
脚本所附加的游戏对象的转换,而不是预设的子对象(较小的立方体)
正确的方法是:
// Instantiate the prefab
RigidBody rigidPrefab = Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation) as Rigidbody;
// Detach all children from the parent. Each child refers to the transform of a single cube.
foreach (Transform child in rigidPrefab.transform)
{
child.parent = null;
}
游戏运行时,预制件已经损坏。对一个衍生预制件的更改不会影响其他预制件。如果您的意思是在层次结构中对它们进行解析,请将它们的transform.parent设置为null。我在spawn脚本中添加了“transform.parent to null”,但它不允许我单击预置中的每个多维数据集。我将添加我尝试过的代码…因此脚本可以编译,但它仍然不会在单击时将附加的脚本运行到每个多维数据集。同一个脚本在预置外部的单个多维数据集上工作。假设脚本将图像上载到单个多维数据集上。顺便说一句,谢谢你的帮助。非常感谢。那么也许您也想在多维数据集上显示您的单个脚本?您在哪里调用
OpenExplorer()
?另外,EditorUtility.OpenFilePanel()
用于编辑器窗口。可能不适合游戏中的任何东西。想象一下,有10个多维数据集和10个打开文件提示开始打开,我在每个多维数据集上调用OpenExplorer()。我找到了另一个可能有用的脚本。”GetComponentChildren函数似乎是正确的选择。我会在找到答案后更新。谢谢回复。我更关心预制房里的孩子们。我相信“getComponentChildren”函数似乎是正确的选择。
// Instantiate the prefab
RigidBody rigidPrefab = Instantiate(Prefab, Spawnpoint.position, Spawnpoint.rotation) as Rigidbody;
// Detach all children from the parent. Each child refers to the transform of a single cube.
foreach (Transform child in rigidPrefab.transform)
{
child.parent = null;
}