简体   繁体   English

更改游戏对象 Unity 的不透明度并更改精灵的外观

[英]Change the opacity of gameobject Unity and Change the look of a sprite

I am creating a unity game, where I have different tiles, (2D) and I was wondering two things.我正在创建一个统一游戏,其中有不同的图块(2D),我想知道两件事。 One, how do you make a sprite more transparent.一,你如何让精灵更透明。 And two, how would I change the look of a gameObject.第二,我将如何改变游戏对象的外观。 so say we have a dirt block and I want to change it to a stone block.所以说我们有一个泥土块,我想把它换成石头块。 How would do that?怎么做到的?

Thanks!谢谢!

Edit: Using code编辑:使用代码

My errors \/我的错误\/ 我收到的错误

我的剧本 my script ^我的剧本^

检查员面板

错误

I'm assuming you want to do all of this in Code, because you can't change a sprite during runtime inside of the Unity Editor.我假设您想在代码中完成所有这些操作,因为您无法在运行时在 Unity 编辑器中更改精灵。

To make a Sprite more transparent you have to access the color value of your SpriteRenderer.要使 Sprite 更透明,您必须访问 SpriteRenderer 的颜色值。

SpriteRenderer.color = new Color(1f, 1f, 1f, 1f);

This is an example of how it could look.这是它的外观示例。 The first three numbers are responsible for the color of the object (R, G, B).前三个数字负责 object(R、G、B)的颜色。 The fourth color changes the opacity of your object. This is how it would look with only half the opacity.第四种颜色改变了 object 的不透明度。这就是不透明度只有一半时的样子。

SpriteRenderer.color = new Color(1f, 1f, 1f, 0.5f);

Now to your second question on how to change a texture at runtime.现在回答关于如何在运行时更改纹理的第二个问题。

There are different ways you can go about this.您可以通过多种方式 go 了解此事。

     gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("img1", typeof(Sprite)) as Sprite;

or或者

public Sprite stoneSprite;
SpriteRenderer.sprite = stoneSprite;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM