使用 c# 脚本更改 Unity 中游戏对象的纹理 [英] Changing texture on gameobject in Unity using c# script

查看:37
本文介绍了使用 c# 脚本更改 Unity 中游戏对象的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 c# 脚本在 Unity 中创建了一个换装游戏,当我使用以下代码单击更改按钮时,该脚本会更改身体上的纹理:

I have created a dressup game in Unity using c# script which changes the texture on the body when I click the change button using the following code:

gotexture = GameObject.Find ("Body");
Texture2D mytexture=(Texture2D) Resources.LoadAssetAtPath("Assets/characters/Female/textures/texture2.tga",typeof(Texture2D));
gotexture.renderer.material.mainTexture = mytexture;

当我统一运行项目时,此代码会更改主体上的纹理,但是当我为 Windows Phone 构建项目并运行它时,单击按钮时纹理不会更改.在 Windows Phone 上使用时,是否有任何要更改的纹理设置?纹理是 tga 文件.

This code changes the texture on the body when I run the project in unity but when I build the project for Windows Phone and run it, the texture doesn't change when button is clicked. are there any settings of the textures to be changed when using it on Windows Phone? The textures are tga files.

推荐答案

当您构建项目时,您的资源与它们在项目中的文件路径不同.不使用 Resources.LoadAssetAtPath,只需使用 Resources.Load.

When you build the project, your resources are not living in the same file paths as they are in the project. Instead of using Resources.LoadAssetAtPath, just use Resources.Load.

不过,为了使用它,您需要将可能动态加载的任何内容放在名为 Resources 的文件夹下.例如,

In order to use this though, you need to put anything that you might dynamically load like that under a folder called Resources. For example,

Resources
|___
    -Textures
    -Sound Effects
    -Prefabs
    -etc

现在您可以像这样加载这些资源:

Now you can load those assets like so:

Resources.Load("Textures/myTexture");

这对于加载预制件也非常方便!无论您在哪里运行它,它都应该有效.

This is really handy for loading prefabs too! And it should work no matter where you run it.

注意:我相信您在像这样使用时省略了文件扩展名.我不太记得了,我不在可以测试的地方.如果它不起作用,只需修改文件名直到它起作用.

Note: I believe you omit the file extension when using it like this. I don't quite remember and I'm not in a place where I can test it. If it doesn't work, just play around with the filename until it works.

这篇关于使用 c# 脚本更改 Unity 中游戏对象的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆