Skip to content

Commit

Permalink
Added PlanterEditor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Keijiro Takahashi authored and Keijiro Takahashi committed Jul 28, 2014
1 parent 4c5b7da commit e2aba7c
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 26 deletions.
88 changes: 88 additions & 0 deletions Assets/Reaktion/Editor/Utility/PlanterEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// Reaktion - An audio reactive animation toolkit for Unity.
//
// Copyright (C) 2013, 2014 Keijiro Takahashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using UnityEngine;
using UnityEditor;
using System.Collections;

namespace Reaktion {

[CustomEditor(typeof(Planter)), CanEditMultipleObjects]
public class PlanterEditor : Editor
{
SerializedProperty propPrefabs;
SerializedProperty propMaxObjects;
SerializedProperty propDistributionMode;
SerializedProperty propDistributionRange;
SerializedProperty propGridSpace;
SerializedProperty propRotationMode;
SerializedProperty propIntervalMode;
SerializedProperty propInterval;

void OnEnable()
{
propPrefabs = serializedObject.FindProperty("prefabs");
propMaxObjects = serializedObject.FindProperty("maxObjects");
propDistributionMode = serializedObject.FindProperty("distributionMode");
propDistributionRange = serializedObject.FindProperty("distributionRange");
propGridSpace = serializedObject.FindProperty("gridSpace");
propRotationMode = serializedObject.FindProperty("rotationMode");
propIntervalMode = serializedObject.FindProperty("intervalMode");
propInterval = serializedObject.FindProperty("interval");
}

public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(propPrefabs, true);
EditorGUILayout.PropertyField(propMaxObjects);

EditorGUILayout.PropertyField(propDistributionMode);
if (propDistributionMode.hasMultipleDifferentValues ||
propDistributionMode.enumValueIndex != (int)Planter.DistributionMode.Single)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(propDistributionRange, GUIContent.none);
EditorGUI.indentLevel--;
}

if (propDistributionMode.hasMultipleDifferentValues ||
propDistributionMode.enumValueIndex == (int)Planter.DistributionMode.Grid)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(propGridSpace);
EditorGUI.indentLevel--;
}

EditorGUILayout.PropertyField(propRotationMode);

EditorGUILayout.PropertyField(propIntervalMode);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(propInterval);
EditorGUI.indentLevel--;

serializedObject.ApplyModifiedProperties();
}
}

} // namespace Reaktion
8 changes: 8 additions & 0 deletions Assets/Reaktion/Editor/Utility/PlanterEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 26 additions & 26 deletions Assets/Reaktion/Utility/Planter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ public class Planter : MonoBehaviour
{
// General parameters.
public GameObject[] prefabs;
public int maxObjects;
public int maxObjects = 100;

// Placement parameters.
public enum PlacementType { Single, Random, Grid }
public PlacementType placementType;
public Vector2 extent;
public float gridSize;
// Distribution settings.
public enum DistributionMode { Single, Random, Grid }
public DistributionMode distributionMode;
public Vector2 distributionRange = new Vector2(3, 0);
public float gridSpace = 1;

// Rotation parameters.
public enum RotationType { NotTouch, Planter, Random }
public RotationType rotationType;
// Rotation setting.
public enum RotationMode { Keep, Planter, Random }
public RotationMode rotationMode;

// Interval parameters.
public enum IntervalType { Distance, Time }
public IntervalType intervalType;
public float interval;
// Interval settings.
public enum IntervalMode { Distance, Time }
public IntervalMode intervalMode;
public float interval = 1;

// Object pool.
GameObject[] objectPool;
int objectPoolIndex;

// Interval counter.
// Variables for managing interval.
float intervalCounter;
Vector3 previousPosition;
Quaternion previousRotation;
Expand All @@ -60,7 +60,7 @@ public enum IntervalType { Distance, Time }
void PutInstance(Vector3 position, Quaternion rotation)
{
// Randomize rotation if needed.
if (rotationType == RotationType.Random)
if (rotationMode == RotationMode.Random)
rotation = Random.rotation;

// Pick up the oldest object.
Expand All @@ -70,7 +70,7 @@ void PutInstance(Vector3 position, Quaternion rotation)
{
// Make a new instance and push it to the pool.
var prefab = prefabs[Random.Range(0, prefabs.Length)];
if (rotationType == RotationType.NotTouch)
if (rotationMode == RotationMode.Keep)
rotation = prefab.transform.rotation;
go = Instantiate(prefab, position, rotation) as GameObject;
objectPool[objectPoolIndex] = go;
Expand All @@ -79,7 +79,7 @@ void PutInstance(Vector3 position, Quaternion rotation)
{
// Reuse the oldest object in the pool.
go.transform.position = position;
if (rotationType != RotationType.NotTouch)
if (rotationMode != RotationMode.Keep)
go.transform.rotation = rotation;
}

Expand All @@ -95,16 +95,16 @@ void PlantAlongGrid(Vector3 position, Quaternion rotation)
var ly = rotation * Vector3.up;

// Number of columns and rows.
var nx = Mathf.Max(Mathf.FloorToInt(extent.x / gridSize), 0);
var ny = Mathf.Max(Mathf.FloorToInt(extent.y / gridSize), 0);
var nx = Mathf.Max(Mathf.FloorToInt(distributionRange.x / gridSpace), 0);
var ny = Mathf.Max(Mathf.FloorToInt(distributionRange.y / gridSpace), 0);

// Put instances on each point of the grid.
for (var y = 0; y <= ny; y++)
{
var dy = gridSize * ((float)y - 0.5f * ny);
var dy = gridSpace * ((float)y - 0.5f * ny);
for (var x = 0; x <= nx; x++)
{
var dx = gridSize * ((float)x - 0.5f * nx);
var dx = gridSpace * ((float)x - 0.5f * nx);
PutInstance(position + lx * dx + ly * dy, rotation);
}
}
Expand All @@ -118,8 +118,8 @@ void PlantRandom(Vector3 position, Quaternion rotation)
var ly = rotation * Vector3.up;

// Get random value.
var dx = (Random.value - 0.5f) * extent.x;
var dy = (Random.value - 0.5f) * extent.y;
var dx = (Random.value - 0.5f) * distributionRange.x;
var dy = (Random.value - 0.5f) * distributionRange.y;

// Put an instance on the point.
PutInstance(position + lx * dx + ly * dy, rotation);
Expand All @@ -139,7 +139,7 @@ void Start()
void Update()
{
// Get delta value on the interval parameter.
var delta = intervalType == IntervalType.Distance ?
var delta = intervalMode == IntervalMode.Distance ?
Vector3.Distance(transform.position, previousPosition) : Time.deltaTime;

// Look for the next plant position between frames.
Expand All @@ -151,9 +151,9 @@ void Update()
var rotation = Quaternion.Slerp(previousRotation, transform.rotation, p);

// Plant!
if (placementType == PlacementType.Grid)
if (distributionMode == DistributionMode.Grid)
PlantAlongGrid(position, rotation);
else if (placementType == PlacementType.Random)
else if (distributionMode == DistributionMode.Random)
PlantRandom(position, rotation);
else
PutInstance(position, rotation);
Expand Down

0 comments on commit e2aba7c

Please sign in to comment.