예제 #1
0
        public static void SOtoExcel(SOList soList, ExcelSheet excelSheet)
        {
            var bindInfo = soList.BindInfo;

            using (var map = excelSheet.ToExcelMapper(bindInfo.HeadRow, bindInfo.StartRow, bindInfo.Key))
            {
                for (int i = 0; i < soList.List.Count; i++)
                {
                    var    DynObj   = Dyn.Object(soList.List[i]);
                    string keyValue = DynObj.Q <KeyFieldBindAttribute>().GetValue()?.ToString();

                    if (map.ContainsID(keyValue) == false)
                    {
                        map.AddRow(keyValue);
                    }

                    var members = DynObj.GetDynMembers <FieldBindAttribute>();
                    for (int j = 0; j < members.Count; j++)
                    {
                        var cell = map[keyValue, members[j].Item2.FieldName];
                        if (cell != null)
                        {
                            cell.Value = members[j].Item1.GetValue();
                        }
                        else
                        {
                            Debug.LogError($"excel表中找不到{keyValue}.{members[j].Item2.FieldName}");
                        }
                    }
                }
                map.Save();
            }
        }
예제 #2
0
        public static void ExcelToSO(ExcelSheet excelSheet, SOList soList)
        {
            var bindInfo  = soList.BindInfo;
            var excelList = excelSheet.ToList(bindInfo.HeadRow, bindInfo.StartRow, bindInfo.Key);

            for (int i = 0; i < excelList.Count; i++)
            {
                var excelRow = excelList[i];
                var keyValue = excelRow[bindInfo.Key];
                var exist    = false;
                for (int j = 0; j < soList.List.Count; j++)
                {
                    var so     = soList.List[j];
                    var DynObj = Dyn.Object(so);
                    var item   = DynObj.Q <KeyFieldBindAttribute>().GetValue();

                    //if(item.ToString() == keyValue.ToString())
                    //{
                    //    Debug.Log($"=={ConvertAndEquals(item, keyValue)}");
                    //}

                    if (item != null && ConvertAndEquals(item, keyValue))// item.ToString() == keyValue.ToString())// item.Equals(keyValue))
                    {
                        exist = true;
                        foreach (var rowItem in excelRow)
                        {
                            DynObj.Q <FieldBindAttribute>((attr) => attr.FieldName == rowItem.Key).SetValue(rowItem.Value);
                        }
                    }
                }

                if (!exist)
                { // so中不存在,创建新的
                    var so     = ScriptableObject.CreateInstance(soList.ScriptableObjectType);
                    var DynObj = Dyn.Object(so);
                    DynObj.Q <KeyFieldBindAttribute>().SetValue(keyValue);
                    foreach (var rowItem in excelRow)
                    {
                        DynObj.Q <FieldBindAttribute>((attr) => attr.FieldName == rowItem.Key).SetValue(rowItem.Value);
                    }
                    var assetInfo    = so as INewAsset;
                    var pathFileName = assetInfo == null?keyValue.ToString() : assetInfo.NewAsset().GetPath();

                    var pathAll = $"{soList.Path}/{pathFileName}.asset";
                    if (assetInfo != null)
                    {
                        AssetDatabase.CreateAsset(so, pathAll);
                        AssetImporter.GetAtPath(pathAll).assetBundleName = assetInfo.NewAsset().AssetBundle;
                    }
                    else
                    {
                        AssetDatabase.CreateAsset(so, pathAll);
                    }
                }
            }
        }
예제 #3
0
        //private Type _type;
        //private string _path;

        public SOList(List <Object> list, Type type, string path)
        {
            List = list;
            ScriptableObjectType = type;
            Path = path;
            if (type.IsSubclassOf(typeof(ScriptableObject)) == false)
            {
                throw new Exception($"{type} 需要是继承 ScriptableObject类");
            }
            BindInfo = Dyn.GetAttribute <ExcelBindAttribute>(type);
        }