简体   繁体   English

C#Unity游戏测试

[英]C# Unity game test

I'm currently trying to get a game working in unity via C#. 我目前正试图通过C#让游戏在团结中运作。 I think I did a good job to this point. 我认为我在这一点上做得很好。

The game is a stacker Game: 游戏是堆叠器游戏:

A box will move from left to right and you press space to stack it If it's on the last box you placed its going further. 一个盒子将从左向右移动,你按空格来叠加它如果它在最后一个盒子上你放置它进一步。

But now my problem is: the box is an object made in Blender. 但现在我的问题是:盒子是Blender制造的物体。

But I want the boxes I'm creating underneath the current box the player is controlling to look like the box I made in Blender. 但我希望我正在当前盒子下方创建的盒子,玩家控制的盒子看起来像我在Blender中制作的盒子。

How can I achieve this? 我怎样才能做到这一点? To this point I just use: 到目前为止我只是使用:

    GameObject cb = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cb.transform.position = new Vector3(CurrentX, CurrentY, CurrentZ);

to create a normal cube. 创建一个普通的立方体。

I don't think that 2 lines of code can do everything you just mentioned. 我不认为2行代码可以完成你刚才提到的所有内容。 Hopefully, that's just a snippet from your code. 希望,这只是代码中的一小部分。

But now my problem is: the box is an object made in Blender. 但现在我的问题是:盒子是Blender制造的物体。

But I want the boxes I'm creating underneath the current box the player is controlling to look like the box I made in Blender. 但我希望我正在当前盒子下方创建的盒子,玩家控制的盒子看起来像我在Blender中制作的盒子。

You have to export the box from Blender to .fbx , .blend or any other 3D model format that is supported in Unity. 您必须将框从Blender导出到.fbx.blend或Unity支持的任何其他3D模型格式。 You can then import that file you exported into Unity. 然后,您可以导入导出到Unity的文件。 Here is a tutorial for that. 是一个教程。

Make that imported cube/GameObject into a prefab then use the Instantiate function to create new cube each time instead of GameObject.CreatePrimitive . 将导入的cube / GameObject放入预制件中,然后使用Instantiate函数每次创建新的多维数据集,而不是GameObject.CreatePrimitive

Change your current code to 将当前代码更改为

public GameObject boxPrfab;
GameObject cb = Instantiate(boxPrfab, Vector3.zero, Quaternion.identity) as GameObject;
cb.transform.position = new Vector3(CurrentX, CurrentY, CurrentZ);

Make sure to drag that prefab into the boxPrfab slot. 确保将该预制件拖入boxPrfab插槽。

I assume the box GameObject that you want to spawn already exists in the scene. 我假设box GameObject要产卵已经存在于现场。 Then create a Prefab in the Project tab by right-clicking into the tab and selecting Create -> Prefab . 然后在“ Project选项卡中创建一个Prefab ,方法是右键单击选项卡,然后选择“ Create - >“ Prefab Name the prefab something sensitive like eg BoxPrefab . 将预制件命名为敏感的东西,例如BoxPrefab Now drag'n'drop the box GameObject from the Hierarchy tab into the Project tab onto the newly created prefab entry. 现在拖放的box GameObjectHierarchy标签进入Project选项卡到新创建的预制条目。

Now you can add a public member variable to the script that is to instantiate the box spawns, eg like this: 现在,您可以将一个公共成员变量添加到要实例化框生成的脚本中,例如:

public GameObject PrefabBox = null;

Then drag'n'drop the prefab entry that you created before from the Project tab onto that new property PrefabBox in the Inspector tab. 然后将您之前创建的预制条目从“ Project选项卡PrefabBox到“ Inspector选项卡中的新属性PrefabBox上。 In that same script you can instantiate spawns of the prefab eg by the following code: 在同一个脚本中,您可以通过以下代码实例化预制件的生成:

GameObject boxSpawn = GameObject.Instantiate<GameObject>(
    PrefabBox,
    new Vector3(CurrentX, CurrentY, CurrentZ),
    PrefabBox.transform.rotation);

Find more info on instantiating GameObjects dynamically in the Unity3d docs. 在Unity3d文档中查找有关动态实例化GameObjects的更多信息。

Find the syntax of GameObject.Instantiate here. 在这里找到GameObject.Instantiate的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM