Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by elix · Dec 17, 2017 at 11:17 AM · inspectorinheritanceevents

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;
         :

alt text
but I can not show it to do this:

 namespace UnityEngine.UI
   {
   public class ScrollRectSticky : ScrollRect
     {
     public static UnityEvent TestABEvent;
             :

alt text
How do I show UnityEvent in inspector on Inheritance class of ScrollRect ?

スクリーンショット-2017-12-17-200114.png (16.4 kB)
スクリーンショット-2017-12-17-200123.png (36.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
     }
 }

alt text


スクリーンショット-2017-12-19-105635.png (35.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 17, 2017 at 01:08 PM 0
Share

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)

avatar image elix · Dec 18, 2017 at 04:28 AM 0
Share

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;
 }

alt text

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 . alt text

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hide headers and variables in inspector from parent classes 1 Answer

Unity Event Inspector Condition 0 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

How to set an object reference field in the Inspector 0 Answers

Values lost? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges