Tool preEditTool = Tool.None; // the tool the user had selected before clicking edit void OnEnable() { Shape shape = (Shape)serializedObject.targetObject; if (!shape.GetComponent <SpriteRenderer>() && !shape.GetComponent <Image>()) { if (shape.GetComponentInParent <Canvas>() == null) { Undo.AddComponent <SpriteRenderer>(shape.gameObject); } else { Undo.AddComponent <Image>(shape.gameObject); } // collapse into the operation that made this happen Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); shape.Configure(); } shapeTypeProp = serializedObject.FindProperty("settings._shapeType"); outlineSizeProp = serializedObject.FindProperty("settings._outlineSize"); blurProp = serializedObject.FindProperty("settings._blur"); outlineColorProp = serializedObject.FindProperty("settings._outlineColor"); roundnessPerCornerProp = serializedObject.FindProperty("settings._roundnessPerCorner"); roundnessProp = serializedObject.FindProperty("settings._roundness"); roundnessTLProp = serializedObject.FindProperty("settings._roundnessTopLeft"); roundnessTRProp = serializedObject.FindProperty("settings._roundnessTopRight"); roundnessBLProp = serializedObject.FindProperty("settings._roundnessBottomLeft"); roundnessBRProp = serializedObject.FindProperty("settings._roundnessBottomRight"); innerCutoutProp = serializedObject.FindProperty("settings._innerCutout"); startAngleProp = serializedObject.FindProperty("settings._startAngle"); endAngleProp = serializedObject.FindProperty("settings._endAngle"); invertArcProp = serializedObject.FindProperty("settings._invertArc"); fillTypeProp = serializedObject.FindProperty("settings._fillType"); fillColorProp = serializedObject.FindProperty("settings._fillColor"); fillColor2Prop = serializedObject.FindProperty("settings._fillColor2"); fillRotationProp = serializedObject.FindProperty("settings._fillRotation"); fillOffsetProp = serializedObject.FindProperty("settings._fillOffset"); fillScaleProp = serializedObject.FindProperty("settings._fillScale"); gradientTypeProp = serializedObject.FindProperty("settings._gradientType"); gradientStartProp = serializedObject.FindProperty("settings._gradientStart"); gradientAxisProp = serializedObject.FindProperty("settings._gradientAxis"); fillTextureProp = serializedObject.FindProperty("settings._fillTexture"); gridSizeProp = serializedObject.FindProperty("settings._gridSize"); lineSizeProp = serializedObject.FindProperty("settings._lineSize"); triangleOffsetProp = serializedObject.FindProperty("settings._triangleOffset"); polygonPresetProp = serializedObject.FindProperty("settings._polygonPreset"); usePolygonMapProp = serializedObject.FindProperty("settings._usePolygonMap"); pathThicknessProp = serializedObject.FindProperty("settings._pathThickness"); fillPathLoopsProp = serializedObject.FindProperty("settings._fillPathLoops"); }
public void Init(RockData aRockData, bool ignoreNewlySeen) { rockData = aRockData; mIgnoreNewlySeen = ignoreNewlySeen; if (!mRockShape) { if (mRockShapes == null) { mRockShapes = rockShapesRoot.GetComponentsInChildren <Shapes2D.Shape>(); } if (mRockShapes.Length > 0) { int rockShapeInd = Random.Range(0, mRockShapes.Length); for (int i = 0; i < mRockShapes.Length; i++) { mRockShapes[i].gameObject.SetActive(rockShapeInd == i); } mRockShape = mRockShapes[rockShapeInd]; mRockShapeImage = mRockShape.GetComponent <Image>(); } } }
private void FromPolygonCollider2D(Shape shape) { PolygonCollider2D pc2d = shape.GetComponent <PolygonCollider2D>(); if (shape.settings.shapeType == ShapeType.Polygon) { if (pc2d.points.Length >= 64) { EditorUtility.DisplayDialog("Too many points", "The PolygonCollider2D has too many points (max 64).", "Okay"); return; } Vector3[] points = new Vector3[pc2d.points.Length]; for (int i = 0; i < pc2d.points.Length; i++) { points[i] = shape.transform.TransformPoint(pc2d.points[i]); } Undo.RecordObject(shape, "Edit Shapes2D Polygon Vertices"); shape.SetPolygonWorldVertices(points); EditorUtility.SetDirty(target); } else if (shape.settings.shapeType == ShapeType.Path) { if (pc2d.points.Length >= 32) { EditorUtility.DisplayDialog("Too many points", "The PolygonCollider2D has too many points (max 32).", "Okay"); return; } PathSegment[] segments = new PathSegment[pc2d.points.Length]; for (int i = 0; i < pc2d.points.Length; i++) { Vector3 p0 = shape.transform.TransformPoint(pc2d.points[i]); Vector3 p2 = i == pc2d.points.Length - 1 ? shape.transform.TransformPoint(pc2d.points[0]) : shape.transform.TransformPoint(pc2d.points[i + 1]); segments[i] = new PathSegment(p0, p2); } Undo.RecordObject(shape, "Edit Shapes2D Path Segments"); shape.SetPathWorldSegments(segments); EditorUtility.SetDirty(target); } }
private void SetPolygonCollider2D(Shape shape) { PolygonCollider2D pc2d = shape.GetComponent <PolygonCollider2D>(); if (shape.settings.shapeType == ShapeType.Polygon) { if (!pc2d) { pc2d = shape.gameObject.AddComponent <PolygonCollider2D>(); } Vector3[] points = shape.GetPolygonWorldVertices(); Vector2[] colliderPoints = new Vector2[points.Length]; for (int i = 0; i < points.Length; i++) { colliderPoints[i] = shape.transform.InverseTransformPoint(points[i]); } Undo.RecordObject(pc2d, "Set PolygonCollider2D Points"); pc2d.points = colliderPoints; } }
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()); } }