123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- namespace AtlasUtility
- {
- using UnityEditor;
- using UnityEngine;
- using UnityEditor.SceneManagement;
- using System;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- public class ReferenceManager
- {
- public static void ChangeReference(List<string> fromReferenceList, List<string> toReferenceList)
- {
- if (fromReferenceList.Count != toReferenceList.Count)
- {
- throw new Exception("An error occured");
- }
- if (fromReferenceList.Count == 0)
- {
- return;
- }
- AssetDatabase.SaveAssets();
- EditorSceneManager.SaveOpenScenes();
- SerializationMode userSerializationMode = EditorSettings.serializationMode;
- EditorSettings.serializationMode = SerializationMode.ForceText;
- List<string> searchExtension = new List<string>()
- {
- ".asset",
- ".anim",
- ".unity",
- ".prefab"
- };
- List<string> pathList = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories).Where
- (
- path =>
- {
- string extension = Path.GetExtension(path);
- if (searchExtension.Contains(extension))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- )
- .ToList();
- for (int i = 0; i < pathList.Count; i++)
- {
- EditorUtility.DisplayProgressBar("Changing reference", Path.GetFileNameWithoutExtension(pathList[i]), (i + 1)/(float) pathList.Count);
- string originText = File.ReadAllText(pathList[i]);
- string newText = originText;
- for (int j = 0; j < fromReferenceList.Count; j++)
- {
- newText = newText.Replace(fromReferenceList[j], toReferenceList[j]);
- }
- if (newText != originText)
- {
- File.WriteAllText(pathList[i], newText);
- }
- }
- EditorUtility.ClearProgressBar();
- EditorSettings.serializationMode = userSerializationMode;
- AssetDatabase.Refresh();
- }
- public static void ChangeAllReference()
- {
- if (!EditorUtility.DisplayDialog("Warning", "Are you sure you want to set all the references?", "Go ahead", "Cancel"))
- {
- return;
- }
- List<string> fromReferenceList = new List<string>();
- List<string> toReferenceList = new List<string>();
- List<string> itemList = ReferenceTable.ReadAllLine();
- for (int i = 0; i < itemList.Count; i++)
- {
- itemList[i] = itemList[i].TrimEnd((char)13);
- string atlasReference = itemList[i].Split('|')[0];
- string sourceReference = itemList[i].Split('|')[1];
- if (fromReferenceList.Contains(sourceReference))
- {
- int index = fromReferenceList.IndexOf(sourceReference);
- Debug.LogWarning($"Repeat sprite detected {ReferenceToFakePath(atlasReference)} {ReferenceToFakePath(toReferenceList[index])}");
- }
- fromReferenceList.Add(sourceReference);
- toReferenceList.Add(atlasReference);
- }
- ChangeReference(fromReferenceList, toReferenceList);
- }
- public static void ResetReference(List<string> pngPathList)
- {
- if (pngPathList.Count == 0)
- {
- return;
- }
- List<string> fromReferenceList = new List<string>();
- List<string> toReferenceList = new List<string>();
- List<string> itemList = ReferenceTable.ReadAllLine();
- foreach (string path in pngPathList)
- {
- string atlasGUID = AssetDatabase.AssetPathToGUID(path);
- for (int i = 0; i < itemList.Count; i++)
- {
- itemList[i] = itemList[i].TrimEnd((char)13);
- string atlasReference = itemList[i].Split('|')[0];
- string sourceReference = itemList[i].Split('|')[1];
- if (atlasReference.Contains(atlasGUID))
- {
- fromReferenceList.Add(atlasReference);
- toReferenceList.Add(sourceReference);
- itemList.RemoveAt(i--);
- }
- }
- ChangeReference(fromReferenceList, toReferenceList);
- ReferenceTable.WriteAllLine(itemList);
- }
- }
- public static void ResetAllReference()
- {
- if (!EditorUtility.DisplayDialog("Warning", "Are you sure you want to reset all the references?", "Go ahead", "Cancel"))
- {
- return;
- }
- List<string> fromReferenceList = new List<string>();
- List<string> toReferenceList = new List<string>();
- List<string> itemList = ReferenceTable.ReadAllLine();
- for (int i = 0; i < itemList.Count; i++)
- {
- itemList[i] = itemList[i].TrimEnd((char)13);
- string atlasReference = itemList[i].Split('|')[0];
- string sourceReference = itemList[i].Split('|')[1];
- fromReferenceList.Add(atlasReference);
- toReferenceList.Add(sourceReference);
- }
- ChangeReference(fromReferenceList, toReferenceList);
- ReferenceTable.Clear();
- }
- public static string ReferenceToFakePath(string reference)
- {
- string guid = ReferenceToGUID(reference);
- string fileId = ReferenceToFileId(reference);
- int spriteIndex = (int.Parse(fileId) - 21300000)/2;
-
- string atlasPath = AssetDatabase.GUIDToAssetPath(guid);
- TextureImporter textureImporter = (TextureImporter) AssetImporter.GetAtPath(atlasPath);
- return $"{atlasPath}/{textureImporter.spritesheet[spriteIndex].name}";
- }
- public static string ReferenceToGUID(string reference)
- {
- return reference.Split(',')[1].Split(' ')[2];
- }
- public static string ReferenceToFileId(string reference)
- {
- return reference.Split(',')[0].Split(' ')[1];
- }
- public static List<string> GetChildPathList(string path)
- {
- List<string> pathList = new List<string>();
- List<string> itemList = ReferenceTable.ReadAllLine();
- string atlasGUID = AssetDatabase.AssetPathToGUID(path);
-
- for (int i = 0; i < itemList.Count; i++)
- {
- itemList[i] = itemList[i].TrimEnd((char) 13);
- string atlasReference = itemList[i].Split('|')[0];
- string sourceReference = itemList[i].Split('|')[1];
- if (atlasReference.Contains(atlasGUID))
- {
- pathList.Add(AssetDatabase.GUIDToAssetPath(ReferenceToGUID(sourceReference)));
- }
- }
- return pathList;
- }
- }
- }
|