Unity:淡入游戏对象

时间:2018-01-15 09:55:55

标签: c# unity3d

我正在开发和安卓游戏,当我在产生时淡入敌人,问题是敌人的预制件有2个蒙皮网格渲染器材质(ZombearMaterial,EyeMaterial)。我使用向下给定的代码淡入敌人,但我面临的问题是脚本只能淡入层次结构中的第一个元素。那么如何在Gameobject中访问和更改所有蒙皮的网格渲染器材质。

淡入

public Material enemyAlpha;
public float fadetime;

// Use this for initialization
void Start () {

    enemyAlpha = GetComponent<SkinnedMeshRenderer> ().material;
}

// Update is called once per frame
void Update () {

    fadetime = (Time.time)/2;
    fadeIn (fadetime);
}

void fadeIn(float time)
{
    enemyAlpha.color = new Color (1, 1, 1, (time));
}

Gameobject检查员

enter image description here

1 个答案:

答案 0 :(得分:0)

只需创建一系列材料:

public class EnemySetAlpha : MonoBehaviour {

    public float fadeSpeed = 0.1f;
    private Material[] enemyMaterials;
    // Value used to know when the enemy has been spawned
    private float spawnTime ;

    // Use this for initialization
    void Start () {
        // Retrieve all the materials attached to the renderer
        enemyMaterials = GetComponent<SkinnedMeshRenderer> ().materials;
        spawnTime = Time.time ;
    }

    // Update is called once per frame
    void Update () {
            // Set the alpha according to the current time and the time the object has spawned
            SetAlpha( (Time.time - spawnTime) * fadeSpeed );
    }

    void SetAlpha(float alpha)
    {
        // Change the alpha value of each materials' color
        for( int i = 0 ; i < enemyMaterials.Length ; ++i )
        {
            Color color = enemyMaterials[i].color;
            color.a = Mathf.Clamp( alpha, 0, 1 );
            enemyMaterials[i].color = color;
        }
    }
}