【问题标题】:Blink GameObject闪烁游戏对象
【发布时间】:2017-01-19 14:51:10
【问题描述】:

我有点编程菜鸟,我正在尝试制作一个 GameObject ,在设定的几秒钟内停用和重新激活。例如,我希望我的星星在它消失之前慢慢闪烁,以创造一个很酷的外观影响。如果有更好的方法来使用此方法而不使用 SetActive(false) 和其他方法,请随时给我您的方法 - 这是我的代码,对不起,如果它很乱,我必须在这方面做得更好,但我会到期时间

谢谢大家

//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;

    }
}

}

【问题讨论】:

  • 尝试查看/谷歌搜索Coroutines,但如果您要制作游戏,编程本身必须非常强大。
  • 是的,最简单的方法是使用协程,但根据要实现的目标,您可能希望使用其他方法使对象不可见。
  • 好的,非常感谢,我会好好研究一下

标签: c# unity3d unity5


【解决方案1】:

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

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

如果这是一个 3D 游戏对象,请将 渲染模式 从不透明(默认)更改为 淡化透明

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

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&lt;MeshRenderer&gt;().material.color 代码替换为obj.GetComponent&lt;SpriteRenderer&gt;().color

【讨论】:

  • 感谢大家的帮助,它非常适合我想要做的事情!!! ^_^ 解释得很好,很清楚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多