如何检测新对象何时被生成或从统一中删除以获得评分/分数?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何检测新对象何时被生成或从统一中删除以获得评分/分数?相关的知识,希望对你有一定的参考价值。
我一直试图为我的游戏制作一个得分/积分系统,每当一个物体被摧毁时,得分增加,我使用一组名为GDG_Assets的资产,它允许你创建可破坏的环境对象。当一个对象被击中时,主对象变得不可见,并且产生了被破坏的对象的模型。我想知道如何检测这些新模型何时被生成以及如何将它们实现到我的代码中? “Amount”整数是存储被破坏/产生的对象数量以得分的位置。我可以检测到与我的射弹的碰撞,但问题是游戏的设计方式是爆炸导致连锁反应导致其他物体被摧毁。任何想法或代码改进将不胜感激。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class scoreCount : MonoBehaviour {
public Text countText;
public int eggCount;
public int barrelCount;
public int cubeCount;
//game objects
public GameObject Egg_000;
public GameObject Barrel_000;
public GameObject Cube01_000;
//amount of objects destroyed
public int barrelAmount;
public int eggAmount;
public int cubeAmount;
private int count;
private int egg;
private int barrel;
private int cube;
// Initializing points of each object
void Start () {
count = 0;
egg = 100;
barrel = 75;
cube = 200;
countText.text = "Score: " + count.ToString ();
}
// Update is called once per frame
void Update () {
//multiplies amount of objects destroyed by points
eggCount = egg * eggAmount;
barrelCount = barrel * barrelAmount;
cubeCount = cube * cubeAmount;
//determines final score
count = eggCount + barrelCount + cubeCount;
countText.text = "Score: " + count.ToString();
}
}
答案
你应该把更多的事件驱动,而不是在Update
中
因此我会实施一个enum
。
public enum DestructableType
{
Egg,
Barrel,
Cube,
}
比我更改你的班级
public class scoreCount : MonoBehaviour
{
[Header("Components")]
public Text countText;
[Header("Prefabs")]
public DestructableObject Egg_000;
public DestructableObject Barrel_000;
public DestructableObject Cube01_000;
// I didn't see what those are used for
/*
public int eggCount;
public int barrelCount;
public int cubeCount;
*/
[Header("amount of objects destroyed")]
public int barrelAmount;
public int eggAmount;
public int cubeAmount;
// Simply adjust those in the inspector as well and give them default values
[Header("points values")]
[SerializeField] private int egg = 100;
[SerializeField] private int barrel = 75;
[SerializeField] private int cube = 200;
// Will be 0 by default anyway
private int count;
// Initializing points of each object
private void Start ()
{
// Only thing needed to be set in Start
// though even this you could simply do in that Text component already
countText.text = "Score: " + count.ToString ();
}
// Called by the DestructableObjects when destroyed
public void ObjectsDestroyed(DestructableType type)
{
// I didn't know which values you really need in the end
// actually the lines with count += xyz; would be enough I guess
switch(type)
{
case Egg:
eggCount += egg;
eggAmount++;
count += egg;
break;
case Barrel:
barrelCount += barrel;
barrelAmount++;
count += barrel;
break;
case Cube:
cubeCount += cube;
cubeAmount++;
count += cube;
break;
}
countText.text = "Score: " + count.ToString();
}
}
在Prefabs上添加一个额外的组件
public class DestructableObject : MonoBehaviour
{
// Adjust this through the inspector
public DestructableType Type;
private void OnDestroy ()
{
FindObjectOfType<scoreCount>().ObjectsDestroyed(Type);
}
}
以上是关于如何检测新对象何时被生成或从统一中删除以获得评分/分数?的主要内容,如果未能解决你的问题,请参考以下文章