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. 获取spring容器对象方法和原因

    为什么要获取Spring容器对象:拿到spring容器对象后,你就可以用spring管理的bean了,拿到bean,自然可以使用bean的方法,场景:比如jsp页面.通过注解是无法注入bean的,在开 ...

  2. 自己写的C#三层代码生成器

    思来想去用T4生成代码要学习它的语法,C#本身能很简单地生成txt文件,为啥不直接批量替换模板方式自己写个的三层代码生成器.说干就干,2个小时搞定.当然各层还可以做的更精细,比如DAL层的Add方法I ...

  3. 注册Docker Hub、以及Push(九)

      一.注册   1.使用浏览器打开官网的时候,发现注册按钮点不了       2.下载google访问助手,添加到浏览器         下载地址:http://www.ggfwzs.com/,根据 ...

  4. Btrace 拦截构造函数,同名函数

    拦截方法: 1.普通方法  @OnMethod(clazz="", method="") 2.构造函数@OnMethod(claszz="" ...

  5. 备份原有yum源,设置为自建yum源的脚本

    #脚本开始 #!/bin/bash cd /etc/yum.repos.d/ tar -zcvf yum.bak.tar.gz CentOS-* rm -rf CentOS* touch /etc/y ...

  6. MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed

    MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed 1.安装完MySQL设置root密码报错如下 [root@vm ...

  7. php+redis实现消息队列

    参考:http://www.cnblogs.com/lisqiong/p/6039460.html 参考:http://blog.csdn.net/shaobingj126/article/detai ...

  8. python3-基础2

    数据类型:数字 .字符.列表.字典.集合 字符串: 要用引号引起来  单引号 双引号  三引号 字符串只能存一个值,没有单独字符一说 取字符串值  print(name[0])    中括号表示 常用 ...

  9. selenium 添加动态隧道代理

    # 无须密码验证方法 chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument('--proxy-server=http ...

  10. [转][SqlServer]收缩日志

    USE StudyDB ; GO ALTER DATABASE StudyDB SET RECOVERY SIMPLE;--设置简单恢复模式 GO ); GO ALTER DATABASE Study ...