Search Unity

Delegates does not work in editor extensions

Discussion in 'Editor & General Support' started by elseforty, Jun 2, 2020.

  1. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    290
    Hi,

    is there someone here that have managed to get delegates to work in an editor extension

    Thanx
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Can you be more specific?
     
  3. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    290
    i'm trying to use delegates to communicate between two editor extensions,

    i have a spline draw update method, that gets called whenever i change the spline shape, i have put the delegate in that method and i subscribed to the delegate in the other extension,

    problem is that delegate does not get called until i press play
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    How are you invoking the delegate? Are you using C# events, or just raw delegates?
     
  5. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    290
    Hi,
    im using delegates and events

    Code (CSharp):
    1. public class SplineCreationClass
    2. {
    3.  
    4.     public delegate void OnUpdate();
    5.     public static event OnUpdate Update_Spline;
    6.  
    7.     public void Method_Get_Called_When_Spline_Edit()
    8.     {
    9.         if (Update_Spline != null) Update_Spline();
    10.     }
    11. }
    implementation in the other extension

    Code (CSharp):
    1.    private void OnEnable()
    2.     {
    3.         SplineCreationClass.Update_Spline += Update_Splines;
    4.     }
    5.     private void OnDisable()
    6.     {
    7.         SplineCreationClass.Update_Spline -= Update_Splines;
    8.     }
    9.       void Update_Splines()
    10.     {
    11.         Debug.Log("Delegate called");
    12.     }
    13.  
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Ok that looks alright - how are you invoking the method "Method_Get_Called_When_Spline_Edit"?
     
  7. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    290
    the method gets called whenever i edit a spline shape by moving the nodes in the editor,
    i have even put a debug.log to make sure
    Code (CSharp):
    1.  public void Method_Get_Called_When_Spline_Edit()
    2.     {
    3.       Debug.Log("Method_Get_Called_When_Spline_Edit called");
    4.         if (Update_Spline != null) Update_Spline();
    5.     }
    it is showed in the console but the delegate implementation debug.log does not show
    Code (CSharp):
    1.     void Update_Splines()
    2.     {
    3.         Debug.Log("Delegate called");
    4.     }
     
  8. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Have you ensured that OnEnable and OnDisable is getting called when you expect it to?
     
    elseforty likes this.
  9. elseforty

    elseforty

    Joined:
    Apr 3, 2018
    Posts:
    290
    that was the issue, i did a stupid thing by adding the delegate implementation in the mono behaviour class,
    the correct thing to do is to put it in the editor class instead, where on enable and on disable gets called in the editor

    thanx alot
     
    Madgvox likes this.