unity | 图片放入指定文件夹自动变成sprite精灵模式

一、理解我们要做的事

        原本图片放入文件夹后是Default(默认)模式,但是需要大量处理图片的时候,我们希望它拖进去就是sprite模式 = 我们想修改unity原本的功能。

二、unity是允许我们去修改它本身的一些功能的,你可以定制属于你的unity

        这需要用到UnityEditor,所以我们需要在代码里加上using UnityEditor;

三、unityEditor

        因为我们现在想要修改的是导入图片时改变图片模式,所以这部分内容在导入这里,因此我们需要继承AssetPostprocessor

using UnityEditor;
using UnityEngine;

public class SpriteProcessor : AssetPostprocessor
{}

四、开始写代码

TextureImporter 纹理导入器,用这个类可以修改导入unity图片的设置

using UnityEditor;
using UnityEngine;

public class SpriteProcessor : AssetPostprocessor
{
    //这是人家写好的方法,当你把图片搞进来时
    private void OnPostprocessTexture(Texture2D texture) {
        //如果我们的文件夹里有Sprites这个名字
        if (assetPath.IndexOf("/Sprites/")!= -1)
        {
            //assetImporter就是拖进来的东西,在AssetProstprocessor类里写好的,你可以直接用
            //强制转换成TextureImporter
            TextureImporter textureImporter = (TextureImporter)assetImporter;
            //把格式改成sprite
            textureImporter.textureType = TextureImporterType.Sprite;
            //图片不用分割,就一张
            textureImporter.spriteImportMode = SpriteImportMode.Single;
            //有透明通道
            textureImporter.alphaIsTransparency = true;
            //不需要图片深度
            textureImporter.mipmapEnabled = false;
        }
    }
}

五、注意事项

        这是我们改编辑器的代码,你在打包的时候是不需要去打包这部分内容的。所以你需要专门建一个文件夹Editor,把它放进去,这是unity专门留给我们的文件夹,它不会因为编辑器的部分影响打包和运行。

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Unity自动生成并加载`StreamingAssets`文件夹中的图片列表,你可以使用`System.IO`命名空间和Unity的UI系统。下面是一个示例代码: ```csharp using UnityEngine; using UnityEngine.UI; using System.IO; public class ImageListLoader : MonoBehaviour { public GameObject imagePrefab; // 图片预制体 public Transform content; // 列表的内容容器 private string imageFolderPath = "Images"; // 图片文件夹路径 private void Start() { LoadImages(); } private void LoadImages() { string streamingPath = Path.Combine(Application.streamingAssetsPath, imageFolderPath); if (Directory.Exists(streamingPath)) { string[] imageFiles = Directory.GetFiles(streamingPath); foreach (string imagePath in imageFiles) { // 加载图片预制体 GameObject imageGO = Instantiate(imagePrefab, content); // 获取图片名称 string imageName = Path.GetFileNameWithoutExtension(imagePath); // 设置图片预制体的名称 imageGO.name = imageName; // 设置图片显示 Image imageComponent = imageGO.GetComponent<Image>(); StartCoroutine(LoadImageCoroutine(imagePath, imageComponent)); } } } private IEnumerator LoadImageCoroutine(string imagePath, Image imageComponent) { var request = UnityEngine.Networking.UnityWebRequestTexture.GetTexture("file://" + imagePath); yield return request.SendWebRequest(); if (request.result != UnityEngine.Networking.UnityWebRequest.Result.Success) { Debug.LogError("Failed to load image: " + request.error); yield break; } Texture2D texture = ((UnityEngine.Networking.DownloadHandlerTexture)request.downloadHandler).texture; Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f); imageComponent.sprite = sprite; } public void GenerateImageList() { ClearImageList(); // 清空之前的图片列表 string streamingPath = Path.Combine(Application.streamingAssetsPath, imageFolderPath); if (Directory.Exists(streamingPath)) { string[] imageFiles = Directory.GetFiles(streamingPath); foreach (string imagePath in imageFiles) { // 加载图片预制体 GameObject imageGO = Instantiate(imagePrefab, content); // 获取图片名称 string imageName = Path.GetFileNameWithoutExtension(imagePath); // 设置图片预制体的名称 imageGO.name = imageName; // 设置图片显示 Image imageComponent = imageGO.GetComponent<Image>(); StartCoroutine(LoadImageCoroutine(imagePath, imageComponent)); } } } public void ClearImageList() { foreach (Transform child in content) { Destroy(child.gameObject); } } } ``` 这段代码假设你已经在场景中创建了一个空对象并将`ImageListLoader`脚本附加到该对象上。你还需要创建一个图片预制体,并将其分配给`imagePrefab`变量。`content`变量引用了列表的内容容器,你可以在Unity编辑器中将容器对象指定为变量。 在`Start`方法中,我们调用`LoadImages`方法来加载图片。`LoadImages`方法首先构造了`StreamingAssets`文件夹图片的路径,并检查路径是否存在。然后,我们使用`Directory.GetFiles`方法获取图片文件夹中的所有文件路径,并遍历这些文件路径。 在循环中,我们实例化一个图片预制体,并设置预制体的名称为图片名称(去除扩展名)。然后,我们获取预制体上的`Image`组件,并使用协程`LoadImageCoroutine`加载图片并设置到`Image`组件上。 在`LoadImageCoroutine`方法中,我们使用`UnityWebRequestTexture.GetTexture`方法加载图片。通过协程等待加载完成后,我们检查是否成功加载。如果成功加载,我们创建一个`Sprite`并设置到传的`Image`组件上。 在代码的最后,我们添加了两个公共方法`GenerateImageList`和`ClearImageList`。`GenerateImageList`方法用于生成图片列表,会先调用`ClearImageList`方法清空之前的图片列表,然后再重新加载新的图片列表。 请确保你已经正确设置了场景和图片资源,并根据需要修改代码中的路径和加载的资源类型。同时,需要在列表的内容容器中创建一个空对象作为父级,并将其指定给脚本中的`content`变量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菌菌巧乐兹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值