您当前的位置: 首页 > 

染指流年灬

暂无认证

  • 4浏览

    0关注

    194博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ScriptableObject批量赋值脚本

染指流年灬 发布时间:2022-03-14 11:46:41 ,浏览量:4

在用到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
                    }
                }
            }
        }
    }
}

关注
打赏
1665909078
查看更多评论
立即登录/注册

微信扫码登录

0.0398s