コード例 #1
0
    //[MenuItem("UI/Create Atlas")]
    public static void CreateUIAtlas()
    {
        string rootFloder = Application.dataPath + "/UI_Atlas/";

        if (!Directory.Exists(rootFloder))
        {
            return;
        }

        string targetFloder = Application.dataPath + "/Resources/UI/Atlas/";

        if (!Directory.Exists(targetFloder))
        {
            Directory.CreateDirectory(targetFloder);
        }

        List <string> lastnames = new List <string>();
        DirectoryInfo last      = new DirectoryInfo(targetFloder);

        FileInfo[] pfbs = last.GetFiles("*.prefab", SearchOption.AllDirectories);
        foreach (FileInfo pfb in pfbs)
        {
            string name = pfb.FullName.Substring(targetFloder.Length);
            name = name.Substring(0, name.IndexOf("."));
            name = name.Replace("\\", "/");
            lastnames.Add(name);
        }

        DirectoryInfo dir = new DirectoryInfo(rootFloder);

        DirectoryInfo[] childs = dir.GetDirectories("*", SearchOption.AllDirectories);
        foreach (DirectoryInfo child in childs)
        {
            string subpath = child.FullName.Substring(Application.dataPath.IndexOf("Assets"));
            subpath = subpath.Replace("\\", "/");
            subpath = subpath.Substring("Assets/UI_Atlas/".Length);
            lastnames.Remove(subpath);

            string floder = Path.GetDirectoryName(targetFloder + subpath);
            if (!Directory.Exists(floder))
            {
                Directory.CreateDirectory(floder);
            }

            string atlasName = "Atlas/" + subpath;

            List <Sprite> sprites = new List <Sprite>();
            FileInfo[]    files   = child.GetFiles("*.png");
            foreach (FileInfo file in files)
            {
                string old = File.ReadAllText(file.FullName + ".meta", System.Text.Encoding.Default);
                File.Move(file.FullName + ".meta", file.FullName + ".meta.old");
                //*
                YamlSerializer serializer            = new YamlSerializer();
                object[]       rsl                   = serializer.Deserialize(old);
                Dictionary <object, object> settings = rsl[0] as Dictionary <object, object>;
                string createTime = settings["timeCreated"].ToString();
                Dictionary <object, object> TextureImporter = settings["TextureImporter"] as Dictionary <object, object>;
                string wm  = "1";
                string spx = "0";
                string spy = "0";
                string sbx = "0";
                string sby = "0";
                string sbz = "0";
                string sbw = "0";
                string an  = "";
                string av  = "";
                if (TextureImporter != null)
                {
                    int textureType = (int)TextureImporter["textureType"];
                    if (textureType == 8)
                    {
                        Dictionary <object, object> sp = TextureImporter["spritePivot"] as Dictionary <object, object>;
                        spx = sp["x"].ToString();
                        spy = sp[true].ToString();

                        Dictionary <object, object> sb = TextureImporter["spriteBorder"] as Dictionary <object, object>;
                        sbx = sb["x"].ToString();
                        sby = sb[true].ToString();
                        sbz = sb["z"].ToString();
                        sbw = sb["w"].ToString();

                        Dictionary <object, object> textureSettings = TextureImporter["textureSettings"] as Dictionary <object, object>;
                        wm = textureSettings["wrapMode"].ToString();
                    }
                    an = TextureImporter["assetBundleName"] == null ? "" : TextureImporter["assetBundleName"].ToString();
                    av = TextureImporter["assetBundleVariant"] == null ? "" : TextureImporter["assetBundleVariant"].ToString();
                }

                string guid = AssetDatabase.AssetPathToGUID("Assets/UI_Atlas/" + subpath + "/" + file.Name);
                string str  = metastring.Replace("#GUID#", guid);
                str = str.Replace("#TC#", createTime);
                str = str.Replace("#WM#", wm);
                str = str.Replace("#SPX#", spx);
                str = str.Replace("#SPY#", spy);
                str = str.Replace("#SBX#", sbx);
                str = str.Replace("#SBY#", sby);
                str = str.Replace("#SBZ#", sbz);
                str = str.Replace("#SBW#", sbw);
                str = str.Replace("#PT#", atlasName);
                str = str.Replace("#AN#", an);
                str = str.Replace("#AV#", av);
                //*/
                try
                {
                    File.WriteAllText(file.FullName + ".meta", str, System.Text.Encoding.Default);
                }
                catch (System.Exception ex)
                {
                    Debug.LogError(ex);
                    File.Copy(file.FullName + ".meta.old", file.FullName + ".meta", true);
                }
                File.Delete(file.FullName + ".meta.old");

                AssetDatabase.Refresh();
                Object[] objs = AssetDatabase.LoadAllAssetsAtPath("Assets/UI_Atlas/" + subpath + "/" + file.Name);
                foreach (Object obj in objs)
                {
                    if (obj.GetType() == typeof(Sprite))
                    {
                        sprites.Add(obj as Sprite);
                    }
                }
            }

            GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>("Assets/Resources/UI/Atlas/" + subpath + ".prefab");
            if (go == null)
            {
                go = new GameObject();
                UIAtlas atlas = go.AddComponent <UIAtlas>();
                atlas.SetSprites(sprites);
                PrefabUtility.CreatePrefab("Assets/Resources/UI/Atlas/" + subpath + ".prefab", go);
                DestroyImmediate(go);
            }
            else
            {
                GameObject go2   = PrefabUtility.InstantiatePrefab(go) as GameObject;
                UIAtlas    atlas = go2.GetComponent <UIAtlas>();
                if (atlas == null)
                {
                    atlas = go2.AddComponent <UIAtlas>();
                    atlas.SetSprites(sprites);
                    PrefabUtility.ReplacePrefab(go2, go, ReplacePrefabOptions.ConnectToPrefab);
                }
                else
                {
                    bool b = false;
                    for (int i = 0; i < sprites.Count; ++i)
                    {
                        if (sprites[i] != null && atlas[sprites[i].name] != sprites[i])
                        {
                            b = true;
                            break;
                        }
                    }
                    if (b || sprites.Count != atlas.Count)
                    {
                        atlas.SetSprites(sprites);
                        PrefabUtility.ReplacePrefab(go2, go, ReplacePrefabOptions.ConnectToPrefab);
                    }
                }
                DestroyImmediate(go2);
            }
        }

        foreach (string str in lastnames)
        {
            File.Delete(targetFloder + str + ".prefab");
        }

        AssetDatabase.Refresh();
    }