Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 如何动态更新Unity预置?_Unity3d - Fatal编程技术网

Unity3d 如何动态更新Unity预置?

Unity3d 如何动态更新Unity预置?,unity3d,Unity3d,1) 我怎样才能找到预制件的高度?在我下面的代码中,我想使232值成为动态的,这样我可以在将来安全地更新我的预置 2) 如何在渲染预置中获取对文本对象的引用 Player[] players = JsonHelper.getJsonArray<Player>(www.downloadHandler.text); float nextY = 0; for (int i = 0; i < players.Length; i++) { Player player = pl

1) 我怎样才能找到预制件的高度?在我下面的代码中,我想使232值成为动态的,这样我可以在将来安全地更新我的预置

2) 如何在渲染预置中获取对文本对象的引用

Player[] players = JsonHelper.getJsonArray<Player>(www.downloadHandler.text);

float nextY = 0;

for (int i = 0; i < players.Length; i++)
{
    Player player = players[i];

    Vector3 pos = new Vector3(0, -nextY, spawnPoint.position.z);

    GameObject spawnedItem = Instantiate(listViewItem, pos, spawnPoint.rotation);

    nextY += 232;

    // spawnedItem.?  <- need to put a Player name into a Text in here

    spawnedItem.transform.SetParent(spawnPoint, false);
}
Player[]players=JsonHelper.getJsonArray(www.downloadHandler.text);
float-nextY=0;
for(int i=0;i
public class MyCustomPrefab : MonoBehaviour
{

    [SerializedField] private float height;
    [SerializedField] private TextMeshProUGUI text;

    // other variable, methods, anything you want

}
然后使用您的类型,而不是将其实例化为游戏对象:

MyCustomPrefab prefab = Instantiate(.....);

// Then you can access/use any variable/method that you want
nextY += prefab.height;
prefab.text = .....;


你的引用来自何处?它也需要是MyCustomPrefable类型(或者如果你真的不能,就使用GetComponent)@gdonald
Instantiate
返回传递的预置的类型。正如Jichael所说,将你的预置字段设置为
MyCustomPrefable
类型,或者使用
MyCustomPrefact=Instantiate(…).GetComponent()
我真的建议坚持使用后面的方法,因为这也为检查器本身增加了额外的安全性:您只能引用实际连接了该组件的东西!@Jichael我不会调用实例变量
prefact
,这有点让人困惑。最好将其称为
实例
或其他类似的东西imilar@gdonald对不起,我的意思当然不是坚持后一个,而是坚持第一个^^