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. Qt applendPlainText()/append() 多添加一个换行解决方法

    Qt applendPlainText()/append() 多添加一个换行解决方法 void ConsoleDialog::appendMessageToEditor(const QString & ...

  2. java-项目中无法访问js、css等静态资源

    解决方法:在mvc.xml配置文件中增加如下配置 如果增加<mvc:default-servlet-handler/> 后无法访问controller,需要增加<mvc:annota ...

  3. reset Cisco 2960 password

    详见:http://www.cahilig.net/2014/04/14/how-reset-cisco-2960-switch-password-without-losing-your-config ...

  4. Ntrip协议简介(转)

    原文地址:https://blog.csdn.net/sinat_19447667/article/details/67637167 1 什么是Ntrip? CORS(Continuously Ope ...

  5. Python2.X和Python3.X的w7同时安装使用

    一.介绍 Python([ˈpaɪθən])是一种面向对象.解释型计算机程序设计语言.Python语法简洁.清晰,具有丰富和强大的类库. Python源代码遵循GPL(GNU General Publ ...

  6. DSP 运行时间计算函数--_itoll(TSCH,TSCL);

    DSP OMAP 程序耗时测定 CPU周期 两种方法 利用TSCL和TSCH来计算时钟周期,这两天看了一下如何他们 DSP开发,测量某个函数或某段代码的cycles消耗是经常要做的 事情,常用的pro ...

  7. 内存问题-JVM调优思路

    通常,内存的问题就是 GC 的问题,因为 Java 的内存由 GC 管理.有2种情况,一种是内存溢出了,一种是内存没有溢出,但 GC 不健康. 内存溢出的情况可以通过加上 -XX:+HeapDumpO ...

  8. keepalived+nginx实现niginx高可用,宕机自动重启

    nginx作为http服务器,在集群中 用于接受客户单发送过来的请求,并且根据配置的策略将请求 转发给具体的哪台服务器 如果在nginx服务器使用轮询策略处理客户端的请求,出现了tomcat 宕机的情 ...

  9. MySQL数据库事务各隔离级别加锁情况--read committed && MVCC

    之前已经转载过几篇相关的文章,此次基于mysql 5.7 版本,从测试和源码角度解释一下RR,RC级别为什么看到的数据不一样 先补充一下基础知识 基本知识 假设对于多版本(MVCC)的基础知识,有所了 ...

  10. 基于fastadmin快速搭建后台管理

    FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架:开发文档 下面对环境搭建简要概述,希望后来者能少走弯路: 1. 百度搜索最新版wampserver, 安装并启动 ...