在用到Scriptable的时候,赋值是个问题。可以参考下面的脚本进行自动赋值
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
///
/// 编辑器模式下半自动填充HistoriesData文件中的pictures列表
///
[ExecuteInEditMode]
public class AutoAsignHistoricalPictures : MonoBehaviour
{
[SerializeField] private bool autoAsign;// 是否需要自动填充
private void OnEnable()
{
if (autoAsign)
{
autoAsign = false;
AutoAsign();
}
}
private const string PATH_OF_HISTORIES = "Gallery/Histories"; // 历史数据所在的路径
private const string PATH_OF_PICTURES = "Gallery/Pictures"; // 历史照片所在的路径
private const string FORMAT_OF_PATH = "{0}/Unit{1}/Topic{2}"; // 资源路径格式
private const string FORMAT_OF_PATH_AND_FILE_NAME = "{0}/{1}"; // 资源路径加文件名称格式
private void AutoAsign()
{
HistoriesData[] historiesDatas = Resources.LoadAll(PATH_OF_HISTORIES);
if (historiesDatas != null && historiesDatas.Length > 0)
{
foreach (HistoriesData historiesData in historiesDatas)
{
if (historiesData != null && historiesData.pictureNames != null && historiesData.pictureFilePaths != null)
{
historiesData.pictureNames.Clear();
historiesData.pictureFilePaths.Clear();
string pathOfTopicPictures = string.Format(FORMAT_OF_PATH, PATH_OF_PICTURES, historiesData.unitId, historiesData.topicId);
Sprite[] pictures = Resources.LoadAll(pathOfTopicPictures);
if (pictures != null && pictures.Length > 0)
{
foreach (Sprite sprite in pictures)
{
historiesData.pictureNames.Add(sprite.name);
historiesData.pictureFilePaths.Add(string.Format(FORMAT_OF_PATH_AND_FILE_NAME, pathOfTopicPictures, sprite.name));
}
#if UNITY_EDITOR
EditorUtility.SetDirty(historiesData);// 修改数据文件之后,通知系统该文件已经被修改
#endif
}
}
}
}
}
}