使用 ui 文本计算上次访问 waypoints 时,如何使其从 1 而不是 0 开始计数?

When counting last visited waypoints using ui text, how to make it to start counting from 1 instead 0?

目标是从 1 开始计数,从 0 开始计数。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class MoveOnCurvedLines : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public float speed;
    public bool go = false;
    public bool moveToFirstPositionOnStart = false;
    public float rotSpeed;
    public bool random = false;
    public int currentCurvedLinePointIndex;
    public Text lastWaypointText;

    private Vector3[] positions;
    private Vector3[] pos;
    private int index = 0;
    private bool goForward = true;
    private List<GameObject> curvedLinePoints = new List<GameObject>();
    private int numofposbetweenpoints;
    private bool getPositions = false;
    int randomIndex;
    int curvedPointsIndex;

    // Start is called before the first frame update
    void Start()
    {
        curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();

        if (curvedLinePoints != null && curvedLinePoints.Count > 0)
        {
            transform.rotation = curvedLinePoints[1].transform.rotation;
        }

        if (random)
            GetNewRandomIndex();

        lastWaypointText.text = curvedPointsIndex + 1.ToString();
    }

    Vector3[] GetLinePointsInWorldSpace()
    {
        positions = new Vector3[lineRenderer.positionCount];
        //Get the positions which are shown in the inspector 
        lineRenderer.GetPositions(positions);


        //the points returned are in world space
        return positions;
    }

    // Update is called once per frame
    void Update()
    {
        if (lineRenderer.positionCount > 0 && getPositions == false)
        {
            pos = GetLinePointsInWorldSpace();
            numofposbetweenpoints = pos.Length / curvedLinePoints.Count;

            if (moveToFirstPositionOnStart == true)
            {
                transform.position = pos[index];
            }

            getPositions = true;
        }

        if (go == true && lineRenderer.positionCount > 0)
        {
            Move();

            Vector3 targetDirection = (curvedLinePoints[c].transform.position - transform.position).normalized;
            curvedLinePoints[c].transform.localRotation = Quaternion.LookRotation(targetDirection);
            transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints[c].transform.localRotation, Time.deltaTime * rotSpeed);
        }

        var dist = Vector3.Distance(transform.position, curvedLinePoints[curvedPointsIndex].transform.position);
        if (dist < 0.1f)
        {
            if (curvedPointsIndex < curvedLinePoints.Count - 1)
            {
                curvedPointsIndex++;

                lastWaypointText.text = curvedPointsIndex.ToString();
            }

            currentCurvedLinePointIndex = curvedPointsIndex;
        }
    }
Start() 中的

curvedPointsIndex 为 0,因此如果有 30 个 curvedLinePoints,它将从 0 到 29 开始计数,但我希望它显示在 lastWaypointText.text 中,从 1 到 30。

我在 Start() 中试过:

lastWaypointText.text = curvedPointsIndex.ToString();

制作

lastWaypointText.text = curvedPointsIndex + 1.ToString();

但这是将文本中的第一个数字设为 01,然后数到 29。

当您执行 lastWaypointText.text = curvedPointsIndex + 1.ToString(); 时,您只是将 ToString 应用于公式的最后一部分,这使得它在 lastWaypointIndex 之后添加“1”。您需要将其括在括号中,而不是像这样:

lastWaypointText.text = (curvedPointsIndex + 1).ToString();