简体   繁体   English

我如何知道我在 Unity 上单击了哪个游戏对象?

[英]How do I to know on which GameObject I clicked on Unity?

I want to make a Simple Memory Game using C# on Unity.我想在 Unity 上使用 C# 制作一个简单的 Memory 游戏。 It is a 2D game.这是一个2D游戏。 I am trying to make a game in which the PC player clicks in a card, and then the game needs to call the function memory(GameObject card), but I don't know how to do and I can't seen to find it in the internet.我正在尝试制作一个PC玩家点击卡片的游戏,然后游戏需要调用function内存(GameObject卡),但我不知道该怎么做,我找不到它在互联网。 How do I do that?我怎么做? My code is:我的代码是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MemoryGame : MonoBehaviour
{

    private int counterOverturnedCards;
    private GameObject[] overturnedCards = new GameObject[2];
    public Sprite back, krunk, nash, smallNorm, bigNorm, geary, emperorVeloXXVII, realVelo, nTrance, nitrosOxide, zam, zem, mixvelo;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.GetComponent<SpriteRenderer>().sprite = back;
        counterOverturnedCards = 0;
    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnMouseDown()
    {
        if (gameObject.GetComponent<SpriteRenderer>().sprite == back && (counterOverturnedCards == 0 || counterOverturnedCards == 1))
        {
            if (gameObject == GameObject.Find("Cards/card10") || gameObject == GameObject.Find("Cards/card20"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = krunk;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card03") || gameObject == GameObject.Find("Cards/card21"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = nash;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card05") || gameObject == GameObject.Find("Cards/card13"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = geary;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card04") || gameObject == GameObject.Find("Cards/card24"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = mixvelo;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card06") || gameObject == GameObject.Find("Cards/card18"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = smallNorm;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card12") || gameObject == GameObject.Find("Cards/card22"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = bigNorm;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card01") || gameObject == GameObject.Find("Cards/card14"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = emperorVeloXXVII;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card08") || gameObject == GameObject.Find("Cards/card17"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = realVelo;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card07") || gameObject == GameObject.Find("Cards/card19"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = nTrance;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card09") || gameObject == GameObject.Find("Cards/card15"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = zam;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card11") || gameObject == GameObject.Find("Cards/card16"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = zem;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card02") || gameObject == GameObject.Find("Cards/card23"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = nitrosOxide;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
        }
        if (counterOverturnedCards == 2)
        {
            if (overturnedCards[0].GetComponent<SpriteRenderer>().sprite != overturnedCards[1].GetComponent<SpriteRenderer>().sprite)
            {
                StartCoroutine(waitTwoSeconds());
                overturnedCards[0].GetComponent<SpriteRenderer>().sprite = back;
                overturnedCards[1].GetComponent<SpriteRenderer>().sprite = back;
            }
            counterOverturnedCards = 0;
        }
    }

    IEnumerator waitTwoSeconds()
    {
        yield return new WaitForSeconds(2);
    }
}

You have two options:你有两个选择:

1)Physics2D.Raycast , this may work perfectly for your game since you have a single script that should detect all the cards. 1)Physics2D.Raycast ,这可能非常适合您的游戏,因为您有一个应该检测所有卡片的脚本。

void Update () {
         if (Input.GetMouseButtonDown(0)) {
             CastRay();
         }       
     }

     void CastRay() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
         if (hit.collider !=null) {
             Debug.Log (hit.collider.gameObject.name);
     }

2) OnMouseDown , you can attach a script to all of your cards with OnMouseDown method in them. 2) OnMouseDown ,您可以将脚本附加到所有带有 OnMouseDown 方法的卡片上。

  void OnMouseDown(){
     Debug.Log (this.gameObject.name);
  } 

Both options will give you the name of the object, not much of a difference especially for a small game.这两个选项都会为您提供 object 的名称,特别是对于小型游戏而言,差别不大。 The main difference is that Physics2D.Raycast works only for 2D colliders.主要区别在于Physics2D.Raycast仅适用于 2D 对撞机。 On the other hand, OnMouseDown is generic.另一方面, OnMouseDown是通用的。

Please note that you have to have 2D colliders on your game objects for both of the solutions.请注意,对于这两种解决方案,您的游戏对象上都必须有 2D 对撞机。

You could use RaycastHit to trap the GameObject for example:(So you have to put a collider on GameObject)例如,您可以使用 RaycastHit 来捕获 GameObject:(因此您必须在 GameObject 上放置一个对撞机)

In Game 3D:在游戏 3D 中:

     if (Input.GetMouseButtonDown(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit rayHit;
         if (Physics.Raycast(ray, out rayHit, 100.0f)){
             if(rayHit.collider.tag == "Something") //I test tag, but you could what you want..
             {
                 var GameObjClicked  = rayHit.collider.gameObject;
             }
         }
     }

rayHit.collider.gameObject gives you the gameobject Clicked rayHit.collider.gameObject 为您提供点击的游戏对象

To adapt the script with 2D:使用 2D 调整脚本:

    if (Input.GetMouseButtonDown(0)) {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

        RaycastHit2D rayHit = Physics2D.Raycast(mousePos2D, Vector2.zero);
        if (rayHit.collider != null) {
            var gameobjClicked = rayHit.collider.gameObject
        }
    }

A possible solution would be assigning a 2D collider to your card and attach a script to it, with an OnMouseDown() function.一个可能的解决方案是为您的卡片分配一个 2D 对撞机并在其上附加一个脚本,使用 OnMouseDown() function。

For example (still haven't tested it, though):例如(虽然还没有测试过):

public class Card : MonoBehaviour {

public Card(){
        // Constructor, modify it depending on what you need
    }

void OnMouseDown()
    {
        // Find the game object with the "memoria" script as you please. In this example 
        // you find it by name but there are many ways to do this
        GameObject myMemory =  GameObject.Find("Name of the gameobject with the memoria script");
        myMemory.GetComponent<memoria>().memory(gameObject); //Call the memory method and pass the clicked gameObject
    }
}

i would use a raycast to get what object you hit.我会使用光线投射来获得你击中的 object。 you can store it in the cast Ray ray = new Ray(transform.position + offset, dir);你可以将它存储在演员 Ray ray = new Ray(transform.position + offset, dir);

    Debug.DrawRay(transform.position + offset, dir * sightDis, Color.green);
            if (Physics.Raycast(ray, out RaycastHit hit,sightDis * sightDis))
            {
              //  print(hit);

                if (hit.transform == attackTarget) return true;
                //clear line of vision


                // if distances < thershold && raycast from player to boss hits..
                // transtion to prsue

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM