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的更多相关文章

  1. (转载)iOS UILabel自定义行间距时获取高度

    本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...

  2. Unity 模型导入导出

    从3DMAX导出,参考: http://tieba.baidu.com/p/2807225555 -> 使用3dmax 2013,会自带导出 fbx 的功能 -> 从 3dmax 导出 - ...

  3. Unity模型导入导出

    从3DMAX导出,参考: http://tieba.baidu.com/p/2807225555 -> 使用3dmax 2013,会自带导出 fbx 的功能 -> 从 3dmax 导出 - ...

  4. iOS UILabel自定义行间距时获取高度

    本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...

  5. Unity 自定义"=="操作符 [翻译来源blogs.unity3d,2014/05]

    主要内容来源 https://blogs.unity3d.com/cn/2014/05/16/custom-operator-should-we-keep-it/ 在我们代码里,如果有这样的代码: i ...

  6. (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译

    Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...

  7. Python包的相对导入时出现错误的解决方法

    在练习Python中package的相对导入时,即 from . import XXX 或者 from .. import XXX 时会遇到这样两个错误: SystemError: Parent mo ...

  8. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究

      关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...

  9. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程02:关键帧动画导入与切割》

    2. 关键帧动画导入与切割 动画的分割与导入概述: 在游戏当中,游戏角色在不同状态下会有不同的动作,这些动作在引擎里相当于一段段的动画片段.当导入模型资源的时候,连同模型动画都会一并导入到引擎中.开发 ...

随机推荐

  1. C++——volatile关键字的学习

    首先声明一点,本文是关于volatile关键字的学习,学习内容主要是来自一些大牛的网络博客. 一篇是何登成先生的C/C++ Volatile关键词深度剖析(http://hedengcheng.com ...

  2. Zabbix server的配置文件没有真正生效

    现状 Zabbix server端迁移之后,一直卡,卡的不行的卡,一直以为是网络的原因,实在忍受不下去了,还是得排查排查   在界面上一直显示这条,但是我的zabbix_server确实是启动的,但是 ...

  3. Spring boot配置logback

    在application-dev.yml增加配置 配置文件的路径为 rescoures/evn/dev/logback-spring.xml <?xml version="1.0&qu ...

  4. php正则表达式提取img alt/title标签并替换

    有时我们需要对富文本编辑器中的img标签进行必要的处理以满足网站自身的需要,比如:根据站点关键词对页面内img的alt标签设定关键词,以下为提取并替换alt/title标签内容的正则: $title ...

  5. FM与PM信号的表现形式

    角度调制可以写成如下形式: $u(t)=A_c cos(2\pi f_c t + \phi (t) )$ $A_c cos(2\pi f_c t)$是载波,调制信号控制$\phi (t)$. 对于PM ...

  6. ASP.NET CORE 3 安装遇到的问题

    最近在研究 ASP.NET CORE, visualstudio2019 也已正式发布,本以为安装vs2019后就默认支持asp.net core 3,谁知是不支持的,需单独安装net core 3及 ...

  7. OPC客户端开发问题总结

    环境准备 采用MatrikonOPC做模拟服务器,注册 OPCDAAuto.dll组件 引用 Interop.OPCAutomation.dll组件.开始开发. 1..new OPCServer()- ...

  8. cookie和session的讲解

    php和js都是脚本语言: 客户端与服务器之间的交互,都是传输协议来进行交互的,客户向服务器发送的数据叫请求 request 服务器向客户端传输数据叫响应 response 他们之间都是无状态的: 无 ...

  9. Scrapy实战篇(八)之爬取教育部高校名单抓取和分析

    本节我们以网址https://daxue.eol.cn/mingdan.shtml为初始链接,爬取教育部公布的正规高校名单. 思路: 1.首先以上面的地址开始链接,抓取到下面省份对应的链接. 2.在解 ...

  10. winrar+目录穿透复现

    前言: 学习下该漏洞,记录下这是自动化复现,没有具体分析.菜逼只会用. 00x1: 漏洞简单描述: 该漏洞事一个由UNACEV2.dll代码库中的一个深藏已久的漏洞 当攻击者制作一个恶意的ACE文件时 ...