Blink GameObject

时间:2017-01-19 14:51:11

标签: c# unity3d unity5

我是一个编程的菜鸟,我正在尝试制作一个游戏对象,停用并重新激活一定的秒数。例如,我希望我的星星在它消失之前慢慢闪烁,以创造一个很酷的外观影响。如果有一个更好的方法来使用这个方法完成而不使用SetActive(假)什么不是,请随时给我你的方法 - 这是我的代码,对不起,如果它的凌乱,我必须在这方面变得更好,但我会在到期时间

谢谢你们

//Timers
public float ScoreTimer;
public float[] TimeMarkStamp;

//Scoring
public int totalCollStars;
[Space]
public int maxStars = 5;

public GameObject[] StarImages;


// Use this for initialization
void Start()
{


}

// Update is called once per frame
void Update()
{
    ScoreTimer += Time.deltaTime;
    if (ScoreTimer <= TimeMarkStamp[0])
    {
        Debug.Log("It works");
        StarImages[0].SetActive(false);
    }
    else if (ScoreTimer <= TimeMarkStamp[1])
    {
        Debug.Log("It workds" + TimeMarkStamp[1]);
        StarImages[1].SetActive(false);
    }
    else if (ScoreTimer <= TimeMarkStamp[2])
    {
        Debug.Log("It works" + TimeMarkStamp[2]);
        StarImages[2].SetActive(false);
    }
    else if (ScoreTimer <= TimeMarkStamp[3])
    {
        //This is not working 
        InvokeRepeating("flickerEffect", 3f, 1f);
    }
}

void flickerEffect()
{
    bool flickCheck = false;


    if (flickCheck == false)
    {
        StarImages[3].SetActive(true);
        flickCheck = true;
    }
    else if (flickCheck == true)
    {
        StarImages[3].SetActive(false);
        flickCheck = false;

    }
}

}

1 个答案:

答案 0 :(得分:1)

  

如果有更好的方法使用此方法完成使用   SetActive(false)和什么不是

是的,除了使用SetActive功能外,还有更好的方法。您应该将GameObject的alpha color来回0更改为1。之后,您可以使用SetActive禁用GameObject。这样可以节省重复调用SetActive函数时生成的垃圾量。

如果这是3D GameObject,请将渲染模式从不透明(默认)更改为淡化透明

一个可以做到这一点的简单函数:

void blink(GameObject obj, float blinkSpeed, float duration)
{
    StartCoroutine(_blinkCOR(obj, blinkSpeed, duration));
}

IEnumerator _blinkCOR(GameObject obj, float blinkSpeed, float duration)
{
    obj.SetActive(true);
    Color defualtColor = obj.GetComponent<MeshRenderer>().material.color;

    float counter = 0;
    float innerCounter = 0;

    bool visible = false;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        innerCounter += Time.deltaTime;

        //Toggle and reset if innerCounter > blinkSpeed
        if (innerCounter > blinkSpeed)
        {
            visible = !visible;
            innerCounter = 0f;
        }

        if (visible)
        {
            //Show
            show(obj);
        }
        else
        {
            //Hide
            hide(obj);
        }

        //Wait for a frame
        yield return null;
    }

    //Done Blinking, Restore default color then Disable the GameObject
    obj.GetComponent<MeshRenderer>().material.color = defualtColor;
    obj.SetActive(false);
}

void show(GameObject obj)
{
    Color currentColor = obj.GetComponent<MeshRenderer>().material.color;
    currentColor.a = 1;
    obj.GetComponent<MeshRenderer>().material.color = currentColor;
}

void hide(GameObject obj)
{
    Color currentColor = obj.GetComponent<MeshRenderer>().material.color;
    currentColor.a = 0;
    obj.GetComponent<MeshRenderer>().material.color = currentColor;
}

<强>用法

void Start()
{
    blink(gameObject, 0.2f, 5f);
}

如果这是SpriteRender,则必须将所有obj.GetComponent<MeshRenderer>().material.color代码替换为obj.GetComponent<SpriteRenderer>().color