Unity 自定义导入时切割Sprite
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using UnityEditor.Experimental.AssetImporters;
using UnityEditor.Experimental.U2D;
public class SpriteSheetPostprocessor:AssetPostprocessor{
private void OnPreprocessAsset(){
}
private void OnPreprocessTexture(){
string dataPath=Application.dataPath;
dataPath=dataPath.Substring(0,dataPath.LastIndexOf("/")+1);
int dotIndex=assetPath.LastIndexOf('.');
string xmlPath=assetPath.Substring(0,dotIndex)+".xml";
xmlPath=dataPath+xmlPath;
if(File.Exists(xmlPath)){
OnSpriteSheetProcess(xmlPath);
/*var texture=AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
if(texture){
Debug.Log("texture.height:"+texture.height);
OnSpriteSheetProcess(texture.height,xmlPath);
}else{
Debug.Log("null");
AssetDatabase.ImportAsset(assetPath);
}*/
}
}
private void OnPostprocessTexture(Texture2D texture){
}
private void OnSpriteSheetProcess(string xmlPath){
var doc=new XmlDocument();
doc.Load(xmlPath);
var nodes=doc.DocumentElement.SelectNodes("SubTexture");
var spritesheet=new SpriteMetaData[nodes.Count];
float textureHeight=getTextureHeightWithXmlNodes(nodes);
Vector2 pivot=new Vector2();
for(int i=0;i<nodes.Count;i++){
XmlElement ele=nodes[i] as XmlElement;
if(i==0){
pivot.x=float.Parse(ele.GetAttribute("pivotX"));
pivot.y=float.Parse(ele.GetAttribute("pivotY"));
}
string name=ele.GetAttribute("name");
float x=float.Parse(ele.GetAttribute("x"));
float y=float.Parse(ele.GetAttribute("y"));
float width=float.Parse(ele.GetAttribute("width"));
float height=float.Parse(ele.GetAttribute("height"));
float frameX=float.Parse(ele.GetAttribute("frameX"));
float frameY=float.Parse(ele.GetAttribute("frameY"));
float frameWidth=float.Parse(ele.GetAttribute("frameWidth"));
float frameHeight=float.Parse(ele.GetAttribute("frameHeight"));
float poX=(frameWidth/2.0f+frameX)/width;
float poY=((frameHeight-pivot.y)-(frameHeight-height+frameY));
poY/=height;
var spriteMetaData=new SpriteMetaData();
spriteMetaData.name=name;
spriteMetaData.alignment=(int)SpriteAlignment.Custom;
spriteMetaData.pivot=new Vector2(poX,poY);
spriteMetaData.rect=new Rect(x,-y+textureHeight-height,width,height);
spritesheet[i]=spriteMetaData;
//
}
var importer=assetImporter as TextureImporter;
importer.spriteImportMode=SpriteImportMode.Multiple;
importer.spritesheet=spritesheet;
}
private float getTextureHeightWithXmlNodes(XmlNodeList nodes){
float result=0;
float maxX=0;
float maxY=0;
for(int i=0;i<nodes.Count;i++){
XmlElement ele=nodes[i] as XmlElement;
float x=float.Parse(ele.GetAttribute("x"));
float y=float.Parse(ele.GetAttribute("y"));
float width=float.Parse(ele.GetAttribute("width"));
float height=float.Parse(ele.GetAttribute("height"));
float x1=x+width;
float y1=y+height;
if(x1>maxX)maxX=x1;
if(y1>maxY)maxY=y1;
}
float max=Mathf.Max(maxX,maxY);
//flash sprite sheet中,
//以竖向排列优先[占位高度大于某个(2的次方值)则马上放大图表高度缩小宽度进行排列),
//所以如果占位宽大于占位高图表的高度一定是大于占位宽的(2的次方值)]
int pow=5;//5~13
while(true){
float val=1<<pow;
pow++;
if(val>max){
result=val;
break;
}else if(pow>=13){
result=val;
break;
}
}
Debug2.Log("textureHeight:"+result,"maxX:"+maxX,"maxY:"+maxY);
return result;
}
/*private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths){
foreach (string str in importedAssets){
if(str.EndsWith(".png")||str.EndsWith(".PNG")){
OnSpriteSheetPostprocess(str);
}
}
}
private static void OnSpriteSheetPostprocess(string path){
string dataPath=Application.dataPath;
dataPath=dataPath.Substring(0,dataPath.LastIndexOf("/")+1);
int dotIndex=path.LastIndexOf('.');
string xmlPath=path.Substring(0,dotIndex)+".xml";
xmlPath=dataPath+xmlPath;
if(File.Exists(xmlPath)){
var doc=new XmlDocument();
doc.Load(xmlPath);
XmlElement firstEle=doc.DocumentElement.FirstChild as XmlElement;
var nodes=doc.DocumentElement.SelectNodes("SubTexture");
for(int i=0;i<nodes.Count;i++){
XmlElement ele=nodes[i] as XmlElement;
string name=ele.GetAttribute("name");
}
}
//
//parseAndExportXml(path);
Debug.Log("OnSpriteSheetPostprocess");
//var settings=new TextureGenerationSettings();
//var spriteImportData=new SpriteImportData();
//spriteImportData.rect=new Rect(0,0,50,50);
//settings.spriteImportData=new SpriteImportData[]{spriteImportData};
//TextureGenerator.GenerateTexture(settings,colorBuffer);
}*/
}
Unity 自定义导入时切割Sprite的更多相关文章
- (转载)iOS UILabel自定义行间距时获取高度
本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...
- Unity 模型导入导出
从3DMAX导出,参考: http://tieba.baidu.com/p/2807225555 -> 使用3dmax 2013,会自带导出 fbx 的功能 -> 从 3dmax 导出 - ...
- Unity模型导入导出
从3DMAX导出,参考: http://tieba.baidu.com/p/2807225555 -> 使用3dmax 2013,会自带导出 fbx 的功能 -> 从 3dmax 导出 - ...
- iOS UILabel自定义行间距时获取高度
本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...
- Unity 自定义"=="操作符 [翻译来源blogs.unity3d,2014/05]
主要内容来源 https://blogs.unity3d.com/cn/2014/05/16/custom-operator-should-we-keep-it/ 在我们代码里,如果有这样的代码: i ...
- (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译
Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...
- Python包的相对导入时出现错误的解决方法
在练习Python中package的相对导入时,即 from . import XXX 或者 from .. import XXX 时会遇到这样两个错误: SystemError: Parent mo ...
- 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...
- 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程02:关键帧动画导入与切割》
2. 关键帧动画导入与切割 动画的分割与导入概述: 在游戏当中,游戏角色在不同状态下会有不同的动作,这些动作在引擎里相当于一段段的动画片段.当导入模型资源的时候,连同模型动画都会一并导入到引擎中.开发 ...
随机推荐
- day02格式化输出等
1.格式化输出 format % 占位符 %s:str, %d: dight, %f: float 字符串多行用三个单引号或三个双引号 %%5,百分之五,转义字符%.想 ...
- 运维wiki
意识 1.责任心 要有 owner 意识.运维是线上产品的首要负责人,出现故障都默认是运维的故障,要推动改进. 2.细心 要有敏感的风险意识,稳定和安全是运维的最高责任 3.上进心 要善于学习,不断反 ...
- 剑指offer 10.递归和循环 矩形覆盖
题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 当n=0时 ,target=0: 当n=1时 ,ta ...
- C#中使用EntityFramework(EF)生成实体进行存储过程的调用
在EF中使用定义对象模型的方式调用一个存储过程,这个存储过程返回的是一组包含两列的值.(ProjectName,Count) 下面是存储过程: CREATE procedure [dbo].[Pro_ ...
- rocketmq 4.3.2 解决远程不能消费问题,解决未识别到公网IP问题
1.解决远程不能消费问题 问题描述: nameserver和broker启动后,用tools(命令如下)能发送消息和消费消息,在局域网服务器能发送消息,消费启动后收不到消息通知问题 sh tools. ...
- 【Oracle】group by 和partition by的区别
总结: group 单纯分组 partition 也能分组,但还具备累计的功能 order by 排序,与计算函数联用,需要累加计算 0.select * from test; ---测试数据 ...
- Robot Framework脚本在jenkins执行完之后无法查看日志
首先确保Robot Framework Plugin已经安装好,构建结束后test result和log.html等日志文件已显示,只是无法打开查看: 修改tomcat配置:vi tomcat/con ...
- python3学习笔记12(变量作用域)
变量作用域 参考http://www.runoob.com/python3/python3-function.html Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量 ...
- 正则求解@" (?<=^\[length=)(\d+)(?=\])"
举个例子 [length=1548]这个正则 就是匹配 length的值了(1548)(?<=exp)匹配之后的(?=exp)匹配表达式之前的^是边界,在行首例如 aa[length=1548] ...
- 前端-JavaScript1-8——JavaScript之作业练习
1.表示气温有两种方法:摄氏度和华氏度.两者的关系是: 编写程序,让用户输入摄氏度,弹出对应的华氏温度. //第一步,让用户输入数字 var sheshidu = parseFloat(prompt( ...