首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >眨眼GameObject

眨眼GameObject
EN

Stack Overflow用户
提问于 2017-01-19 14:51:10
回答 1查看 712关注 0票数 2

我是个编程新手,我正在尝试做一个GameObject,通过一定数量的seconds.For示例来禁用和重新激活,我希望我的明星在它消失之前慢慢眨眼,以创造一个很酷的外观效果。如果有更好的方法来使用这个方法而不用使用SetActive(假的)什么的,请给我你的方法--这是我的代码,对不起,如果麻烦了,我会在适当的时候做得更好。

谢谢各位

代码语言:javascript
复制
//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;

    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-19 16:48:14

如果有更好的方法来使用这个方法而不用使用SetActive(false)什么的

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

如果这是3D GameObject,请将呈现模式从不透明(默认)更改为淡出或透明。

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

代码语言:javascript
复制
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;
}

使用

代码语言:javascript
复制
void Start()
{
    blink(gameObject, 0.2f, 5f);
}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41744781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档