Setup sprite Custom Physics Shape during asset import from script?'
[Unity][2D]Sprite精灵多个多边形切割
对于Sprite Mode的属性解释:
Single:这种模式下的一个image就是一个Sprite Multiple:这种模式下可以对一个图片进行裁剪,分割成多个Sprite在Multiple中的slice有两个选项也就是选择分割模式。 Automatioc:根据图片大小unity系统自动裁剪,将一个image分割成多个Sprite Grid By Cell Size和Grid By Cell Count都是网格分割都是属于自定义分割大小 Grid By Cell Size是根据元素大小进行网格分割 Grid By Cell Count是根据元素数量进行网格分割 Polygon:在这种模式下,一般用于更改图片显示的形状,通过设置图片显示的图形顶点数,来改变图片显示的形状
自动设置为Polygon
var importer = TextureImporter.GetAtPath(p) as TextureImporter; importer.spriteImportMode = SpriteImportMode.Polygon;
设置Custome OutLine 点参考 官方论坛帖子,但是不太好用。
【Unity开发】图片加载时 设置Sprite Custom OutLine
var windowType = typeof(UnityEditor.U2D.Sprites.SpriteEditorModuleBase).Assembly.GetType("UnityEditor.U2D.Sprites.SpriteEditorWindow"); var window= EditorWindow.GetWindow(type); window.Show(); //Need to delay at least 1 frame so that the sprite editor menu appears SetupPhysicsShape(windowType, window); window.Close(); private static void SetupPhysicsShape(Type windowType, EditorWindow window){ var tolerance = 1f; var method = windowType.GetMethod("SetupModule", BindingFlags.Instance | BindingFlags.NonPublic); method.Invoke(window, new Object[] { 2 }); var module = windowType.GetField("m_CurrentModule", BindingFlags.Instance | BindingFlags.NonPublic); var moduleInstance = module.GetValue(window); var moduleType = moduleInstance.GetType(); var outlineInstance = moduleType.GetField("m_Outline", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(moduleInstance); var outlineType = outlineInstance.GetType(); var list = outlineType.GetField("m_SpriteOutlineList", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(outlineInstance) as IList; var spriteOutline = list[0]; var spriteOutlineType = spriteOutline.GetType(); var tess = spriteOutlineType.GetField("m_TessellationDetail", BindingFlags.Instance | BindingFlags.NonPublic); tess.SetValue(spriteOutline, tolerance); moduleType = moduleType.BaseType; var mets = moduleType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); var recordUndo = mets.First(m => m.Name == "RecordUndo"); recordUndo.Invoke(moduleInstance, null); var shapeOutlineList = moduleType.GetProperty("selectedShapeOutline", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(moduleInstance) as IList; shapeOutlineList.Clear(); var selectedProp = moduleType.GetField("m_Selected", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(moduleInstance); moduleType.GetMethod("SetupShapeEditorOutline", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(moduleInstance, new[] { selectedProp }); moduleType.GetProperty("shapeEditorDirty", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(moduleInstance, true); windowType.GetMethod("DoApply", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(window, null); }
下面是详细思路,
获取SpriteEditorWindow 句柄
根据里面的函数利用反射思路获取函数句柄并调用
保存图片数据
订单问题联系378074730@qq.com