首页 > 解决方案 > 为什么我不能让触摸输入(Unity with C#)工作?

问题描述

我正在尝试完成我一直在开发的 Android 游戏。我首先使用鼠标控制机制,但由于它需要使用多点触控来控制射击按钮和瞄准区域,我尝试根据教程将脚本转换为触控输入,但它似乎没有注册任何触控。

另外,我希望脚本在开始时记录是否有一个触摸(无论顺序如何)被放置在用于射击或瞄准控制的区域上,并将其放置在 Vector3 列表中。当体面的触摸被解除时,它会重置该列表索引中的值。然后,例如,射击脚本检查列表值之一是否在射击按钮半径范围内,因此它激活武器加载。它一直告诉我这个错误:

ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引

Here are the class codes for the touch inputs and for the shooting button:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchControls : MonoBehaviour

{
    public static bool shotTime = false;
    public static List <Vector3> position = new List<Vector3>(2);
    public static bool moveTime = false;
    // Start is called before the first frame update
    void Start()
    {
        position[0] = new Vector3(0, 0, 0);
        position[1] = new Vector3(0, 0, 0);
    }

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < Input.touchCount && i < 2; i++)
        {
            Debug.Log("Touch");
            Touch touch = Input.GetTouch(i);
            Vector3 pos = Camera.main.ScreenToWorldPoint(touch.position);
            pos.z = 0;
            if (touch.phase == TouchPhase.Began)
            {
                position[i] = pos;
            }
            if (touch.phase == TouchPhase.Ended)
            {
                position[i] = new Vector3(0, 0, 0);
            }
        }
        Debug.Log(position[0] + position[1]);
    }
}

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

public class ShotButton : MonoBehaviour
{
    public static bool time = false;
    private Vector3 scale;
    // Start is called before the first frame update
    void Start()
    {
        scale = gameObject.transform.localScale;
    }

    // Update is called once per frame
    void Update()
    {
        {
            if (Vector2.Distance(gameObject.transform.position, TouchControls.position[0]) < scale.x || Vector2.Distance(gameObject.transform.position, TouchControls.position[1]) < scale.x)
            {
                time = true;
                gameObject.transform.localScale = scale * 0.9f;
            }
            else
            {
                time = false;
                gameObject.transform.localScale = scale;
            }
        }
    }
}

代码有什么问题?我该如何解决?

标签: c#androidunity3d

解决方案


对于列表,这是获取某个索引处的元素的方式

public T this[int index]
{
    get
    {
        if (index >= this._size)
            throw new ArgumentOutOfRangeException("...");
        return this._items[index];
    }
    set
    {
        if (index >= this._size)
            throw new ArgumentOutOfRangeException("...");
        this._items[index] = value;
    }
}

所以它的作用是检查_size变量而不是容量,如果您请求的索引大于它,则抛出异常。

_size除非您将非空集合传递给构造函数,否则初始化为 0,并在使用Add, RemoveAt,Clear等方法时更改值。

列表的Capacity表示列表当前为当前对象和要添加到其中的对象预留了多少内存。列表的计数(此处_size)是实际添加到列表中的项目数。

因此,您需要将元素添加到列表中。我认为第一个脚本Start()没有被调用(所以大小保持为 0)可能是因为脚本没有启用。

即使没有调用该方法,您也可以确保根据您的需要Start()调用或放入Start()逻辑。我希望这有帮助。Awake()Start()


推荐阅读