通常,想要对任何类型的游戏资源进行修改时,您会希望在运行时进行并希望是临时操作。例如,如果角色选择了无敌能量块,您可能想要更改玩家角色的__材质____着色器__以便直观展示无敌状态。此操作涉及修改正在使用的材质。此修改不是永久性的,因为当我们退出__播放模式__时,我们不希望材质具有不同的着色器。
但是,在 Unity 中可以编写永久修改源资源的脚本。让我们使用上面的材质示例作为起点。
要临时更改材质的着色器,我们可更改__材质__组件的__着色器__属性。
private var invincibleShader = Shader.Find ("Specular");
function StartInvincibility {
renderer.material.shader = invincibleShader;
}
使用此脚本并退出播放模式时,材质的状态将重置为最初进入播放模式之前的状态。发生这种情况是因为无论何时访问 renderer.material,都会自动实例化材质并返回实例。此实例同时并自动应用于渲染器。因此,您可以进行真正想要的任何更改而不必担心永久性。
下面介绍的方法将修改 Unity 中使用的实际源资源文件。这些修改不可撤销。请谨慎使用。
现在,假如我们在退出播放模式时不希望材质重置。为此,可使用 renderer.sharedMaterial。sharedMaterial 属性将返回此渲染器(以及其他渲染器)使用的实际资源。
下面的代码将永久更改材质来使用 Specular 着色器,不会将材质重置为进入播放模式之前的状态。
private var invincibleShader = Shader.Find ("Specular");
function StartInvincibility {
renderer.sharedMaterial.shader = invincibleShader;
}
如您所见,对 sharedMaterial 进行任何更改都可能在有用的同时又有风险。对 sharedMaterial 所做的任何更改都将是永久性的,而不是可撤销的。
上述相同的方法不仅可应用于材质。遵循该惯例的完整资源清单如下:
如果声明任何上述类(材质、网格或物理材质)的公共变量,并使用该变量而不是使用相关的类成员对资源进行修改,则不会获得在应用修改之前自动实例化的优点。
有两种不同的资源在修改后绝不会自动实例化。
通过脚本对这些资源进行的任何修改都是永久性的,且永远不可撤销。因此,如果通过脚本更改地形的高度贴图,您需要考虑自己实例化和分配值。纹理也是如此。如果更改纹理文件的像素,则更改将是永久性的。
在 iOS 和 Android 项目中修改 Texture2D 资源后绝不会自动实例化这些资源。通过脚本对这些资源进行的任何修改都是永久性的,且永远不可撤销。因此,如果更改纹理文件的像素,则更改将是永久性的。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.