Example #1
0
        internal static void ToAsset(string savePath, Dictionary <int, List <Cell> > dic, string tableName = null)
        {
            string           className = tableName ?? new FileInfo(savePath).Name.Replace(".asset", "");
            ScriptableObject table     = EditorUtils.CreateAsset <ScriptableObject>(className, savePath.Replace(EditorUtils.DataPath, ""));

            IDataCollection db = table as IDataCollection;

            if (db == null)
            {
                return;
            }
            db.Clear();
            var type = EditorUtils.GetType(tableName.Replace("Table", "Info"));

            foreach (var pair in dic)
            {
                var classInstance = type.Assembly.CreateInstance(type.FullName);
                foreach (var cell in pair.Value)
                {
                    var fileInfo = type.GetField(cell.name);
                    if (fileInfo == null)
                    {
                        continue;
                    }
                    if (string.IsNullOrEmpty(cell.value))
                    {
                        continue;
                    }
                    fileInfo.SetValue(classInstance, cell.value, fileInfo.FieldType);
                }
                db.Add(classInstance);
            }

            EditorUtility.SetDirty(table);
            AssetDatabase.SaveAssets();
        }