Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OnEd0t · Oct 18, 2019 at 08:46 PM · uiguieditoreditorwindowextension

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a progress bar in the editor lock the background? 0 Answers

How to force Unity to Repaint an EditorWindow? 1 Answer

Create UI elements dynamically on Unity 4.6 2 Answers

UI element not snapping into the middle of parent? 2 Answers

Editor GUI Foldout header style customization 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges