销毁后如何将浮动文本附加到游戏对象

时间:2019-01-14 17:21:57

标签: c# unity3d

我已将以下脚本附加到名为“ floatingText”的游戏对象上

这有效,但是我只想在对象被破坏时才激活浮动文本。 我在球体/项目符号上附加了另一个销毁脚本,该脚本是通过按屏幕上的按钮以销毁圆柱体对象而触发的。

我的问题是如何使浮动文本仅在撞击位置的子弹破坏了圆柱体时显示。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Float : MonoBehaviour 
{
    Text text;
    public float fadeDuration = 2.0f;
    public float speed = 2.0f;

    // Use this for initialization
    void Start ()
    {
        text = this.GetComponent<Text>();
        StartCoroutine(Fade());
    }

    public IEnumerator Fade ()
    {
        float fadespeed = (float)1.0 / fadeDuration;
        Color c = text.color;

        for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadespeed)
        {
            c.a = Mathf.Lerp(1, 0, t);
            text.color = c;
            yield return true;
        }

        Destroy(this.gameObject);
    }

    // Update is called once per frame
    void Update ()
    {
        this.transform.Translate(Vector3.up * Time.deltaTime * speed);  
    }
}

控制销毁对象的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Destroy : MonoBehaviour 
{
    public ParticleSystem effect;
    public AudioSource explosion;
    public Text scoreText;
    public GameObject floatingText; 

    void OnCollisionExit(Collision col)
    {          
        if (col.gameObject.tag == "Cylinder")
        { 
            effect.Play();
            explosion.Play();
            Destroy(col.gameObject);  
        }
    }
}

2 个答案:

答案 0 :(得分:2)

只需禁用Start中的对象,以后再启用它:

public class Float : MonoBehaviour 
{
    Text text;
    public float fadeDuration = 2.0f;
    public float speed = 2.0f;

    // Use this for initialization
    void Start ()
    {
        text = this.GetComponent<Text>();
        // Hide object
        gameObject.SetActive(false);
    }

    public ShowTextAndFade(Vector3 initialPosition)
    {
        StartCoroutine (Fade(initialPosition));
    }

    private IEnumerator Fade (Vector3 initialPosition)
    {
        // Show object
        gameObject.SetActive(true);

        // Set position
        transform.position = initialPosition;

        float fadespeed = (float)1.0 / fadeDuration;
        Color c = text.color;

        for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadespeed)
        {
            c.a = Mathf.Lerp(1, 0, t);
            text.color = c;
            yield return true;
        }

        Destroy(this.gameObject);
    }

    // Update is called once per frame
    void Update ()
    {
        this.transform.Translate(Vector3.up * Time.deltaTime * speed);  
    }
}

,然后在Destroy脚本中调用它。我假设您想重复使用它,所以我将使用预制件:

  • 使float对象成为预制对象(将其拖动到资产上)
  • 将其从场景中删除
  • 并引用floatPrefab组件的Destroy中创建的预制件

Destroy中:

// Reference the prefab in the inspector
public Float floatPrefab;

//...

effect.Play();
explosion.Play();
var position = col.transform.position;
Destroy(col.gameObject);

// Spawn the float text
var float = Instantiate(floatPrefab); 
// And start fading
float.ShowTextAndFade(position);

您也可以跳过预制件

  • 从场景中引用实际对象
  • 删除Instantiate,而是使用

    public Float float;
    
    //...
    float.ShowTextAndFade(position); 
    

答案 1 :(得分:1)

应将浮动文本制作为预制文本,以便在使用OnDestroy事件破坏圆柱体时可以将其实例化。实例化后,您可以通过floatingText GameObject将信息从圆柱体传递到实例化的浮动文本。

气缸

...

public string textToShow = "Floating text you want to show";
public Vector3 pointOfCollision = gameObject.transform.position; // set on impact

void OnDestroy()
{
    GameObject floatingText = Instantiate(floatingTextPrefab, pointOfCollision, floatingTextPrefab.transform.localRotation);
    floatingText.Text.text = textToShow;

    StartCoroutine(floatingText.GetComponent<Float>().Fade());
}