private void ConvertToSprite(Shape shape) { string dname = "Assets/Resources/Shapes2D Sprites"; string fname = dname + "/" + shape.name + ".png"; string rname = "Shapes2D Sprites/" + shape.name; if (!System.IO.Directory.Exists(dname)) { System.IO.Directory.CreateDirectory(dname); } if (System.IO.File.Exists(fname) && !EditorUtility.DisplayDialog("Overwrite File?", "A file with the name " + fname + " already exists. " + "Are you sure you want to overwrite it?", "Yes", "Cancel")) { return; } float pixelsPerUnit = 100; Shapes2DPrefs prefs = GetPreferences(); if (prefs) { pixelsPerUnit = prefs.pixelsPerUnit; } else { Debug.LogWarning("Can't find Shapes2D Preferences in Shapes2D/Preferences. Please re-import Shapes2D."); } Vector2 pivot = RenderToTexture2D(fname, shape, pixelsPerUnit: pixelsPerUnit); // refresh the asset AssetDatabase.ImportAsset(fname); // set the sprite's pivot point so any rotations/position stay the same TextureImporter textureImporter = AssetImporter.GetAtPath(fname) as TextureImporter; TextureImporterSettings texSettings = new TextureImporterSettings(); textureImporter.ReadTextureSettings(texSettings); #if UNITY_5_5_OR_NEWER texSettings.ApplyTextureType(TextureImporterType.Sprite); #else texSettings.ApplyTextureType(TextureImporterType.Sprite, true); #endif texSettings.spritePixelsPerUnit = pixelsPerUnit; if (Vector2.Distance(pivot, new Vector2(0.5f, 0.5f)) < 0.01f) { texSettings.spriteAlignment = (int)SpriteAlignment.Center; textureImporter.SetTextureSettings(texSettings); } else { texSettings.spriteAlignment = (int)SpriteAlignment.Custom; textureImporter.SetTextureSettings(texSettings); textureImporter.spritePivot = pivot; } AssetDatabase.ImportAsset(fname, ImportAssetOptions.ForceUpdate); Sprite sprite = Resources.Load <Sprite>(rname); Undo.RecordObjects(shape.GetUndoObjects().ToArray(), "Convert to Sprite"); shape.SetAsSprite(sprite, GetDefaultSpriteMaterial()); // exit the gui routine because otherwise we get annoying errors because // we deleted a material and unity still wants to draw it EditorGUIUtility.ExitGUI(); }
public override void OnInspectorGUI() { serializedObject.Update(); // dunno what this does but it's in the examples? Shape shape = (Shape)serializedObject.targetObject; EditorGUI.BeginDisabledGroup(!shape.enabled); // shape type EditorGUILayout.PropertyField(shapeTypeProp); ShapeType shapeType = (ShapeType)shapeTypeProp.enumValueIndex; if (shapeType == ShapeType.Rectangle) { // rectangle props EditorGUILayout.PropertyField(roundnessPerCornerProp); if (shape.settings.roundnessPerCorner) { EditorGUILayout.PropertyField(roundnessTLProp); EditorGUILayout.PropertyField(roundnessTRProp); EditorGUILayout.PropertyField(roundnessBLProp); EditorGUILayout.PropertyField(roundnessBRProp); } else { EditorGUILayout.PropertyField(roundnessProp); roundnessTLProp.floatValue = roundnessProp.floatValue; roundnessTRProp.floatValue = roundnessProp.floatValue; roundnessBLProp.floatValue = roundnessProp.floatValue; roundnessBRProp.floatValue = roundnessProp.floatValue; } } else if (shapeType == ShapeType.Ellipse) { //ellipse props EditorGUILayout.PropertyField(startAngleProp); EditorGUILayout.PropertyField(endAngleProp); EditorGUILayout.PropertyField(invertArcProp); EditorGUILayout.PropertyField(innerCutoutProp); } else if (shapeType == ShapeType.Polygon) { // polygon props EditorGUILayout.PropertyField(polygonPresetProp); EditorGUI.BeginDisabledGroup(Selection.objects.Length != 1); if (GUILayout.Toggle(isEditingPolygon, "Edit Shape", "Button")) { if (!isEditingPolygon) { EditPolygon(); } } else { if (isEditingPolygon) { StopEditingPolygon(true); } } EditorGUI.EndDisabledGroup(); EditorGUILayout.PropertyField(usePolygonMapProp, new GUIContent("Optimize rendering (see docs!)")); } else if (shapeType == ShapeType.Triangle) { // triangle props EditorGUILayout.PropertyField(triangleOffsetProp); } // common props EditorGUILayout.PropertyField(blurProp); EditorGUILayout.PropertyField(outlineSizeProp); EditorGUILayout.PropertyField(outlineColorProp); // fill props EditorGUILayout.PropertyField(fillTypeProp); FillType fillType = (FillType)fillTypeProp.enumValueIndex; if (fillType == FillType.Gradient) { EditorGUILayout.PropertyField(gradientTypeProp); if ((GradientType)gradientTypeProp.enumValueIndex < GradientType.Radial) { EditorGUILayout.PropertyField(gradientAxisProp); } EditorGUILayout.PropertyField(gradientStartProp); } if (fillType >= FillType.SolidColor && fillType < FillType.Texture) { EditorGUILayout.PropertyField(fillColorProp); } if (fillType == FillType.Texture) { EditorGUILayout.PropertyField(fillTextureProp); EditorGUILayout.PropertyField(fillScaleProp); } if (fillType >= FillType.Gradient && fillType < FillType.Texture) { EditorGUILayout.PropertyField(fillColor2Prop); } if (fillType >= FillType.Gradient) { EditorGUILayout.PropertyField(fillOffsetProp); EditorGUILayout.PropertyField(fillRotationProp); } if (fillType == FillType.Grid || fillType == FillType.Stripes) { EditorGUILayout.PropertyField(lineSizeProp); } if (fillType == FillType.Grid || fillType == FillType.Stripes || fillType == FillType.CheckerBoard) { EditorGUILayout.PropertyField(gridSizeProp); } EditorGUI.BeginDisabledGroup(Selection.objects.Length != 1); if (GUILayout.Button("Convert to Sprite")) { ConvertToSprite(shape); } EditorGUI.EndDisabledGroup(); EditorGUI.EndDisabledGroup(); serializedObject.ApplyModifiedProperties(); // if the material has been destroyed, configure everything again. // or if the shape has been re-enabled after being converted to a sprite, // attempt to restore the scale it had previously if (shape.enabled && (!shape.IsConfigured() || shape.wasConverted)) { Undo.RecordObjects(shape.GetUndoObjects().ToArray(), "Re-enable Shapes2D Component"); shape.Configure(); if (shape.wasConverted) { shape.RestoreFromConversion(); } // combine with the re-enable action Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); } }
public override void OnInspectorGUI() { serializedObject.Update(); // dunno what this does but it's in the examples? Shape shape = (Shape)serializedObject.targetObject; EditorGUI.BeginDisabledGroup(!shape.enabled); // shape type EditorGUILayout.PropertyField(shapeTypeProp); ShapeType shapeType = (ShapeType)shapeTypeProp.enumValueIndex; if (shapeType == ShapeType.Rectangle) { // rectangle props EditorGUILayout.PropertyField(roundnessPerCornerProp); if (shape.settings.roundnessPerCorner) { EditorGUILayout.PropertyField(roundnessTLProp); EditorGUILayout.PropertyField(roundnessTRProp); EditorGUILayout.PropertyField(roundnessBLProp); EditorGUILayout.PropertyField(roundnessBRProp); } else { EditorGUILayout.PropertyField(roundnessProp); roundnessTLProp.floatValue = roundnessProp.floatValue; roundnessTRProp.floatValue = roundnessProp.floatValue; roundnessBLProp.floatValue = roundnessProp.floatValue; roundnessBRProp.floatValue = roundnessProp.floatValue; } } else if (shapeType == ShapeType.Ellipse) { //ellipse props EditorGUILayout.PropertyField(startAngleProp); EditorGUILayout.PropertyField(endAngleProp); EditorGUILayout.PropertyField(invertArcProp); EditorGUILayout.PropertyField(innerCutoutProp); } else if (shapeType == ShapeType.Polygon) { // polygon props EditorGUILayout.PropertyField(polygonPresetProp); EditorGUI.BeginDisabledGroup(Selection.objects.Length != 1); if (GUILayout.Toggle(isEditing, "Edit Shape", "Button")) { if (!isEditing) { EditShape(); } GUIStyle helpStyle = new GUIStyle(GUI.skin.label); helpStyle.wordWrap = true; helpStyle.normal.textColor = Color.green; EditorGUILayout.LabelField("Click on a segment to add a new node (up to 64).\nCtrl-click nodes to delete.\nSee docs about performance!", helpStyle); } else { if (isEditing) { StopEditingShape(true); } } if (shape.GetComponent <PolygonCollider2D>() && GUILayout.Button("From Polygon Collider 2D")) { FromPolygonCollider2D(shape); } if (GUILayout.Button("Set Polygon Collider 2D")) { SetPolygonCollider2D(shape); } EditorGUI.EndDisabledGroup(); EditorGUILayout.PropertyField(usePolygonMapProp, new GUIContent("Optimize rendering (see docs!)")); } else if (shapeType == ShapeType.Triangle) { // triangle props EditorGUILayout.PropertyField(triangleOffsetProp); } else if (shapeType == ShapeType.Path) { EditorGUILayout.PropertyField(pathThicknessProp); EditorGUILayout.PropertyField(fillPathLoopsProp); EditorGUI.BeginDisabledGroup(Selection.objects.Length != 1); if (GUILayout.Toggle(isEditing, "Edit Path", "Button")) { if (!isEditing) { EditShape(); } GUIStyle helpStyle = new GUIStyle(GUI.skin.label); helpStyle.wordWrap = true; helpStyle.normal.textColor = Color.green; EditorGUILayout.LabelField("Click to add a new segment (up to 32).\nShift-drag to separate connected nodes or prevent snapping.\nCtrl-click nodes to delete.\nDouble-click a circular node to make it linear.\nSee docs about performance!", helpStyle); } else { if (isEditing) { StopEditingShape(true); } } if (shape.GetComponent <PolygonCollider2D>() && GUILayout.Button("From Polygon Collider 2D")) { FromPolygonCollider2D(shape); } EditorGUI.EndDisabledGroup(); } // common props EditorGUILayout.PropertyField(blurProp); EditorGUILayout.PropertyField(outlineSizeProp); EditorGUILayout.PropertyField(outlineColorProp); // fill props EditorGUILayout.PropertyField(fillTypeProp); FillType fillType = (FillType)fillTypeProp.enumValueIndex; if (fillType == FillType.Gradient) { EditorGUILayout.PropertyField(gradientTypeProp); if ((GradientType)gradientTypeProp.enumValueIndex < GradientType.Radial) { EditorGUILayout.PropertyField(gradientAxisProp); } EditorGUILayout.PropertyField(gradientStartProp); } if (fillType >= FillType.SolidColor && fillType < FillType.Texture) { EditorGUILayout.PropertyField(fillColorProp); } if (fillType == FillType.Texture) { EditorGUILayout.PropertyField(fillTextureProp); EditorGUILayout.PropertyField(fillScaleProp); } if (fillType >= FillType.Gradient && fillType < FillType.Texture) { EditorGUILayout.PropertyField(fillColor2Prop); } if (fillType >= FillType.Gradient) { EditorGUILayout.PropertyField(fillOffsetProp); EditorGUILayout.PropertyField(fillRotationProp); } if (fillType == FillType.Grid || fillType == FillType.Stripes) { EditorGUILayout.PropertyField(lineSizeProp); } if (fillType == FillType.Grid || fillType == FillType.Stripes || fillType == FillType.CheckerBoard) { EditorGUILayout.PropertyField(gridSizeProp); } EditorGUI.BeginDisabledGroup(Selection.objects.Length != 1); if (GUILayout.Button("Convert to Sprite")) { ConvertToSprite(shape); } EditorGUI.EndDisabledGroup(); EditorGUI.EndDisabledGroup(); serializedObject.ApplyModifiedProperties(); // if the material has been destroyed, configure everything again. // or if the shape has been re-enabled after being converted to a sprite, // attempt to restore the scale it had previously if (shape.enabled && (!shape.IsConfigured() || shape.wasConverted)) { Undo.RecordObjects(shape.GetUndoObjects().ToArray(), "Re-enable Shapes2D Component"); shape.Configure(); if (shape.wasConverted) { shape.RestoreFromConversion(); } // combine with the re-enable action Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); } }