最近一直在考慮關於客戶端和關卡編譯器之間資源同步的問題,需要讓兩個工程裏面用到的模型以及數據都得相同,應策劃要求只能整個工具供他們使用(明明可以複製粘貼,這策劃就知道偷懶)。
廢話就到這,下面是主要源碼,可以實現固定文件夾裏面所有的預設體,以及預設相關聯的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);
}
}