Sprites,Editor使图片生成prefab

来源:互联网 发布:淘宝静物相机 编辑:程序博客网 时间:2024/06/07 10:52
    /// <summary>    /// 使表情生成prefab    /// </summary>    [MenuItem("SpritesPacker/_使互动表情生成prefabs-别乱点")]    public static void MakeStickerAtlas()    {        UnityEngine.Debug.Log("碎图 表情--用时再打开");          return;//用时再打开        string parentPath = Application.dataPath + "/Resources/Prefab/Stickers";        if (Directory.Exists(parentPath) == false)            Directory.CreateDirectory(parentPath);        //图片原路径        DirectoryInfo uiStickers = new DirectoryInfo(Application.dataPath + "/Art/UI/Stickers");        foreach (DirectoryInfo item in uiStickers.GetDirectories())//可以有子文件夹        {            string tempPath = parentPath + "/" + Path.GetFileName(item.FullName);            if (Directory.Exists(parentPath + Path.GetFileName(item.FullName)) == false)            {//prefab生成在该目录中                Directory.CreateDirectory(parentPath + "/" + Path.GetFileName(item.FullName));            }            foreach (FileInfo pngItem in item.GetFiles("*.png", SearchOption.AllDirectories))            {                string allPath = pngItem.FullName;                string assetPath = allPath.Substring(allPath.IndexOf("Assets"));                Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);                GameObject go = new GameObject(sprite.name);                go.AddComponent<SpriteRenderer>().sprite = sprite;                                allPath = tempPath + "/" + sprite.name + ".prefab";                string prefabPath = allPath.Substring(allPath.IndexOf("Assets"));                PrefabUtility.CreatePrefab(prefabPath, go);                GameObject.DestroyImmediate(go);            }        }    }
原创粉丝点击