在用到Scriptable的时候,赋值是个问题。可以参考下面的脚本进行自动赋值
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[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<HistoriesData>(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<Sprite>(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
}
}
}
}
}
}
|