void AddBehavior(CompositeBehaviorV2 cb) { int oldCount = (cb.behaviors != null) ? cb.behaviors.Length : 0; HerdBehavior[] newBehaviors = new HerdBehavior[oldCount + 1]; float[] newWeights = new float[oldCount + 1]; for (int i = 0; i < oldCount; i++) { newBehaviors[i] = cb.behaviors[i]; newWeights[i] = cb.weights[i]; } newWeights[oldCount] = 1f; cb.behaviors = newBehaviors; cb.weights = newWeights; }
void RemoveBehavior(CompositeBehaviorV2 cb) { int oldCount = cb.behaviors.Length; if (oldCount == 1) { cb.behaviors = null; cb.weights = null; return; } HerdBehavior[] newBehaviors = new HerdBehavior[oldCount - 1]; float[] newWeights = new float[oldCount - 1]; for (int i = 0; i < oldCount - 1; i++) { newBehaviors[i] = cb.behaviors[i]; newWeights[i] = cb.weights[i]; } cb.behaviors = newBehaviors; cb.weights = newWeights; }
public override void OnInspectorGUI() { // setup of inspector CompositeBehaviorV2 cb = (CompositeBehaviorV2)target; // the thing being looked at in the inspector Rect r = EditorGUILayout.BeginHorizontal(); r.height = EditorGUIUtility.singleLineHeight; // check for behaviors if (cb.behaviors == null || cb.behaviors.Length == 0) { EditorGUILayout.HelpBox("no behaviors in array.", MessageType.Warning); EditorGUILayout.EndHorizontal(); r = EditorGUILayout.BeginHorizontal(); r.height = EditorGUIUtility.singleLineHeight; } else { // Headers r.x = 30f; r.width = EditorGUIUtility.currentViewWidth - 95f; EditorGUI.LabelField(r, "Behaviors"); r.x = EditorGUIUtility.currentViewWidth - 65f; r.width = 60f; EditorGUI.LabelField(r, "Weights"); EditorGUI.BeginChangeCheck(); // Behaviors and Weights r.y += EditorGUIUtility.singleLineHeight * 1.2f; for (int i = 0; i < cb.behaviors.Length; i++) { r.x = 5f; r.width = 20f; EditorGUI.LabelField(r, i.ToString()); r.x = 30f; r.width = EditorGUIUtility.currentViewWidth - 95f; cb.behaviors[i] = (HerdBehavior)EditorGUI.ObjectField(r, cb.behaviors[i], typeof(HerdBehavior), false); r.x = EditorGUIUtility.currentViewWidth - 65f; r.width = 60f; cb.weights[i] = EditorGUI.FloatField(r, cb.weights[i]); r.y += EditorGUIUtility.singleLineHeight * 1.1f; } if (EditorGUI.EndChangeCheck()) { EditorUtility.SetDirty(cb); } } // Buttons for adding or removing behaviors EditorGUILayout.EndHorizontal(); r.x = 5f; r.width = EditorGUIUtility.currentViewWidth - 10f; r.y += EditorGUIUtility.singleLineHeight * 0.5f; if (GUI.Button(r, "Add Behavior")) { AddBehavior(cb); EditorUtility.SetDirty(cb); } r.y += EditorGUIUtility.singleLineHeight * 1.5f; if (cb.behaviors != null && cb.behaviors.Length > 0) { if (GUI.Button(r, "Remove Behavior")) { RemoveBehavior(cb); EditorUtility.SetDirty(cb); } } }