2014-08-14 19:47:13|?次阅读|上传:huigezrx【已有?条评论】发表评论
关键词:Unity3D, 游戏, 虚拟现实|来源:唯设编程网
这是一个工作流模式,在Unity里可反复使用。在一开始,你可能惊讶,为什么会是这样好,因为该脚本创建立方体的代码只有2行长。
But because you are using a Prefab now, you can adjust the Prefab in seconds. Want to change the mass of all those instances? Adjust the Rigidbody in the Prefab only once. Want to use a different Material for all the instances? Drag the Material onto the Prefab only once. Want to change friction? Use a different Physic Material in the Prefab's collider. Want to add a Particle System to all those boxes? Add a child to the Prefab only once.
但是,因为现在你使用的是预制品,你可以在几秒钟内调整预制品。想要改变的那些例子吗?在预制品里调整刚体仅一次。想要使用一种不同的材料对所有的例子吗?拖拽物质到预制品上仅一次。想要改变摩擦吗?使用不同的物理资料在预制品的碰撞器里。要添加一个粒子系统给所有那些方框吗?添加一个子预制品仅一次。
Instantiating rockets & explosions火箭和爆炸的初始化
Here's how Prefabs fit into this scenario: 现在是预制品如何融入到这个场景里:
一个火箭发射实例化一个火箭预制品当用户按发射键时。预制品包含一个网格、刚体、碰撞器以及一个含有痕迹粒子系统的子GameObject。
火箭影响和一个爆炸预制品的实例。爆炸预制包含一个粒子系统、随着时间的推移淡出光以及脚本应用损害周围的GameObjects。
While it would be possible to build a rocket GameObject completely from code, adding Components manually and setting properties, it is far easier to instantiate a Prefab. You can instantiate the rocket in just one line of code, no matter how complex the rocket's Prefab is. After instantiating the Prefab you can also modify any properties of the instantiated object (e.g. you can set the velocity of the rocket's Rigidbody).
虽然有可能从代码完成构建一个火箭的GameObject,完全手动添加组件并设置属性,容易得多去实例化一个预制品。你可以在仅有一行代码里实例化火箭,无论多么复杂,火箭是预制的。实例化预制品后,你还可以修改实例化对象的任何属性(例如,可以设置的火箭刚体速度)。
Aside from being easier to use, you can update the prefab later on. So if you are building a rocket, you don't immediately have to add a Particle trail to it. You can do that later. As soon as you add the trail as a child GameObject to the Prefab, all your instantiated rockets will have particle trails. And lastly, you can quickly tweak the properties of the rocket Prefab in the Inspector, making it far easier to fine-tune your game.
除了更容易的使用,稍后你可以更新预制。因此,如果你要构建一个火箭,你不立即需要添加一个粒子痕迹给它。以后你能做到这一点。只要你添加一个子GameObject到预制品,所有实例化的火箭将有粒子痕迹。最后,你可以快速调整火箭的性能预制在检视器里,使微调你的游戏容易得多。
This script shows how to launch a rocket using the Instantiate() function.
此脚本显示了如何使用发射火箭的Instantiate()函数。
// Require the rocket to be a rigidbody. // This way we the user can not assign a prefab without rigidbody var rocket : Rigidbody; var speed = 10.0; function FireRocket () { var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation); rocketClone.velocity = transform.forward * speed; // You can also acccess other components / scripts of the clone rocketClone.GetComponent(MyRocketScript).DoSomething(); } // Calls the fire method when holding down ctrl or mouse function Update () { if (Input.GetButtonDown("Fire1")) { FireRocket(); } }
Replacing a character with a ragdoll or wreck 取代一个拥有破碎的或损坏的飞船
Let's say you have a fully rigged enemy character and he dies. You could simply play a death animation on the character and disable all scripts that usually handle the enemy logic. You probably have to take care of removing several scripts, adding some custom logic to make sure that no one will continue attacking the dead enemy anymore, and other cleanup tasks.
假设你有一个完全操纵敌人的角色和它死亡。你可以简单地处理一个死亡的动画和禁用所有通常敌人的逻辑处理脚本。你可能要消除几个脚本,加入一些自定义的逻辑,以确保没有人会继续攻击死亡的敌人,以及其它清理工作。
A far better approach is to immediately delete the entire character and replace it with an instantiated wrecked prefab. This gives you a lot of flexibility. You could use a different material for the dead character, attach completely different scripts, spawn a Prefab containing the object broken into many pieces to simulate a shattered enemy, or simply instantiate a Prefab containing a version of the character.