Unity3D高级手册03:通过脚本修改源资产

2014-08-16 12:24:56|?次阅读|上传:huigezrx【已有?条评论】发表评论

关键词:Unity3D, 游戏, 虚拟现实|来源:唯设编程网

Unity Manual > Advanced > Modifying Source Assets Through Scripting

Unity手册->高级->通过脚本修改源资产

Modifying Source Assets Through Scripting通过脚本修改源资产

Automatic Instantiation自动实例

Usually when you want to make a modification to any sort of game asset, you want it to happen at runtime and you want it to be temporary. For example, if your character picks up an invincibility power-up, you might want to change the shader of the material for the player character to visually demonstrate the invincible state. This action involves modifying the material that's being used. This modification is not permanent because we don't want the material to have a different shader when we exit Play Mode.

通常当你想做一个任何游戏资产排序的修改,你希望它在运行时发生并希望它是临时的。例如,如果你的角色拿起一不可战胜的权力,你可能想改变玩家角色的物质着色直观显示战无不胜的状态。这一行动涉及到修改,目前已被使用的材料。这种修改是不是永久性的,因为我们不想材料有不同的着色,当我们离开播放模式时。

However, it is possible in Unity to write scripts that will permanently modify a source asset. Let's use the above material example as a starting point.

然而,它有可能在Unity编写脚本去永久地修改源代码的资产。让我们以上述材料的例子为起点。

To temporarily change the material's shader, we change the shader property of the material component.

要暂时改变材料的着色,我们改变材料的组件部分阴影的属性。

        private var invincibleShader = Shader.Find ("Specular");

        function StartInvincibility {

               renderer.material.shader = invincibleShader;

        }

When using this script and exiting Play Mode, the state of the material will be reset to whatever it was before entering Play Mode initially. This happens because whenever renderer.material is accessed, the material is automatically instantiated and the instance is returned. This instance is simultaneously and automatically applied to the renderer. So you can make any changes that your heart desires without fear of permanence.

当使用脚本并退出播放模式,材料的状态将返回到它最初播放之前的状态。这个发生因为无论何时渲染器、材质被访问,材质自动实例并且实例将返回。这个实例是同时的和自动的应用于渲染器。因此你可做一些你心里期望的没有永久害怕的更改。

Direct Modification  直接修改

IMPORTANT NOTE 重要注释

The method presented below will modify actual source asset files used within Unity. These modifications are not undoable. Use them with caution.

下面将修改文件的实际来源的资产使用的在Unity范围内提出的方法。这样的修改是不可撤消的。请小心使用他们。

Now let's say that we don't want the material to reset when we exit play mode. For this, you can use renderer.sharedMaterial. The sharedMaterial property will return the actual asset used by this renderer (and maybe others).

现在让我们说,我们不希望这些材料在我们退出播放模式后重置。为此,你可以使用renderer.sharedMaterial。该sharedMaterial属性将返回实际的资产这个渲染器(或者其他人)使用。

The code below will permanently change the material to use the Specular shader. It will not reset the material to the state it was in before Play Mode.

下面的代码将永久改变材料使用的反射阴影。它不会重置材料中的状态是在播放模式之前。

        private var invincibleShader = Shader.Find ("Specular");

        function StartInvincibility {

               renderer.sharedMaterial.shader = invincibleShader;

        }

As you can see, making any changes to a sharedMaterial can be both useful and risky. Any change made to a sharedMaterial will be permanent, and not undoable.

正如你所看到的,做任何更改sharedMaterial可以是有益的和危险的。任何改动的sharedMaterial将是永久的和不可撤消的。

Applicable Class Members 可用的类成员

The same formula described above can be applied to more than just materials. The full list of assets that follow this convention is as follows:

在上述相同的准则可应用于不仅仅是材料。资产的完整列表,按照约定如下:

  • Materials: renderer.material and renderer.sharedMaterial
  • 材料renderer.material 和 renderer.sharedMaterial
  • Meshes: meshFilter.mesh and meshFilter.sharedMesh
  • 网格:meshFilter.mesh 和 meshFilter.sharedMesh
  • Physic Materials: collider.material and collider.sharedMaterial
  • 物理材料:collider.material 和 collider.sharedMaterial

Direct Assignment 直接赋值

If you declare a public variable of any above class: Material, Mesh, or Physic Material, and make modifications to the asset using that variable instead of using the relevant class member, you will not receive the benefits of automatic instantiation before the modifications are applied.

如果你声明任何上述类的公共变量:材料,网格,或物理材料,并作出资产使用该变量而不是使用有关的类成员的修改,你将不会收到自动实例化的好处在修改被应用之前。

Assets that are not automatically instantiated 资产不自动化实例

There are two different assets that are never automatically instantiated when modifying them.

有两个不同的资产永远不会自动实例当修改它们时。

Any modifications made to these assets through scripting are always permanent, and never undoable. So if you're changing your terrain's heightmap through scripting, you'll need to account for instantiating and assigning values on your own. Same goes for Textures. If you change the pixels of a texture file, the change is permanent.

通过脚本对这些资产的任何修改总是永久的,永不撤消的。因此,如果你通过脚本更改你的地形的高度图,你需要考虑实例和分配你自己的值。纹理也是一样的。如果你更改了文件的纹理的像素,变化是永久性的。

发表评论0条 】
网友评论(共?条评论)..
Unity3D高级手册03:通过脚本修改源资产