[Unity2D]精灵颜色材质

    材质(Materials)用来把网格(Mesh)或粒子渲染器(Particle Renderers)贴到游戏对象上。他们在定义对象怎么被显示发挥重要组成部分。材质包括用于呈现网状或颗粒着色器的参考,所以这些组件不能在没有材质的情况下显示。

任何材质的属性取决于选定的着色器(Shader)发生变化。这是最常用的属性:

Shader 着色器:将被材质(Material)使用的着色器。

Main Color 主颜色:任何一种色调都可使用。不填色就使用白色。

Base 基本:将被显示的纹理

    下面看下怎么添加一种材质:

    在Project面板文件里面,右键“Create”->"Material"添加:

添加之后的文件如下所示:

设置材质的Shader 着色器为Sprite default:

把材质对象拖到精灵的材质属性上:

    除此之外我们还可以在脚本里面添加和修改材质:

    private SpriteRenderer healthBar;            // Reference to the sprite renderer of the health bar.

    void Awake ()
    {

        healthBar = GameObject.Find("HealthBar").GetComponent<SpriteRenderer>();

        // Getting the intial scale of the healthbar (whilst the player has full health).
        healthScale = healthBar.transform.localScale;
    }

    public void UpdateHealthBar ()
    {
        // Set the health bar's colour to proportion of the way between green and red based on the player's health.
        healthBar.material.color = Color.Lerp(Color.green, Color.red, 1 - health * 0.01f);

        // Set the scale of the health bar to be proportional to the player's health.
        healthBar.transform.localScale = new Vector3(healthScale.x * health * 0.01f, 1, 1);
    }

 

posted on 2014-09-26 01:23  linzheng  阅读(2835)  评论(0)    收藏  举报

导航