using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace Assets
{
[Serializable]
public abstract class BaseGenericComponent<T1, T2> : MonoBehaviour
where T1 : Button
where T2 : Text
{
[SerializeField]
T1 button;
public void SetButton(T1 button) { this.button = button; }
[SerializeField]
T2 text;
public void SetText(T2 text) { this.text = text; }
public virtual void OnClickOverride()
{
Debug.Log(this.GetType().ToString());
this.gameObject.SetActive(false);
this.gameObject.SetActive(true);
}
void Awake()
{
this.button.onClick.AddListener(this.OnClickOverride);
}
#if UNITY_EDITOR
[SerializeField]
int editorVal1;
[SerializeField]
int editorVal2 = 10;
[SerializeField]
bool editorSetup = false;
void EditorSetup()
{
this.OnEditorSetup();
}
private void OnDrawGizmos()
{
if (this.editorSetup)
{
this.editorSetup = false;
this.EditorSetup();
}
}
public virtual void OnEditorSetup()
{
this.button = GetComponentInChildren<T1>();
this.text = GetComponentInChildren<T2>();
}
#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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace Assets
{
[Serializable]
public abstract class BaseGenericComponent<T1, T2> : MonoBehaviour
where T1 : Button
where T2 : Text
{
[SerializeField]
T1 button;
public void SetButton(T1 button) { this.button = button; }
[SerializeField]
T2 text;
public void SetText(T2 text) { this.text = text; }
public virtual void OnClickOverride()
{
Debug.Log(this.GetType().ToString());
this.gameObject.SetActive(false);
this.gameObject.SetActive(true);
}
void Awake()
{
this.button.onClick.AddListener(this.OnClickOverride);
}
[SerializeField]
int editorVal1;
[SerializeField]
int editorVal2 = 10;
[SerializeField]
bool editorSetup = false;
#if UNITY_EDITOR
void EditorSetup()
{
this.OnEditorSetup();
}
private void OnDrawGizmos()
{
if (this.editorSetup)
{
this.editorSetup = false;
this.EditorSetup();
}
}
public virtual void OnEditorSetup()
{
this.button = GetComponentInChildren<T1>();
this.text = GetComponentInChildren<T2>();
}
#endif// UNITY_EDITOR
}
}
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)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace Assets
{
public abstract class BaseComponent : MonoBehaviour
{
[SerializeField]
Button button;
public void SetButton(Button button) { this.button = button; }
[SerializeField]
Text text;
public void SetText(Text text) { this.text = text; }
public virtual void OnClickOverride()
{
Debug.Log(this.GetType().ToString());
this.gameObject.SetActive(false);
this.gameObject.SetActive(true);
}
void Awake()
{
this.button.onClick.AddListener(this.OnClickOverride);
}
#if UNITY_EDITOR
[SerializeField]
int editorVal1;
[SerializeField]
int editorVal2 = 10;
[SerializeField]
bool editorSetup = false;
void EditorSetup()
{
this.OnEditorSetup();
}
private void OnDrawGizmos()
{
if (this.editorSetup)
{
this.editorSetup = false;
this.EditorSetup();
}
}
public virtual void OnEditorSetup()
{
this.button = GetComponentInChildren<Button>();
this.text = GetComponentInChildren<Text>();
}
#endif// UNITY_EDITOR
}
}
The Point is difference of component between generic and not generic
Unity Engine bug? or logical problem?
I wanna know!!