c# - 销毁时如何将浮动文本附加到游戏对象
问题描述
我已将以下脚本附加到名为“floatingText”的游戏对象
这可行,但我只想在对象被破坏时激活浮动文本。我有另一个 Destroy 脚本附加到一个球体/子弹,通过按下屏幕上的按钮来销毁 Cylinder 对象来触发该脚本。
我的问题是如何让浮动文本仅在气缸在撞击位置被子弹摧毁时才显示。
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);
}
}
}
解决方案
只需禁用对象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
脚本中调用它。我假设您想重用它,所以我会使用预制件:
- 使浮动对象成为预制件(将其拖到资产上)
- 将其从场景中移除
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);
推荐阅读
- python - 如何使用 PyOpenCL 将带有数组和变量的 C 结构传递给 OpenCL 内核
- php - 如何解决这个 view_users.php 警告?
- javascript - 使用带有 Pandas DF 的 Bootstrap-Table 的 Django 模板
- python - 如何在 Python/Django 中启动一个长时间运行的线程
- python - 如何使用 Selenium 和 xpath 或其他替代方法从 url 获取包含动态数据的表?
- f# - 处理记录
- php - htaccess重定向后文件未上传
- python - 更改 url 参数
- c++ - 使用“地址”运算符将二维数组作为内存块传递给 C++ 函数
- css - 避免兄弟元素的 box-shadow 相互重叠