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. 关键帧动画导入与切割 动画的分割与导入概述: 在游戏当中,游戏角色在不同状态下会有不同的动作,这些动作在引擎里相当于一段段的动画片段.当导入模型资源的时候,连同模型动画都会一并导入到引擎中.开发 ...
随机推荐
- 创建一个dynamics 365 CRM online plugin (十一) - Handling Configuration data
Config data 可以在registering step 的时候来配置 配置好的config data 可以使用 constructor 来获取 Secure Config 和 UnSecure ...
- dic and set
一.dic 1.格式:key:value 2.key值必须不可变(可hash) 3.key不可重复(唯一性) 4.优点:查找.插入速度快 5.缺点:空间消耗大 6.实质是以空间换速度 7.常用参数 1 ...
- install rust
Step 1. Trial 1 Download rustup-init.exe exec rustup-init.exe SW hangs 2. Trial 2 install rust-1.33. ...
- abstract class VS interface
关于抽象类 abstract class: 1. 抽象方法必须在抽象类中 2. 抽象类和抽象方法要用abstract 关键字修饰 3. 不可以用new 来实例化一个abstract类,因为调用抽象方法 ...
- go 语言之 生产者消费模型
简易的生产者消费模型,通过管道[也可以理解为队列],管道是先进先出,主要是理解chan 生产者使用make将chan初始化,并且设置chan长度,如果不设置,生产者就写入不了通道 go 是使用线程开始 ...
- 程序员装X指南
一.准备工作“工欲善其事必先利其器.” 1.电脑不一定要配置高,但是双屏是必须的,越大越好,能一个横屏一个竖屏更好.一个用来查资料,一个用来写代码 .总之要显得信息量很大,效率很高. 2.椅子不一 ...
- Ubuntu16.04下安装elasticsearch+kibana实现php客户端的中文分词
1.下载安装java, elasticsearch和kibana apt-get install default-jre default-jdk wget https://artifacts.elas ...
- 【转】redis实现的分布式锁
参考: 1. https://www.bbsmax.com/A/WpdKpM1zVQ/ 2.https://www.oschina.net/translate/redis-distlock
- 2018-2019-2 20175227张雪莹《Java程序设计》实验三 《敏捷开发与XP实践》
2018-2019-2 20175227张雪莹<Java程序设计> 实验三 <敏捷开发与XP实践> 实验报告封面 课程:Java程序设计 班级:1752班 姓名:张雪莹 学号: ...
- 一个故事带你理解if __name__ == '__main__'
如果你刚刚接触python,相信会在看别人的程序的时候会遇到if __name__ == '__main__'酱紫的语法,如果当时没看懂现在也一知半解的话,看下去,本文可以帮你解决这个问题. 大家都知 ...