Example #1
0
    //创建引用图集
    private static void CreateAssetGo(string path, string name, List <string> pathList)
    {
        if (!Directory.Exists(path + "/" + name))
        {
            AssetDatabase.CreateFolder(path, name);
        }

        string atlasPath = path + "/" + name + "/" + name + _atlasPrefabNameRule;

        atlasPath = atlasPath.Replace("//", "/");
        GameObject atlasGo = AssetDatabase.LoadAssetAtPath <GameObject>(atlasPath);

        if (atlasGo == null)
        {
            GameObject atlasTempGo = new GameObject();
            atlasTempGo.name = name + _atlasPrefabNameRule.Split('.')[0];

            atlasGo = PrefabUtility.CreatePrefab(atlasPath, atlasTempGo);
            GameObject.DestroyImmediate(atlasTempGo);

            Debug.Log("生成prefabAtlas文件:" + atlasPath);
        }

        ExtendAssetMono atlasMono = atlasGo.GetComponent <ExtendAssetMono>();

        if (atlasMono == null)
        {
            atlasMono = atlasGo.AddComponent <ExtendAssetMono>();
        }
        atlasMono.assetList.Clear();

        foreach (string assetPath in pathList)
        {
            Sprite spr = AssetDatabase.LoadAssetAtPath <Sprite>(assetPath);
            if (spr == null)
            {
                Debug.LogError("Texture2D文件错误,path:" + assetPath);
                continue;
            }
            atlasMono.assetList.Add(spr);
        }

        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
    }
Example #2
0
    public static void UpdatePrefab()
    {
        InitConfig();

        string[] files = System.IO.Directory.GetFiles(_modules_path, "*", SearchOption.AllDirectories);

        //获取公共图集和材质
        Material   commonMat         = AssetDatabase.LoadAssetAtPath <Material>(_mat_path + "/" + _commonFolder + "/" + _commonFolder + _matNameRule);
        GameObject commonAtlasPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(_atlasPrefab_path + "/" + _commonFolder + "/" + _commonFolder + _atlasPrefabNameRule);

        ExtendAssetMono commomAssets = commonAtlasPrefab.GetComponent <ExtendAssetMono>();

        Dictionary <string, Sprite> name2SpriteDict = new Dictionary <string, Sprite>();

        foreach (UnityEngine.Object obj in commomAssets.assetList)
        {
            Sprite spt = obj as Sprite;
            name2SpriteDict.Add(spt.name, spt);
        }

        for (int i = 0; i < files.Length; i++)
        {
            string path = files[i];

            if (_filterList.Contains(Path.GetExtension(path)))
            {
                continue;
            }

            path = path.Replace(@"\", @"/");

            GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>(path);
            if (go == null)
            {
                Debug.LogError(path);
                break;
            }

            FileInfo fileInfo = new FileInfo(path);
            string   dirName  = fileInfo.Directory.Name;

            //对应模块的材质
            Material mat = AssetDatabase.LoadAssetAtPath <Material>(_mat_path + "/" + dirName + "/" + dirName + _matNameRule);

            Image[] imgs = go.GetComponentsInChildren <Image>(true);

            foreach (Image img in imgs)
            {
                if (img.sprite != null)
                {
                    if (name2SpriteDict.ContainsKey(img.sprite.name) || mat == null)
                    {
                        img.material = commonMat;
                    }
                    else if (mat != null)
                    {
                        img.material = mat;
                    }
                }
            }
        }

        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
    }