gargaroots

Composite Behavior Editor

Apr 18th, 2021
491
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. [CustomEditor(typeof(CompositeBehavior))]
  7. public class CompositeBehaviorEditor : Editor
  8. {
  9.     public override void OnInspectorGUI()
  10.     {
  11.         //setup
  12.         CompositeBehavior cb = (CompositeBehavior)target;
  13.  
  14.         //check for behaviors
  15.         if (cb.behaviors == null || cb.behaviors.Length == 0)
  16.         {
  17.             EditorGUILayout.BeginHorizontal();
  18.             EditorGUILayout.HelpBox("No behaviors in array.", MessageType.Warning);
  19.             EditorGUILayout.EndHorizontal();
  20.         }
  21.  
  22.         else
  23.         {
  24.             EditorGUILayout.BeginHorizontal();
  25.             EditorGUILayout.LabelField("Number", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
  26.             EditorGUILayout.LabelField("Behaviors", GUILayout.MinWidth(60f));
  27.             EditorGUILayout.LabelField("Weights", GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
  28.             EditorGUILayout.EndHorizontal();
  29.  
  30.             for (int i = 0; i < cb.behaviors.Length; i++)
  31.             {
  32.                 EditorGUILayout.BeginHorizontal();
  33.                 EditorGUILayout.LabelField(i.ToString(), GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
  34.                 cb.behaviors[i] = (FlockBehavior)EditorGUILayout.ObjectField(cb.behaviors[i], typeof(FlockBehavior), false, GUILayout.MinWidth(60f));
  35.                 cb.weights[i] = EditorGUILayout.FloatField(cb.weights[i], GUILayout.MinWidth(60f), GUILayout.MaxWidth(60f));
  36.                 EditorGUILayout.EndHorizontal();
  37.             }
  38.         }
  39.        
  40.         if (GUILayout.Button("Add Behavior"))
  41.         {
  42.             AddBehavior(cb);
  43.             EditorUtility.SetDirty(cb);
  44.         }
  45.  
  46.         if (cb.behaviors != null && cb.behaviors.Length > 0)
  47.         {
  48.             if (GUILayout.Button("RemoveBehavior"))
  49.             {
  50.                 RemoveBehavior(cb);
  51.                 EditorUtility.SetDirty(cb);
  52.             }
  53.         }
  54.     }
  55.  
  56.     void AddBehavior(CompositeBehavior cb)
  57.     {
  58.         int oldCount = (cb.behaviors != null) ? cb.behaviors.Length : 0;
  59.         FlockBehavior[] newBehaviors = new FlockBehavior[oldCount + 1];
  60.         float[] newWeights = new float[oldCount + 1];
  61.         for (int i = 0; i < oldCount; i++)
  62.         {
  63.             newBehaviors[i] = cb.behaviors[i];
  64.             newWeights[i] = cb.weights[i];
  65.         }
  66.         newWeights[oldCount] = 1f;
  67.         cb.behaviors = newBehaviors;
  68.         cb.weights = newWeights;
  69.     }
  70.  
  71.     void RemoveBehavior(CompositeBehavior cb)
  72.     {
  73.         int oldCount = cb.behaviors.Length;
  74.         if (oldCount == 1)
  75.         {
  76.             cb.behaviors = null;
  77.             cb.weights = null;
  78.             return;
  79.         }
  80.         FlockBehavior[] newBehaviors = new FlockBehavior[oldCount - 1];
  81.         float[] newWeights = new float[oldCount - 1];
  82.         for (int i = 0; i < oldCount - 1; i++)
  83.         {
  84.             newBehaviors[i] = cb.behaviors[i];
  85.             newWeights[i] = cb.weights[i];
  86.         }
  87.         cb.behaviors = newBehaviors;
  88.         cb.weights = newWeights;
  89.     }
  90. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×