【发布时间】:2017-04-23 21:32:47
【问题描述】:
我知道有很多关于统一分数等的问题,但是我发现很难为我的游戏实际构建代码,并查看了大量的 youtube 教程和博客文章。我想知道是否有人可以帮我添加一个分数,这样每次我在我的隐藏对象游戏中单击我的一个对象时,它都会提供一个分数。我真的很感激。到目前为止,我已经添加了一个名为 Score.cs 的单独脚本。代码如下。然后我将它添加到我的背景图像中,并将脚本包含在我的乐谱文本位于画布内的每个对象中,并且当前不计算任何被单击的对象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Text scoreText;
public int gameObject;
private int score;
// Use this for initialization
void Start () {
score = 0;
UpdateScore ();
}
void OnMouseDown () {
score += gameObject;
UpdateScore ();
}
// Update is called once per frame
void UpdateScore () {
scoreText.text = "Score:\n" + score;
}
}
!***** 编辑(Score.cs)*****!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Text scoreText;
public int gameObject;
public int score;
// Use this for initialization
void Start () {
score = 0;
UpdateScore ();
}
void OnMouseDown () {
score += gameObject;
UpdateScore ();
}
// Update is called once per frame
void UpdateScore () {
scoreText.text = "Score:\n" + score;
}
}
!****** Clicker.cs ******!
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
public class Clicker : MonoBehaviour, IPointerClickHandler
{
Score score;
void Start()
{
addPhysics2DRaycaster();
//Get Score Script Instance
string scoreObject = "gameObject";
score = GameObject.Find(scoreObject).GetComponent<Score>();
}
void addPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
public void OnPointerClick(PointerEventData eventData)
{
//Click detected. Increment score
score.score++;
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
}
【问题讨论】:
-
我在我的隐藏对象游戏中单击我的一个对象,它会提供分数。”“提供分数”是什么意思?
-
@programmer,对不起,我应该描述得更好一点,我的意思是当玩家点击每个对象时,每次得分都会逐渐上升