public List <GameObject> UpdateOutlines() { List <List <Vector3> > linesPoints = SpriteBorder.Generate(gameObject, lineOffset); List <GameObject> createdLines = new List <GameObject>(); int lineIdx = 0; int linesBeforeCreating = linesToUpdate.Count; foreach (var linePoints in linesPoints) { foreach (var part in lineParts) { LineRenderer line; if (lineIdx < linesBeforeCreating) { line = linesToUpdate[lineIdx]; lineIdx++; } else { if (part.overrideGameObject != null) { line = Instantiate(prefabWithLineRenderer, transform.position, transform.rotation, transform).GetComponent <LineRenderer>(); } else { line = Instantiate(prefabWithLineRenderer, transform.position, transform.rotation, transform).GetComponent <LineRenderer>(); } linesToUpdate.Add(line); } createdLines.Add(line.gameObject); int startToRemove = (int)(linePoints.Count * part.LineStartNormalized); int endtoRemoveStartId = (int)(linePoints.Count * (part.LineEndNormalized)); int endToRemove = linePoints.Count - endtoRemoveStartId; List <Vector3> lineCut = new List <Vector3>(linePoints); if (part.LineStartNormalized != 0) { lineCut.RemoveRange(0, startToRemove); } if (part.LineEndNormalized != 1) { lineCut.RemoveRange(endtoRemoveStartId - startToRemove, endToRemove); } line.positionCount = lineCut.Count; line.SetPositions(lineCut.ToArray()); } } return(createdLines); }
void GenerateShapesBasedOnMesh() { var selected = Selection.gameObjects; foreach (var gameObject in selected) { string path = AssetDatabase.GetAssetPath(gameObject); SVGImporter importer = AssetImporter.GetAtPath(path) as SVGImporter; if (importer != null) { var sprite = (Sprite)importer.targetObject; var outlines = SpriteBorder.Generate(gameObject, Vector3.zero); Rect rect = sprite.rect; Bounds bounds = sprite.bounds; List <Vector2[]> newOutlines = new List <Vector2[]>(outlines.Count); foreach (var outline in outlines) { List <Vector2> converterOutline = new List <Vector2>(outline.Count); foreach (var v in outline) { converterOutline.Add(new Vector2(v.x * (rect.width / bounds.size.x), v.y * (rect.height / bounds.size.y))); } newOutlines.Add(converterOutline.ToArray()); } OverwriteOutline(sprite, 0, newOutlines); Debug.Log(outlines.Count + " -> outlines.Count"); //Vector2 outlines //Selection.activeObject = sprite; //spriteWindow = Type.GetType("UnityEditor.SpriteEditorWindow, UnityEditor", true); //var getWindowMethod = spriteWindow.GetMethod("GetWindow", BindingFlags.Public | BindingFlags.Static); //getWindowMethod.Invoke(null, null); ////if (spriteWindowInstance == null) //spriteWindowInstance = spriteWindow.GetField("s_Instance").GetValue(null); //SpriteWindowMethod("OnSelectionChange"); ////string selecionPath = SpriteWindowMethod("GetSelectionAssetPath") as string; ////Choose custom outline mode //SpriteWindowMethod("SetupModule", (object)1); //Type spritePhysicsShapeModule = Type.GetType("UnityEditor.U2D.SpritePhysicsShapeModule, UnityEditor", true); //object outlineWindowInstance = SpriteWindowField("m_CurrentModule"); //Type spriteOutlineModule = spritePhysicsShapeModule.BaseType; ////Method(Type.GetType("UnityEditor.Experimental.U2D.SpriteEditorModuleBase, UnityEditor", true), outlineWindowInstance, "OnModuleActivate", null); //object m_Outline = Field(spriteOutlineModule, outlineWindowInstance, "m_Outline"); //Type spriteOutlineModel = Type.GetType("UnityEditor.U2D.SpriteOutlineModel, UnityEditor", true); //object m_SpriteOutlineList = spriteOutlineModel.InvokeMember("", BindingFlags.GetProperty, null, m_Outline, new object[] { 0 }); //Type SpriteOutlineList = Type.GetType("UnityEditor.U2D.SpriteOutlineList, UnityEditor", true); //SpriteOutlineList.GetField("m_TessellationDetail", bf).SetValue(m_SpriteOutlineList, 0.3f); ////3. this.SetupShapeEditorOutline(this.m_Selected); //SpriteRect m_Selected = spriteOutlineModule.GetField("m_Selected", bf).GetValue(outlineWindowInstance) as SpriteRect; //Method(spriteOutlineModule, outlineWindowInstance, "SetupShapeEditorOutline", m_Selected); //List<Vector2[]> outlines = new List<Vector2[]>((List<Vector2[]>)Method(SpriteOutlineList, m_SpriteOutlineList, "ToListVector")); //float tessellation = (float)SpriteOutlineList.GetField("m_TessellationDetail", bf).GetValue(m_SpriteOutlineList); //Method(Type.GetType("UnityEditor.Experimental.U2D.SpriteEditorModuleBase, UnityEditor", true), outlineWindowInstance, "ApplyRevert", true); ////Method(Type.GetType("UnityEditor.Experimental.U2D.SpriteEditorModuleBase, UnityEditor", true), outlineWindowInstance, "OnModuleDeactivate", null); //object ISpriteEditor = spritePhysicsShapeModule.GetProperty("spriteEditorWindow", bf).GetValue(outlineWindowInstance); //Method(Type.GetType("UnityEditor.Experimental.U2D.ISpriteEditor, UnityEditor", true), ISpriteEditor, "ApplyOrRevertModification", true); ////Method(Type.GetType("UnityEditor.Experimental.U2D.ISpriteEditor, UnityEditor", true), ISpriteEditor, "SetDataModified"); ////spriteOutlineModule.GetField("shapeEditorDirty").SetValue(outlineWindowInstance, true); //Debug.Log(outlines[0].Length + " count"); //AssetDatabase.Refresh(); //EditorUtility.SetDirty(sprite); //AssetDatabase.SaveAssets(); //OverwriteOutline(sprite, tessellation, outlines); } } }