当前位置:网站首页>unity 代码拆分图集
unity 代码拆分图集
2022-08-02 03:34:00 【落水无痕】
1、 将分割代码放入Editor中
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using System.IO;
public class Tools : EditorWindow
{
[MenuItem("Tools/openWindow")]
public static void createWindow()
{
Tools window = EditorWindow.GetWindow<Tools>("设置位置");
window.Show();
window.minSize = new Vector2(200, 300);
}
private void OnGUI()
{
if (GUILayout.Button("分割图集"))
{
ProcessToSprite();
}
}
static SpriteMetaData[] spriteArr;
static void getSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称
string path = rootPath + "/" + image.name + ".png";//图片路径名称
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口
spriteArr = texImp.spritesheet;
for (int i = 0; i < spriteArr.Length; i++)
{
MonoBehaviour.print(spriteArr[i].rect);
}
}
static void setSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称
string path = rootPath + "/" + image.name + ".png";//图片路径名称
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口
texImp.spritesheet = spriteArr;
}
[MenuItem("Tools/ProcessToSprite #&C")]
static void ProcessToSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称
string path = rootPath + "/" + image.name + ".png";//图片路径名称
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口
AssetDatabase.CreateFolder(rootPath, image.name);//创建文件夹
MonoBehaviour.print(path);
// texImp.spritesheet =
foreach (SpriteMetaData metaData in texImp.spritesheet)//遍历小图集
{
Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);
for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)//Y轴像素
{
for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)
myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, image.GetPixel(x, y));
}
if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24)
{
Texture2D newTexture = new Texture2D(myimage.width, myimage.height);
newTexture.SetPixels(myimage.GetPixels(0), 0);
myimage = newTexture;
}
var pngData = myimage.EncodeToPNG();
File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".png", pngData);
// 刷新资源窗口界面
AssetDatabase.Refresh();
}
}
}
2、选择图集,勾选模式及可编辑

3、点击Sprite Editor ,选择图片裁剪模式,对图片进行裁剪,Apply保存设置


4、选中图集图片,快捷键shift +alt +c 会在图集当前目录下生成对应的散图,


边栏推荐
- 进程(下):进程控制、终止、等待、替换
- 【LeetCode】合并
- 408-Binary tree-preorder inorder postorder level traversal
- AD8307对数检波器
- Kinematics Analysis of Robot Arm
- UKlog.dat和QQ,微信文件的转移
- TeamCode 产品 UI 全新升级,快来体验吧
- install 命令
- 与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
- The use and simulation of vector implementation:
猜你喜欢
随机推荐
openwrt RK3568_EVB移植
判断子序列 —— LeetCode-392
Host your own website with Vercel
开源代码交叉编译操作流程及遇到的问题解决(lightdm)
增量编译技术在Lightly中的实践
How to remotely debug PLC?
使用飞凌嵌入式IMX6UL-C1板子——qt+opencv环境搭建
【LeetCode】设计链表
【TCS3200 color sensor and Arduino realize color recognition】
【LeetCode】链表相加 进位
“520” 如何正确地用代码向 ta 表白?
NSIS来自己设定快捷方式的图标
LL(1)文法 :解决 if-else/if-else 产生式二义性问题
LT8918L LVDS转MIPI芯片技术支持资料
Introduction and mock implementation of list:list
关于IIC SDA毛刺的那些事
开源日志库 [log4c] 使用
DMA相应外设映射
【MQ-3 Alcohol Detector and Arduino Detect Alcohol】
【操作系统】线程安全保护机制









