How can I create my own UI object if I can not inherit from GameObject?

Hi All,
since from Unity GUI the user can create a Panel object (see following image) I tryed to create my own class Panel. I thought to inherit from GameObject but the class is sealed! So which is the suggested way to implement a new UI class?

3268280--252342--upload_2017-10-27_15-12-51.png

Regards,
Gianni

You can inherit your UI classes from UIBehaviour (Redirecting to latest version of com.unity.ugui), if you need it.

Thanks Kru,
I will give a look at that. Anyway UIBehaviour is a Component. Some other Ui classes like RectTransform inherits from Component. Isn’t possible to inherit directly from Component then?

Regards,
Gianni

Moreover, I have this:

Panel.cs
public class Panel : Component
{
public Panel() {
gameObject.layer = 5; // UI
}
}

but when I call the following line the Panel constructor is never called.
Panel debug = panelGameObject.AddComponent();

So, what should I implement to make the AddComponent working?

Regards,
Gianni

MonoBehaviour constructors should be avoided. Prefer the Awake method to handle most initialization. In the rare event that using the default constructor is warranted, you are limited to manipulating only fields and properties that are wholly owned by the type being created. In your Panel example, gameObject is not wholly owned by the Panel class because the panel does not create the instance of gameObject. So it is unwise to attempt to manipulate the gameObject within the constructor of the panel.

However, if you were to put your initialization inside of Awake(), rather than the ctor, you’d find that the logic works. Note that in order for the Awake method to be called at the appropriate time, your class will need to inherit from MonoBehaviour (not Component).