- Home /
GUI Editior Script not working in ( Unity 2019.x )
Hi, so I was following This Flocking algorithm tutorial and everything worked perfectly till i got to this episode of the tutorial which is about making A custom editor mini extention type thing...(I dont know how to describe the epesode), after following it, i found out that most of the script doesn't work, and thats not a mistake in the actual script, its a problem that has got to do with unity versions, and the script doesnt work in the 2019 versions of unity for some reason, i found an explenation of how to work around that in the comments which is posted by " Dexter André Osiander " but i cant seem to be able to preform it, I've never worked with the GUI system before and i mostly dont understand the script, i am sure its simple but i cant get figure it out for some reason, so what i am asking for is can someone follow the instructions of this guy's comment or come up with a solution himself, any help is appreciated. Thanks in advance!
here is the script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
[CustomEditor(typeof(CompositeBehavior))]
public class CompositeBehaviorEditor : Editor
{
public override void OnInspectorGUI()
{
//setup
CompositeBehavior cb = (CompositeBehavior)target;
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
{
r.x = 30f;
r.width = EditorGUIUtility.currentViewWidth - 95f;
EditorGUI.LabelField(r, "Behaviors");
r.x = EditorGUIUtility.currentViewWidth - 65f;
r.width = 60f;
EditorGUI.LabelField(r, "Weights");
r.y += EditorGUIUtility.singleLineHeight * 1.2f;
EditorGUI.BeginChangeCheck();
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] = (FlockBehavior)EditorGUI.ObjectField(r, cb.behaviors[i], typeof(FlockBehavior), 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);
}
}
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);
}
}
}
void AddBehavior(CompositeBehavior cb)
{
int oldCount = (cb.behaviors != null) ? cb.behaviors.Length : 0;
FlockBehavior[] newBehaviors = new FlockBehavior[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(CompositeBehavior cb)
{
int oldCount = cb.behaviors.Length;
if (oldCount == 1)
{
cb.behaviors = null;
cb.weights = null;
return;
}
FlockBehavior[] newBehaviors = new FlockBehavior[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;
}
}