# Unity中做力扣算法题（Day5 数组跳跃游戏）

失败：

SphereJump类

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

public class SphereJump : MonoBehaviour
{
[SerializeField]
public Transform[] transforms;//记录九个位置的trs组，记得在Unity中赋值

public int step;//输入的步数
public GameObject Sphere;//球
public GameObject Text_1;//最大步数文本
public Button m_QueDingBtn;//确定按钮

private int curSetp;//当前步数
private int nums_1;//在数组中的位置

//public bool ground;
void Awake()
{
CreateNums();
curSetp = 0;
nums_1 = 0;
//ground = true;
}

void Start()
{

CaculateMaxStep(0);

}

void Update()
{
SuccessOrFail();
}

#region 实现空格控制球体跳跃（需要的可以自己取消注释）
//public void FixedUpdate()
//{
//    if (Input.GetKeyDown(KeyCode.Space))
//    {
//        if (ground == true)
//        {
//            transform.Translate(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")));
//            GetComponent<Rigidbody>().velocity += new Vector3(0, 10, 0);
//            ground = false;
//        }

//        DestroyShpere();
//    }
//}

//void DestroyShpere()
//{
//    y = Sphere.transform.position.y;
//    if (y >= 10)
//    {
//        Destroy(Sphere);
//    }
//}
#endregion

/// <summary>
/// 创建随机数组
/// </summary>
public void CreateNums()
{

for (int i = 0; i < NumsManager.nums.Length; i++)
{
NumsManager.num = UnityEngine.Random.Range(0, 9);//随机数
};
}
/// <summary>
/// 计算步数并实现
/// </summary>
public void CaculateStep()
{
var m_Step = GameObject.Find("InputField").gameObject.transform.GetComponentInChildren<InputField>().text;//获取输入的步数
step = Convert.ToInt32(m_Step);
var m_MaxStep = GameObject.Find("01").gameObject.transform.GetComponentInChildren<Text>().text;
int nums_1 = Convert.ToInt32(m_MaxStep);
if (step<= nums_1)
{
gameObject.transform.position = transforms[curSetp + step].position;
curSetp = curSetp + step;
}
else
{
return;
}
//Text_1.GetComponent<Text>().text = "0";
CaculateMaxStep(step);
}
/// <summary>
/// 计算最大步数
/// </summary>
/// <param name="offset"></param>
public void CaculateMaxStep(int offset)
{
var text = NumsManager.numsList[nums_1+offset].ToString();//最大步数等于数组位置的数加上偏移量
nums_1 = nums_1 + offset;//实时更新在数组中的位置
Text_1.GetComponent<Text>().text = text;

}
/// <summary>
/// 判断成功还是失败
/// </summary>
public void SuccessOrFail()
{
float trsY = Sphere.transform.position.y;
var m_materials = Sphere.GetComponent<MeshRenderer>();//获得当前球的材质
//如果球的Y值等于0.5就相当于落到地面
if (trsY == 0.5f)
{
}
//如果球的位置等于trs数组第8位就变黄
if (Sphere.transform.position == transforms[8].position)
{
}
}

}
``````

NumsManager静态类：

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

public static class NumsManager
{
public static int value01;
public static int value02;
//九个位置
public static Vector3 trs01 = new Vector3(94.5f, 12.0f, 10);
public static Vector3 trs02 = new Vector3(95.5f, 12.0f, 10);
public static Vector3 trs03 = new Vector3(96.5f, 12.0f, 10);
public static Vector3 trs04 = new Vector3(97.5f, 12.0f, 10);
public static Vector3 trs05 = new Vector3(98.5f, 12.0f, 10);
public static Vector3 trs06 = new Vector3(99.5f, 12.0f, 10);
public static Vector3 trs07 = new Vector3(100.5f, 12.0f, 10);
public static Vector3 trs08 = new Vector3(101.5f, 12.0f, 10);
public static Vector3 trs09 = new Vector3(102.5f, 12.0f, 10);
public static int[] nums = new int[10];
public static int num;
public static List<int> numsList = new List<int>();

}
``````

NumsJump类

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

public class NumsJump : MonoBehaviour
{
public GameObject Cube;

public int a;
public float m_trsY;

private Vector3 m_trs;

void Awake()
{
m_trs = gameObject.transform.position;
}
void Start()
{
//根据gameobject的名字拿数组的值
string name = gameObject.name;
switch (name)
{
case "0":
GetNumsValue(0);
break;
case "1":
GetNumsValue(1);
break;
case "2":
GetNumsValue(2);
break;
case "3":
GetNumsValue(3);
break;
case "4":
GetNumsValue(4);
break;
case "5":
GetNumsValue(5);
break;
case "6":
GetNumsValue(6);
break;
case "7":
GetNumsValue(7);
break;
case "8":
GetNumsValue(8);
break;
case "9":
GetNumsValue(9);
break;

}
NewSphere(NumsManager.value02);
a = NumsManager.value02;
}

/// <summary>
/// 实例化方块
/// </summary>
/// <param name="index"></param>
void NewSphere(int index)
{
for (int i = 0; i < index; i++)
{
GameObject.Instantiate(Cube, m_trs, Quaternion.identity);
m_trs.y += 2;
}
}
/// <summary>
/// 拿到数组的值
/// </summary>
/// <param name="index"></param>
void GetNumsValue(int index)
{
NumsManager.value02 = NumsManager.numsList[index];
}

}
``````

THE END