ios 如何在 Unity 3d 中在纹理上设置图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13513302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:17:34  来源:igfitidea点击:

How to set image on Texture in Unity 3d

iphoneiosunity3dtextures

提问by josh

I am new to Unity 3D and I want to do just little task: to set image on a Textureinstance in Unity 3D. I don't know how to do this at runtime, and I also would like to know how I set its transparency low.

我是 Unity 3D 的新手,我只想做一些小任务:Texture在 Unity 3D 中的实例上设置图像。我不知道如何在运行时执行此操作,我也想知道如何将其透明度设置得较低。

I don't need Texture2D - I just need Texture. My image is in .pngformat. I also want set an image from my documents directory on to this texture.

我不需要 Texture2D - 我只需要 Texture。我的图像是.png格式。我还想将我的文档目录中的图像设置到此纹理上。

回答by amasiye

  • First import your image into your project by simply dropping it in your project window.

  • Select the image once it's in the project window and make sure that it is set to a Texture Type of Texture in your inspector.

  • Next, create a new material by right clicking in your project window.

  • Next you want to assign your immage to this material and you can go about doing this by dragging and dropping your image (which is in the project window) on to your newly created material.

  • Then click on the new material and in your inspector window it should show you that your image is the active texture and the shader should be set to diffuse by default.

  • To activate transparency you'll want to change the shader type by clicking on the shader drop down menu in the inspector window and selecting Transparent/Diffuse (or any of the transparency options depending on what look you're going for).

  • After this to change it's transparency, simply click on the main color swatch and there should be a new window that opens giving you all kinds of modifiers (with 4 horizontal sliders to adjust Red, Green, Blue & Alpha).

  • Adjust the Alpha slider to affect the transparency of your material.

  • 首先将您的图像导入到您的项目中,只需将其拖放到您的项目窗口中即可。

  • 选择项目窗口中的图像,并确保在检查器中将其设置为纹理类型。

  • 接下来,通过右键单击您的项目窗口来创建一个新材料。

  • 接下来,您想将图像分配给该材质,您可以通过将图像(位于项目窗口中)拖放到新创建的材质上来执行此操作。

  • 然后单击新材质,在您的检查器窗口中,它应该会显示您的图像是活动纹理,并且默认情况下着色器应设置为漫反射。

  • 要激活透明度,您需要通过单击检查器窗口中的着色器下拉菜单并选择透明/漫反射(或任何透明度选项,取决于您想要的外观)来更改着色器类型。

  • 在此更改其透明度之后,只需单击主色样,应该会打开一个新窗口,该窗口将为您提供各种修改器(带有 4 个水平滑块来调整红色、绿色、蓝色和 Alpha)。

  • 调整 Alpha 滑块以影响材质的透明度。

Now, whenever you need to make a call to your material at runtime (e.g if you wanted to change the texture applied to a gameobject), simply do so by using:

现在,每当您需要在运行时调用材质时(例如,如果您想更改应用于游戏对象的纹理),只需使用:

renderer.material

This will affect the material off the gameObject that the script is attached to. So for example, if you wanted to change the textureat runtime from script you could say:

这将影响脚本附加到的游戏对象的材质。例如,如果您想在运行时从脚本更改纹理,您可以说:

// Assign the texture exposed in the inspector the renderer's material

var texture : Texture;
renderer.material.mainTexture = texture;

And if you wanted to change the alpha channel:

如果您想更改 Alpha 通道:

renderer.material.color.a = 0 // For example

Hope this helps. Let me know if anything needs clarifying.

希望这可以帮助。如果有什么需要澄清的,请告诉我。