[Unity] Unity5.6 2D中导入图片顶点数目过多修改为四个顶点
来源:互联网 发布:ping 域名 ip不一样 编辑:程序博客网 时间:2024/04/29 14:23
Unity5.6增强了2D功能,为制作2D游戏提供了很大的便利,让我们可以很方便地根据图片生成多边形碰撞体。在Sprite Editor中增加了Edit Outline的功能,让我们可以编辑图片的outline,而且unity也会在我们导入图片的时候自动地给我们调整图片的outline。
如下图所示,这么一个简单的图片有三十多个顶点,这还是在Outline Tolerance设置为0的情况下。
让我们看一下游戏场景中的情况:
上图可见,这么一个sprite需要30个顶点,28个三角面片,而其实4个顶点,两个三角面片足以胜任此工作。如果一个2D游戏的场景中有大量这种图片的话,很容易就会产生几千个顶点,无缘无故消耗游戏性能。
为了解决这个问题,需要对图片进行处理。我从图片的TextureImporter中获取outline信息,根据texture的宽高进行修改。下面是代码,有对单个Texture进行修改的,也有在导入资源的时候自动处理的:
using UnityEngine;using UnityEditor;using System.Collections.Generic;// Author: 邵志恒 Rick// E-mail: zhiheng.rick@gmail.compublic class AjustTexutureOutline2FourVertex : AssetPostprocessor{ // 导入图片的时候调用 public void OnPostprocessTexture(Texture2D tx) { TextureImporter textureImporter = this.assetImporter as TextureImporter; SerializedObject textureImporterSO = new SerializedObject(textureImporter); SerializedProperty outlineSP = textureImporterSO.FindProperty("m_SpriteSheet.m_Outline"); ModifyOutline(outlineSP, tx.width, tx.height); textureImporterSO.ApplyModifiedProperties(); } [MenuItem("Assets/AdjustOutline")] private static void AdjustOutline() { Object targetObj = Selection.activeObject; if (targetObj && targetObj is Texture2D) { string path = AssetDatabase.GetAssetPath(targetObj); TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; Texture tx = AssetDatabase.LoadAssetAtPath<Texture>(path); SerializedObject textureImporterSO = new SerializedObject(textureImporter); SerializedProperty outlineSP = textureImporterSO.FindProperty("m_SpriteSheet.m_Outline"); ModifyOutline(outlineSP, tx.width, tx.height); textureImporterSO.ApplyModifiedProperties(); AssetDatabase.ImportAsset(path); } } [MenuItem("Assets/ProcessTexture", true)] private static bool Texture2DValidation() { return Selection.activeObject.GetType() == typeof(Texture2D); } private static void ModifyOutline(SerializedProperty outlineSP, float width, float height) { if (outlineSP.arraySize == 0) { outlineSP.InsertArrayElementAtIndex(0); SerializedProperty arrayElementAtIndex = outlineSP.GetArrayElementAtIndex(0); for (int i = 0; i < 4; i++) { arrayElementAtIndex.InsertArrayElementAtIndex(i); arrayElementAtIndex.GetArrayElementAtIndex(i).floatValue = 0.0f; } } for (int i = 0; i < outlineSP.arraySize; i++) { SerializedProperty arrayElementAtIndex = outlineSP.GetArrayElementAtIndex(i); for (int j = 0; j < arrayElementAtIndex.arraySize; j++) { float x, y; if (j == 0 || j == 1) { x = -width / 2; } else { x = width / 2; } if (j == 1 || j == 2) { y = -height / 2; } else { y = height / 2; } arrayElementAtIndex.GetArrayElementAtIndex(j).vector2Value = new Vector2(x, y); } } }}
阅读全文
0 0
- [Unity] Unity5.6 2D中导入图片顶点数目过多修改为四个顶点
- [Unity优化]减少顶点数目
- Unity中 gameObject的顶点捕捉
- Unity中使用Vertex Snapping(顶点捕捉)
- unity中顶点片段shader环境反射
- 修改OGRE中模型的顶点色
- 修改OGRE中模型的顶点色
- Unity MegaFiers 顶点动画
- Unity 获得顶点数据
- 直接提取四个顶点坐标
- 图片旋转--修改老外的代码,返回中增加第四个顶点
- 修改vertex顶点
- D3D中光照,顶点法线,顶点结构,顶点颜色
- Unity Shaders and Effects Cookbook (7-2) Surface Shader 中实现 顶点动画
- unity鼠标拾取网格顶点
- .net创建CAD多重引线,引线中顶点修改问题
- 计算OBB四个顶点的坐标
- 已知四边形四个顶点坐标,求面积
- Struts2_文件上传
- Android Framework AIDL的使用
- Java Reflection(反射机制)详解
- java_DateUtil_篇1
- 阿里巴巴集团技术委员会主席王坚:我曾经被诺贝尔奖得主司马贺忽悠_相信人工智能就是未来!
- [Unity] Unity5.6 2D中导入图片顶点数目过多修改为四个顶点
- java 关于使用java1.8的localDateTime日期操作的基本使用以及一些使用技巧 持续更新...
- android studio 同一应用多次安装并不覆盖方法
- exit()和析构函数的关系
- HBase基于Hadoop2.5完全分布式HA环境搭架
- json xml解析
- 5-13 日K蜡烛图 (15分)
- Storm中worker, executor,task之间的关系:
- [POJ1083]Moving Tables