Search Unity

A custom editor script is causing the scene to become corrupted, losing references to all scripts

Discussion in 'Scripting' started by Abbrew, Jan 16, 2020.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    I made a custom editor script that exposes a button. Upon clicking the button all Monobehaviors on the GameObject implementing a certain interface are gathered, and a TextAsset is extracted from each one through the interface and then added to a single Monobehavior's public array of TextAssets. However, clicking the button causes the scene to become corrupted the next time I run Play. Reimport-All always fixes the problem, but the entire process causes huge delays in development. Here are the editor scripts:

    Code (CSharp):
    1. using UnityEngine;
    2. using Panda;
    3.  
    4. [RequireComponent(typeof(PandaBehaviour))]
    5. public class BehaviorTreeAILinkerAggregator : MonoBehaviour
    6. {
    7.     public void ImportAILinkers()
    8.     {
    9.         var ai = GetComponent<PandaBehaviour>();
    10.  
    11.         var aiLinkers = GetComponents<IBehaviorTreeAILinker>();
    12.         var numAILinkers = aiLinkers.Length;
    13.         ai.scripts = new TextAsset[numAILinkers];
    14.         for (var i = 0; i < numAILinkers; i++)
    15.         {
    16.             var aiLinker = aiLinkers[i];
    17.             ai.scripts[i] = aiLinker.GetAI();
    18.         }
    19.     }
    20. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(BehaviorTreeAILinkerAggregator))]
    5. public class BehaviorTreeAILinkerAggregatorEditor : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         DrawDefaultInspector();
    10.  
    11.         BehaviorTreeAILinkerAggregator script = (BehaviorTreeAILinkerAggregator)target;
    12.         if (GUILayout.Button("Import AI Linkers"))
    13.         {
    14.             script.ImportAILinkers();
    15.         }
    16.     }
    17. }
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Entities;
    3.  
    4. [RequireComponent(typeof(EntityStore))]
    5. [RequireComponent(typeof(BehaviorTreeAILinkerAggregator))]
    6. public abstract class BehaviorTreeAILinker : MonoBehaviour, IBehaviorTreeAILinker
    7. {
    8.     [SerializeField]
    9.     private TextAsset ai;
    10.  
    11.     protected EntityManager EM
    12.     {
    13.         get;
    14.         private set;
    15.     }
    16.  
    17.     protected Entity User
    18.     {
    19.         get;
    20.         private set;
    21.     }
    22.  
    23.     public TextAsset GetAI()
    24.     {
    25.         return ai;
    26.     }
    27.  
    28.     protected void CreateMessage<T>(T message)
    29.         where T : struct, IComponentData
    30.     {
    31.         var messageEntity = EM.CreateEntity();
    32.         EM.AddComponentData(messageEntity, message);
    33.         EM.AddComponentData(messageEntity, new MessagingEnd());
    34.     }
    35.  
    36.     protected virtual void Start()
    37.     {
    38.         EM = World.DefaultGameObjectInjectionWorld.EntityManager;
    39.         User = GetComponent<EntityStore>().entity;
    40.     }
    41. }
     
  2. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Anyone encountered this issue before? This is happening quite frequently to me