我得到的数组索引超出范围错误,但是我找不到它的来源,有人可以帮忙吗
这是代码
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainEditor : MonoBehaviour
{
string data = "This is my level|this level is about hopping from platform to platform|G4merXsquaD/Grass 0 0 1_Grass 10 0 1";
string textdata;
string leveldata;
string title;
string desc;
string creator;
string currentname;
string currentdesc;
public Transform ErrorT;
string[] Level;
public GameObject SelectMenu;
public GameObject ErrorBox;
public GameObject ContentSelect;
public GameObject ItemSelect;
public GameObject Grass;
// Start is called before the first frame update
public string[] Load(string LevelName)
{
data = PlayerPrefs.GetString("ELVL" + LevelName);
textdata = data.Split('/')[0];
title = textdata.Split('|')[0];
desc = textdata.Split('|')[1];
creator = textdata.Split('|')[2];
leveldata = data.Split('/')[1];
Level = leveldata.Split('_');
string[] returnString = new string[4];
returnString[0] = title; returnString[1] = desc; returnString[2] = creator; returnString[3] = leveldata;
for (int i = 0; i < returnString[3].Length; i++)
{
/*## -> ##*/Debug.Log(" - " + returnString[3] + " - " + returnString[3].Split('_')[i] + " - " + returnString[3].Split('_')[i].Split(' ')[0]);
string tempID = returnString[3].Split('_')[i].Split(' ')[0];
if (tempID == "Grass")
{
GameObject tempOBJ = Instantiate(Grass);
tempOBJ.GetComponent<EditorObject>().setValue(tempID);
}
}
return returnString;
}
public void Error(int num, string message, string desc)
{
GameObject Err = Instantiate(ErrorBox, ErrorT.transform);
Err.GetComponent<ErrorManager>().setValues(num, message, desc);
}
public void Save(string title, string desc, string creator, string leveldata)
{
if (title.Contains("ELVL"))
{
title = title.Replace("ELVL", "");
Error(1, "Cannot have \"ELVL\" in your title", "Due to the way of saving levels you can\' have \"ELVL\" in your title \n\n" +
"it automaticly got removed!");
}
PlayerPrefs.SetString("ELVL" + title, title + "|" + desc + "|" + creator + "/" + leveldata);
if (PlayerPrefs.GetString("CreatedLVLs") == "" || PlayerPrefs.GetString("CreatedLVLs") == null)
PlayerPrefs.SetString("CreatedLVLs", PlayerPrefs.GetString("CreateLVLs") + "ELVL" + title);
else
PlayerPrefs.SetString("CreatedLVLs", PlayerPrefs.GetString("CreateLVLs") + "|ELVL" + title);
SelectMenu.SetActive(false);
Debug.Log("CreatedLVLs: " + PlayerPrefs.GetString("CreatedLVLs") + " / Level was saved: " + PlayerPrefs.GetString("ELVL" + title));
}
public string[] GetLVLs()
{
if (PlayerPrefs.GetString("CreatedLVLs") == "" || PlayerPrefs.GetString("CreatedLVLs") == null)
{
return null;
}
string lvls = PlayerPrefs.GetString("CreatedLVLs");
string[] lvla = lvls.Split('|');
return lvla;
}
public void nameChange(string value)
{
currentname = value;
}
public void descChange(string value)
{
currentdesc = value;
}
void Start()
{
SelectMenu.SetActive(true);
}
public void CreateLVL()
{
Save(currentname, currentdesc, GameData.playername, "Ground 0 -3 1");
Load(currentname);
SelectMenu.SetActive(false);
}
public void LoadLVL()
{
string Data = Load(currentname)[3];
}
public void DeleteLVL()
{
}
public void ListLevels()
{
string[] LVLs = GetLVLs();
if (LVLs != null) {
foreach (string lvlt in LVLs)
{
string tempLVL = lvlt.Replace("ELVL", " ");
GameObject tempObj = Instantiate(ItemSelect, ContentSelect.transform);
}
}
}
public void SaveLevel()
{
}
// Update is called once per frame
void Update()
{
}
}
错误:
IndexOutOfRangeException:数组索引超出范围。
MainEditor.Load(System.String LevelName)(在 资产/脚本/MainEditor.cs:36)MainEditor.CreateLVL()(在 资产/脚本/MainEditor.cs:92)
UnityEngine.Events.InvokableCall.Invoke()(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke()(在 C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press()(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108) UnityEngine.EventSystems.ExecuteEvents.Execute(IPointerClickHandler 处理程序,UnityEngine.EventSystems.BaseEventData eventData)(在 C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute [IPointerClickHandler] (UnityEngine.GameObject目标,UnityEngine.EventSystems.BaseEventData eventData,UnityEngine.EventSystems.EventFunction`1 functor)(at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()
是的,我知道playerprefs并不是路要走,这是一个凌乱的代码,但请坚持使用