(统一)如何以编程方式使数组元素可点击? [英] (Unity) How can I programmatically make Array Elements Clickable?

查看:154
本文介绍了(统一)如何以编程方式使数组元素可点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组元素,我将它们放在一个空的GameObject上,我的意思是[SerializeField],并通过脚本(C#Ofcourse)添加了,所以这些对象并没有真正在游戏开始时就在生成.如何将Collider从Empty GameObject克隆到克隆上以使其可单击?到目前为止,到目前为止,只有第一个作品的女巫还是空的GameObject的主人,而现在拥有大肠菌的空我也需要把它们放到克隆中...但是,...怎么办?

I have Array Elements I got them on an Empty GameObject, I mean [SerializeField] and added through the script (C# Ofcourse), So the Objects are not really there they are being Generated when the Game begins. How can I clone the Collider from the Empty GameObject onto the clones in order to make them clickable? So far as of right now only the first one works witch is also the possition of the empty GameObject who has the colider on it now I need them onto the Clones as well... but,...How?

我试图将对撞机应用于精灵,甚至试图将它们变成Prefab,这一切无可救药.我确实认为它必须在脚本上,但是我找不到它的代码示例....

I tried to apply the collider to the sprites I even tried turning them into Prefab Its All hopeless. I do think It has to be on the script but I cannot find a code example of it....

public class Controll : MonoBehaviour {
public const int gridRows = 6;
public const int gridCols = 6;
public const float offsetX = 1.70f;
public const float offsetY = 0.97f;

[SerializeField] private GameObject[] cardBack;
[SerializeField] private GameObject[] positioner;

public AudioSource sound;

public void OnMouseDown()
{

    if (Input.GetMouseButtonDown(0))
    {
        sound.Play();
    }
}

//AudioSource audioSource;



// Use this for initialization
void Start ()
{
   // audioSource = GetComponent<AudioSource>();

    Vector3 startPos = positioner[0].transform.position;
    for (int i = 0; i < gridRows; i++)

    {
        for (int j = 0; j < gridCols; j++)
        {

            var position = transform.position + new Vector3(offsetX * j, offsetY * i * -1, -0.1f);
            Instantiate(cardBack[i], position, Quaternion.identity, transform);
        }
   }
}

我需要能够单击这些元素,以便当我单击并消失时它们可以播放声音....

I need to be able to click on these elements so they be playing a sound when i click and disappear....

推荐答案

我想出了一个解决方案...

I figured out a solution...

@ BugFinder 我已经剪下鼠标按下"功能,并将其粘贴到仅具有该功能的单独脚本中,并将其应用于所有预制件,而具有所有其他功能的其他脚本仍保留在游戏对象上.到目前为止,看来一切正常..

@ BugFinder I had cut out the function "on mouse down" and pasted it onto a separated script with just that function and applied that to all the prefabs while the other script with all other functions has stayed on the game-object. So that seems to work well so far.....

这篇关于(统一)如何以编程方式使数组元素可点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆