Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The using generic class's serialized variable in ifdef UNITY_EDITOR region

Discussion in 'Scripting' started by RedRina, Oct 16, 2017.

  1. RedRina

    RedRina

    Joined:
    Jul 6, 2017
    Posts:
    3
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Events;
    6. using UnityEngine.UI;
    7.  
    8. namespace Assets
    9. {
    10.     [Serializable]
    11.     public abstract class BaseGenericComponent<T1, T2> : MonoBehaviour
    12.         where T1 : Button
    13.         where T2 : Text
    14.     {
    15.         [SerializeField]
    16.         T1 button;
    17.         public void SetButton(T1 button) { this.button = button; }
    18.  
    19.         [SerializeField]
    20.         T2 text;
    21.         public void SetText(T2 text) { this.text = text; }
    22.  
    23.         public virtual void OnClickOverride()
    24.         {
    25.             Debug.Log(this.GetType().ToString());
    26.             this.gameObject.SetActive(false);
    27.             this.gameObject.SetActive(true);
    28.         }
    29.  
    30.         void Awake()
    31.         {
    32.             this.button.onClick.AddListener(this.OnClickOverride);
    33.         }
    34.  
    35. #if UNITY_EDITOR
    36.         [SerializeField]
    37.         int editorVal1;
    38.  
    39.         [SerializeField]
    40.         int editorVal2 = 10;
    41.  
    42.         [SerializeField]
    43.         bool editorSetup = false;
    44.         void EditorSetup()
    45.         {
    46.             this.OnEditorSetup();
    47.         }
    48.  
    49.         private void OnDrawGizmos()
    50.         {
    51.             if (this.editorSetup)
    52.             {
    53.                 this.editorSetup = false;
    54.                 this.EditorSetup();
    55.             }
    56.         }
    57.  
    58.         public virtual void OnEditorSetup()
    59.         {
    60.             this.button = GetComponentInChildren<T1>();
    61.             this.text = GetComponentInChildren<T2>();
    62.         }
    63. #endif// UNITY_EDITOR
    To make divide functions of component for runtime and editor mode, i'd write as above
    It is just filling serialize data automatically what i want in editor mode only.
    There is working well with this class in runtime and editor mode(not use directly, inherited of coursely)

    But the problem is in Android Logcat


    It cause error text about serialize.
    no crash, just causing error text :(

    Here is the solution
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Events;
    6. using UnityEngine.UI;
    7.  
    8. namespace Assets
    9. {
    10.     [Serializable]
    11.     public abstract class BaseGenericComponent<T1, T2> : MonoBehaviour
    12.         where T1 : Button
    13.         where T2 : Text
    14.     {
    15.         [SerializeField]
    16.         T1 button;
    17.         public void SetButton(T1 button) { this.button = button; }
    18.  
    19.         [SerializeField]
    20.         T2 text;
    21.         public void SetText(T2 text) { this.text = text; }
    22.  
    23.         public virtual void OnClickOverride()
    24.         {
    25.             Debug.Log(this.GetType().ToString());
    26.             this.gameObject.SetActive(false);
    27.             this.gameObject.SetActive(true);
    28.         }
    29.  
    30.         void Awake()
    31.         {
    32.             this.button.onClick.AddListener(this.OnClickOverride);
    33.         }
    34.  
    35.         [SerializeField]
    36.         int editorVal1;
    37.  
    38.         [SerializeField]
    39.         int editorVal2 = 10;
    40.  
    41.         [SerializeField]
    42.         bool editorSetup = false;
    43. #if UNITY_EDITOR
    44.         void EditorSetup()
    45.         {
    46.             this.OnEditorSetup();
    47.         }
    48.  
    49.         private void OnDrawGizmos()
    50.         {
    51.             if (this.editorSetup)
    52.             {
    53.                 this.editorSetup = false;
    54.                 this.EditorSetup();
    55.             }
    56.         }
    57.  
    58.         public virtual void OnEditorSetup()
    59.         {
    60.             this.button = GetComponentInChildren<T1>();
    61.             this.text = GetComponentInChildren<T2>();
    62.         }
    63. #endif// UNITY_EDITOR
    64.     }
    65. }
    66.  
    Just move the serialize variables to out of ifdef UNITY_EDITOR region lol
    But these use only editor mode, so i'm wrotten it in region


    This is a case of no error text (working well also)
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Events;
    6. using UnityEngine.UI;
    7.  
    8. namespace Assets
    9. {
    10.     public abstract class BaseComponent : MonoBehaviour
    11.     {
    12.         [SerializeField]
    13.         Button button;
    14.         public void SetButton(Button button) { this.button = button; }
    15.  
    16.         [SerializeField]
    17.         Text text;
    18.         public void SetText(Text text) { this.text = text; }
    19.  
    20.         public virtual void OnClickOverride()
    21.         {
    22.             Debug.Log(this.GetType().ToString());
    23.             this.gameObject.SetActive(false);
    24.             this.gameObject.SetActive(true);
    25.         }
    26.  
    27.         void Awake()
    28.         {
    29.             this.button.onClick.AddListener(this.OnClickOverride);
    30.         }
    31.  
    32. #if UNITY_EDITOR
    33.         [SerializeField]
    34.         int editorVal1;
    35.  
    36.         [SerializeField]
    37.         int editorVal2 = 10;
    38.  
    39.         [SerializeField]
    40.         bool editorSetup = false;
    41.         void EditorSetup()
    42.         {
    43.             this.OnEditorSetup();
    44.         }
    45.  
    46.         private void OnDrawGizmos()
    47.         {
    48.             if (this.editorSetup)
    49.             {
    50.                 this.editorSetup = false;
    51.                 this.EditorSetup();
    52.             }
    53.         }
    54.  
    55.         public virtual void OnEditorSetup()
    56.         {
    57.             this.button = GetComponentInChildren<Button>();
    58.             this.text = GetComponentInChildren<Text>();
    59.         }
    60. #endif// UNITY_EDITOR
    61.     }
    62. }
    63.  
    The Point is difference of component between generic and not generic
    Unity Engine bug? or logical problem?

    I wanna know!!
     
    Last edited: Oct 16, 2017
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Why not just put Editor scripting in editor-only classes? Theres really no reason editor code should exist in a run-time build.

    As for the generic vs non-generic I typically write both and have the generic inherit from the non-generic. This allows me to get around some of the issues you can get with showing generics in an inspector.