我知道關於統一得分等方面有很多問題,但是我發現很難真正爲我的遊戲構建代碼,並且已經查看了大量的YouTube教程和博客帖子。我想知道是否有人可以幫助我添加分數,以便每次在隱藏對象遊戲中單擊我的一個對象時,它都會提供分數。我真的很感激。到目前爲止,我已經添加了一個名爲Score.cs的單獨腳本。代碼如下。然後我將它添加到我的背景圖像中,並將腳本包含在每個對象中,我的評分文本位於畫布內,並且當前不計算任何被點擊的對象。Unity遊戲得分
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
@programmer,對不起,我應該描述它好一點,我的意思是,當玩家點擊每個對象時,得分會逐漸上升 – Rochelle