当前位置: 代码迷 >> 综合 >> Sprites,Editor使图片生成prefab
  详细解决方案

Sprites,Editor使图片生成prefab

热度:76   发布时间:2024-01-18 06:57:13.0
    /// <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);}}}
  相关解决方案