最近一直在考虑关于客户端和关卡编译器之间资源同步的问题,需要让两个工程里面用到的模型以及数据都得相同,应策划要求只能整个工具供他们使用(明明可以复制粘贴,这策划就知道偷懒)。
废话就到这,下面是主要源码,可以实现固定文件夹里面所有的预设体,以及预设相关联的FBX文件,材质球文件,材质球使用的图片文件的同步移动
```csharp
/// <summary>
/// 拷贝预设
/// </summary>
private static void CopyItemPrefabs()
{
List<string> fbxList = new List<string>();
List<string> materialList = new List<string>();
List<string> textList = new List<string>();
#region 目标文件夹
//删除编辑器端预设文件
string prefabPath = Path.Combine(tartgetItemPath, "prefabs");
string startPrefabPath = Path.Combine(Application.dataPath, "Resources/Camp/CampItem");
//预设文件夹
if (!Directory.Exists(prefabPath))
{
Directory.CreateDirectory(prefabPath);
}
CleanDirectory(prefabPath);
//fbx文件夹
string fbxPath = Path.Combine(tartgetItemPath, "fbxs");
if (!Directory.Exists(fbxPath))
{
Directory.CreateDirectory(fbxPath);
}
CleanDirectory(fbxPath);
//material文件夹
string materialPath = Path.Combine(tartgetItemPath, "Materials");
if (!Directory.Exists(materialPath))
{
Directory.CreateDirectory(materialPath);
}
CleanDirectory(materialPath);
//text文件夹
string texturesPath = Path.Combine(tartgetItemPath, "Textures");
if (!Directory.Exists(texturesPath))
{
Directory.CreateDirectory(texturesPath);
}
CleanDirectory(texturesPath);
#endregion
#region
GameObject config = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Bundles/Independent/Config.prefab");
string configStr = config.GetComponent<ReferenceCollector>().Get<TextAsset>("ItemListConfig").text;
List<string> names = new List<string>();
foreach (string str in configStr.Split(new[] { "\n" }, StringSplitOptions.None))
{
try
{
string str2 = str.Trim();
if (str2 == "")
{
continue;
}
ItemListConfig t = ConfigHelper.ToObject<ItemListConfig>(str2);
if (!names.Contains(t.PrefabName))
{
names.Add(t.PrefabName);
}
}
catch (Exception e)
{
Debug.LogError("errpr");
}
}
#endregion
//复制客户端materials文件到编译器
foreach (string subFile in Directory.GetFiles(startPrefabPath))
{
string nullName = Path.GetFileNameWithoutExtension(subFile);
if (!names.Contains(nullName)) continue;
FileInfo fileInfo = new FileInfo(subFile);
File.Copy(fileInfo.FullName, Path.Combine(prefabPath, fileInfo.Name), true);
File.Copy(fileInfo.FullName + ".meta", Path.Combine(prefabPath, fileInfo.Name + ".meta"), true);
GameObject prefab = ResourcesHelper.LoadItem(nullName);
MeshFilter[] meshFilter = prefab.GetComponentsInChildren<MeshFilter>();
foreach (var child in meshFilter)
{
try
{
Mesh filter = child.sharedMesh;
string clothPath = AssetDatabase.GetAssetPath(filter);
FileInfo clothPath_ = new FileInfo(clothPath);
string allName = clothPath_.FullName;
string name = clothPath_.Name;
if (!fbxList.Contains(name))
{
File.Copy(allName, Path.Combine(fbxPath, name), true);
File.Copy(allName + ".meta", Path.Combine(fbxPath, name + ".meta"), true);
fbxList.Add(name);
}
}
catch(Exception e)
{
Debug.LogError(nullName+" -----FBX");
}
}
Renderer[] meshes = prefab.GetComponentsInChildren<Renderer>(true);
foreach (var child in meshes)
{
try
{
string clothPath = AssetDatabase.GetAssetPath(child.sharedMaterial);
FileInfo clothPath_ = new FileInfo(clothPath);
string allName = clothPath_.FullName;
string name = clothPath_.Name;
if (!materialList.Contains(name))
{
File.Copy(allName, Path.Combine(materialPath, name), true);
File.Copy(allName + ".meta", Path.Combine(materialPath, name + ".meta"), true);
materialList.Add(name);
}
}catch(Exception e)
{
Debug.LogError(nullName + " -----Material");
}
try
{
string texture = AssetDatabase.GetAssetPath(child.sharedMaterial.mainTexture);
FileInfo texturePath = new FileInfo(texture);
string textureAllName = texturePath.FullName;
string textureNname = texturePath.Name;
if (!textList.Contains(textureNname))
{
File.Copy(textureAllName, Path.Combine(materialPath, textureNname), true);
File.Copy(textureAllName + ".meta", Path.Combine(materialPath, textureNname + ".meta"), true);
textList.Add(textureNname);
}
}
catch(Exception e)
{
Debug.LogError(nullName + " -----Texture");
}
}
//return;
}
Debug.Log("已导入完预设文件");
}
//删除目标文件夹下面所有文件
public static void CleanDirectory(string dir)
{
foreach (string subdir in Directory.GetDirectories(dir))
{
Directory.Delete(subdir, true);
}
foreach (string subFile in Directory.GetFiles(dir))
{
File.Delete(subFile);
}
}