选择要在Unity中加载的保存文件? [英] Choosing which save file to load in Unity?
本文介绍了选择要在Unity中加载的保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我已经设法设置了程序以使用任何文件名进行保存,而且我已经很容易能够在foreach循环中将它们全部选中.如何从那里在Unity 中使用这些保存文件填充Scroll View列表?就像这样:
So, I've already managed to set up the program to save with any filename, and I'm already easily able to pick them all up in a foreach loop. How can I, from there, populate a Scroll View list in Unity with those save files? Like, something like this:
每个保存文件在滚动视图"中占据一行,可以选择其中的一个,您可以点击加载游戏",然后加载.
where every save file would take up a line in the Scroll View, one could be selected, you could hit 'load game', and it would load.
推荐答案
这里是一个简短的教程,介绍了如何做到这一点:
Here is a short tutorial how to do this:
DynamicScrollView.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Events;
public class DynamicScrollView : MonoBehaviour
{
public GameObject Prefab;
public Transform Container;
public List<string> files = new List<string>();
void Start()
{
// files = // LOAD FILE NAMES HERE.
for (int i = 0; i < files.Count; i++)
{
GameObject go = Instantiate(Prefab);
go.GetComponentInChildren<Text>().text = files[i];
go.transform.SetParent(Container);
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
int buttonIndex = i;
go.GetComponent<Button>().onClick.AddListener(() => OnButtonClick(buttonIndex));
}
}
public void OnButtonClick(int index)
{
string file = files[index];
Debug.Log(file);
// Process file here...
}
}
这篇关于选择要在Unity中加载的保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文