为什么当我的鼠标悬停在一个统一的对象上时,它称它为错误的对象?
Why when hovering my mouse over an object in unity is it calling it the wrong object?
我决定尝试创建一个简单的 Cookie Clicker 游戏作为我的第一个个人项目,没有 tuts 或课程,我设法达到了可以点击我的“Cookie”的程度,但是它向 cookieCounter 添加了 1 unity好像觉得一切都是我的cookie...
不确定是我的代码还是 unity 中的某些设置错误,但我相信这里有人可以为我指明正确的方向。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class cookieCounter : MonoBehaviour
{
public TextMeshProUGUI countText;
public GameObject Cookie;
private int count = 0;
private Rigidbody rb;
private string Object;
// Start is called before the first frame update
void Start()
{
count = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) // On left click
{
Debug.Log(gameObject.name); //Returns the Object of which was clicked on
OnMouseOver();
countText.text = count.ToString("Cookies: 0"); // Updates the cookie counter on screen
Debug.Log(count); // Returns Final Count
}
}
private void OnMouseOver() // Looks at mouse and what it is hovering
{
Object = (gameObject.name); // Detects what the mouse is hovering over
if (Object == "Cookie") // Makes sure the object mouse is hovering is the "Cookie"
{
count++; // Adds 1 to the cookie counter.
}
}
}
请忽略我以前没有做过的低效代码。
你检查鼠标是否被点击,当前对象是"Cookie"
,但你不检查点击发生的地方。如果 Cookie
对象与问题代码共享相同的 Update
/OnMouseOver
方法,则无论点击的位置如何,它都会增加 count
。
您可以通过考虑鼠标位置来修复它。如果 Cookie 是 UI,您可以让 Button
class 处理与检测鼠标点击相关的所有事情(最简单的方法)。
如果 Cookie 是 2D SpriteRenderer
对象(或 3D),那么您可以使用 EventSystem
(可能已经包含在您的项目中)并在 Cookie class 中实现 IPointerClickHandler
.您可以在互联网上找到详细的解决方案,例如 https://forum.unity.com/threads/solved-detecting-mouse-click-on-an-object-in-2d-game.478514/ .
我认为您可以改用函数 OnMouseDown() (Input.GetMouseButtonDown(0))。
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
区别在于点击对象时会调用OnMouseDown(对象必须有碰撞器)。 Input.GetMouseButtonDown(0) 将在您单击屏幕上的任意位置时调用。
我决定尝试创建一个简单的 Cookie Clicker 游戏作为我的第一个个人项目,没有 tuts 或课程,我设法达到了可以点击我的“Cookie”的程度,但是它向 cookieCounter 添加了 1 unity好像觉得一切都是我的cookie...
不确定是我的代码还是 unity 中的某些设置错误,但我相信这里有人可以为我指明正确的方向。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class cookieCounter : MonoBehaviour
{
public TextMeshProUGUI countText;
public GameObject Cookie;
private int count = 0;
private Rigidbody rb;
private string Object;
// Start is called before the first frame update
void Start()
{
count = 0;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) // On left click
{
Debug.Log(gameObject.name); //Returns the Object of which was clicked on
OnMouseOver();
countText.text = count.ToString("Cookies: 0"); // Updates the cookie counter on screen
Debug.Log(count); // Returns Final Count
}
}
private void OnMouseOver() // Looks at mouse and what it is hovering
{
Object = (gameObject.name); // Detects what the mouse is hovering over
if (Object == "Cookie") // Makes sure the object mouse is hovering is the "Cookie"
{
count++; // Adds 1 to the cookie counter.
}
}
}
请忽略我以前没有做过的低效代码。
你检查鼠标是否被点击,当前对象是"Cookie"
,但你不检查点击发生的地方。如果 Cookie
对象与问题代码共享相同的 Update
/OnMouseOver
方法,则无论点击的位置如何,它都会增加 count
。
您可以通过考虑鼠标位置来修复它。如果 Cookie 是 UI,您可以让 Button
class 处理与检测鼠标点击相关的所有事情(最简单的方法)。
如果 Cookie 是 2D SpriteRenderer
对象(或 3D),那么您可以使用 EventSystem
(可能已经包含在您的项目中)并在 Cookie class 中实现 IPointerClickHandler
.您可以在互联网上找到详细的解决方案,例如 https://forum.unity.com/threads/solved-detecting-mouse-click-on-an-object-in-2d-game.478514/ .
我认为您可以改用函数 OnMouseDown() (Input.GetMouseButtonDown(0))。 https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
区别在于点击对象时会调用OnMouseDown(对象必须有碰撞器)。 Input.GetMouseButtonDown(0) 将在您单击屏幕上的任意位置时调用。