- Home /
How do I show UnityEvent in inspector on Inheritance class of ScrollRect ?
Hi, I made page swipe class inheritance from ScrollRect.
but I cannot show added UnityEvent in Inspector.
I can show UnityEvent as following:
namespace UnityEngine.UI
{
public class TestClassAB : UIBehaviour
{
public static UnityEvent TestABEvent;
:
but I can not show it to do this:
namespace UnityEngine.UI
{
public class ScrollRectSticky : ScrollRect
{
public static UnityEvent TestABEvent;
:
How do I show UnityEvent in inspector on Inheritance class of ScrollRect ?
Answer by elix · Dec 19, 2017 at 01:58 AM
I solved this problem with EditorScript as follows:
using UnityEngine;
using UnityEditor;
using UnityEditor.UI;
using UnityEditor.Events;
[CanEditMultipleObjects, CustomEditor(typeof(TestClassA),true)]
public class TestClassAEditor : ScrollRectEditor {
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
this.serializedObject.Update();
EditorGUILayout.PropertyField(this.serializedObject.FindProperty("testAEvent"), true);
this.serializedObject.ApplyModifiedProperties();
}
}
Answer by Bunny83 · Dec 17, 2017 at 01:01 PM
Your event must not be static. Static variables are never serialized in Unity. Your first code example does not belong to your first screen shot. In order to have the "TestABEvent" show up in the inspector the event has to be an instance variable.
public UnityEvent TestABEvent;
Read the 4 bulletpoints in the description here. A more detailed description of how the serialization system works can be found over here.
ps: You should not declare your own classes inside the UnityEngine namespace. The main point of using namespaces is to avoid name collisions. One namespace should be maintained by one developer / cooperation so they have an overview what exists in that namespace.
Use your own namespace and maybe some grouping subnamespace. Forexample i start all my namespaces with "B83" ^^. If you work for a company you usually use the company name as top-level namespace. If certain classes are specific for one particular project you may use the project name as top level or second level namespace.
In general you should avoid adding things to other namespaces. There are of course exceptions to this rule (for example System.Linq might be used if you also implement some Linq extensions)
Thank you for your great help! but the problem does not solved.
I tried to chang my code as you say:
$$anonymous$$aybe it is necessary to add Editor script. I'm trying it now..
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
public class TestClassA : UIBehaviour {
[System.Serializable]
public class TestAEvent : UnityEvent<int>
{
}
public TestAEvent testAEvent;
}
and I changed from UIBehaviour to ScrollRect
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
public class TestClassA : ScrollRect {
[System.Serializable]
public class TestAEvent : UnityEvent<int>
{
}
public TestAEvent testAEvent;
}
so testAEvent will not show in inspector .
Your answer
![](https://koobas.hobune.stream/wayback/20220612144730im_/https://answers.unity.com/themes/thub/images/avi.jpg)