private void ShowColorArray(SerializedProperty prop) { if (prop.arraySize == 0) { prop.arraySize += 1; prop.GetArrayElementAtIndex(prop.arraySize - 1).colorValue = PixelCharacterDrawTool.RandomColor(); } for (int i = 0; i < prop.arraySize; i++) { EditorGUILayout.PropertyField(prop.GetArrayElementAtIndex(i), new GUIContent(" ● Color " + (i + 1))); } EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("Add Color")) { prop.arraySize += 1; prop.GetArrayElementAtIndex(prop.arraySize - 1).colorValue = PixelCharacterDrawTool.RandomColor(); } if (prop.arraySize >= 2) { if (GUILayout.Button("Remove Color")) { prop.arraySize -= 1; } } EditorGUILayout.Space(); EditorGUILayout.EndHorizontal(); }
private void DrawBodyPartWithStyles(BodyPart bodyPart, Vector2 startPoint, Color skinCol) { PixelCharacterDrawTool.DrawFromPixelTexture(texture, bodyPart.shape, skinCol, startPoint); for (int i = 0; i < bodyPart.styleLayers.Count; i++) { if (bodyPart.styleLayers[i].drawProbability >= Random.Range(0f, 1f)) { PixelTexture style = bodyPart.styleLayers[i].styles[Random.Range(0, bodyPart.styleLayers[i].styles.Count)]; Color col = bodyPart.styleLayers[i].colors[Random.Range(0, bodyPart.styleLayers[i].colors.Length)]; PixelCharacterDrawTool.DrawFromPixelTexture(texture, style, col, startPoint); } } }
public void Draw() { if (GetComponent <SpriteRenderer>().sprite != null) { Texture2D.DestroyImmediate(GetComponent <SpriteRenderer>().sprite.texture); Sprite.DestroyImmediate(GetComponent <SpriteRenderer>().sprite); } texture = PixelCharacterDrawTool.SetupTexture(head.shape, body.shape, legs.shape); GetStartPoints(); Color skinCol = skinColors[Random.Range(0, skinColors.Length)]; DrawBodyPartWithStyles(head, startPoints[2], skinCol); DrawBodyPartWithStyles(body, startPoints[1], skinCol); DrawBodyPartWithStyles(legs, startPoints[0], skinCol); texture.Apply(); GetComponent <SpriteRenderer>().sprite = Sprite.Create(texture, new Rect(new Vector2(0f, 0f), new Vector2(texture.width, texture.height)), new Vector2(0.5f, 0.0f)); }
private void DrawShape(PixelTexture shape, int posX, int posY, string label) { GUI.Label(new Rect(posX, posY - 20, 200, 20), label, EditorStyles.boldLabel); for (int y = 0; y < shape.height; y++) { for (int x = 0; x < shape.width; x++) { Rect rect = new Rect(posX + pixelSize * x, posY + pixelSize * (shape.height - y - 1), pixelSize, pixelSize); if (shape.GetPixel(x, y).a == 0f) { DrawBox(rect, Color.white); continue; } GUI.color = PixelCharacterDrawTool.GreyColor(shape.GetPixel(x, y).val); GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture); DrawBox(rect, Color.white); } } DrawBox(new Rect(posX, posY, shape.width * pixelSize, shape.height * pixelSize), Color.white); }
public override void OnInspectorGUI() { SerializedObject obj = this.serializedObject; obj.Update(); SetFoldoutStyle(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Shape Creation", EditorStyles.boldLabel); if (GUILayout.Button("Open Shape Creator")) { ShapeCreatorWindow.ShowWindow(character); } EditorGUILayout.Space(); EditorGUILayout.LabelField("Character Generation", EditorStyles.boldLabel); if (obj.FindProperty("head").FindPropertyRelative("shape").FindPropertyRelative("isNull").boolValue || obj.FindProperty("body").FindPropertyRelative("shape").FindPropertyRelative("isNull").boolValue || obj.FindProperty("legs").FindPropertyRelative("shape").FindPropertyRelative("isNull").boolValue || obj.FindProperty("skinColors").arraySize == 0) { EditorGUILayout.HelpBox("All Body Parts And A Skin Color Is Required Meanwhile The Rest Is Optional", MessageType.Warning); } else { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Build")) { character.Draw(); } if (character.GetComponent <SpriteRenderer>().sprite != null) { if (GUILayout.Button("Save Texture")) { foreach (PixelCharacter p in targets) { PixelCharacterDrawTool.Save(p.GetComponent <SpriteRenderer>().sprite.texture, p.gameObject.name); } } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Multiple Characters", EditorStyles.centeredGreyMiniLabel); EditorGUILayout.BeginHorizontal(); numOfCharacters = EditorGUILayout.IntField("Amount", numOfCharacters); if (GUILayout.Button("Build")) { character.Draw(); float xSpacing = character.GetComponent <SpriteRenderer>().bounds.size.x * 1.2f; float ySpacing = character.GetComponent <SpriteRenderer>().bounds.size.y * 1.2f; int rows = (int)Mathf.Sqrt(numOfCharacters); int columns = Mathf.CeilToInt(numOfCharacters / (float)rows); int n = 1; GameObject clones = new GameObject(character.name + " Clones"); for (int y = 0; y < rows; y++) { for (int x = 0; x < columns; x++) { if (n <= numOfCharacters) { Vector3 pos = new Vector3(character.gameObject.transform.position.x + xSpacing * 2 + xSpacing * x, character.gameObject.transform.position.y - ySpacing * y, 0f); GameObject clone = Instantiate(character.gameObject, pos, Quaternion.identity) as GameObject; clone.transform.parent = clones.transform; clone.name = character.name /* + " " + (n)*/; clone.GetComponent <PixelCharacter>().Draw(); n++; } } } character.Draw(); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.LabelField("Body Parts", EditorStyles.boldLabel); EditorGUIUtility.labelWidth = 60; ShowBodyPart(character.head); ShowBodyPart(character.body); ShowBodyPart(character.legs); EditorGUIUtility.labelWidth = defaultLabelWidth; EditorGUILayout.Space(); EditorGUILayout.LabelField("Skin Colors"); ShowColorArray(obj.FindProperty("skinColors")); GUILayout.Space(20); EditorGUILayout.Space(); headStyleFoldout = EditorGUILayout.Foldout(headStyleFoldout, "Head Style", myFoldoutStyle); if (headStyleFoldout) { ShowBodyPartStyles(character.head, obj.FindProperty("head"), ref character.tempHeadLayerName); } GUILayout.Space(20); EditorGUILayout.Space(); bodyStyleFoldout = EditorGUILayout.Foldout(bodyStyleFoldout, "Body Style", myFoldoutStyle); if (bodyStyleFoldout) { ShowBodyPartStyles(character.body, obj.FindProperty("body"), ref character.tempBodyLayerName); } GUILayout.Space(20); EditorGUILayout.Space(); legsStyleFoldout = EditorGUILayout.Foldout(legsStyleFoldout, "Legs Style", myFoldoutStyle); if (legsStyleFoldout) { ShowBodyPartStyles(character.legs, obj.FindProperty("legs"), ref character.tempLegsLayerName); } obj.ApplyModifiedProperties(); }