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
0
Question by NonameFTW · May 03, 2016 at 07:50 PM · c#guiinspector

Dynamic Target for Object field (and others) in Custom Inspector

Hey there,

I am trying to implement a functionality framework for the UI in unity. I am writing a custom inspector for it because the event system of unity can only handle methods with maxmimum of one parameter. The solution I thought of is using a custom inspector to choose the functionality and change the UI dynamically.

The monobehaviour (Not the actual code just snippets and with names changed!):

 private static readonly List<string>        methods = new List<string>();
 
         /// <summary>
         /// Selector for the method that should be fired.
         /// </summary>
         private int targetmethod;
         public int Targetmethod {get {return targetmethod;} set {targetmethod = value;}}
         public static int Methodcount {get {return methods.Count;}}
 
 
         /// <summary>
         /// Initialization of static elements.
         /// </summary>
         static StaticConstructor()
         {
             methods.Add("StartScene");
             methods.Add("ChangeTextField");
             methods.Add("SaveGame");
             methods.Add("LoadGame");
             methods.Add("Quit Game");
             methods.Add("Set Sound volume");
             methods.Add("Set Window Mode");
             methods.Add("Change Resolution");
             methods.Add("Method8");
         }
 
         /// <summary>
         /// Constructor
         /// </summary>
         public Constructor()
         {
             Object1 = new object();
             Object2 = new object();
             Object3 = new object();
             Object4 = new object();
             Object5 = new object();
         }
 
         /// <summary>
         /// Get string Array of all available methods.
         /// </summary>
         public string[] Methods
         {
             get
             {
                 return methods.ToArray();
             }
         }
 
         
         /// <summary>
         /// placeholder objects to be manipulated.
         /// </summary>
         public object Object1, Object2, Object3, Object4, Object5;
         
         /// <summary>
         /// Activate selected function.
         /// </summary>
         public void FireFunction()
         {
             switch (targetmethod)
             {
                 case 0:
                     try
                     {
                         StartScene(Object1 as string);
                         Debug.Log("Called" + methods[targetmethod] + " on " + this.name);
                     }
                     catch (Exception e)
                     {
                         Debug.LogError(e.Message);
                         throw;
                     }
                     break;
                 case 1:
              
 
              ....
 
         }
 
 private void StartScene(string sceneName)
         {
             try
             { SceneManager.LoadScene(sceneName); }
             catch(Exception e)
             {
                 Debug.Log(e.Message);
                 if (sceneName == null) throw new ArgumentNullException(sceneName,"Scene Name is not given.");
             }
         }

I am using C# type: object as targets because I can't anticipate the way this framework would be used. Using type specific objects would make a huge mess of public objects in the monobehaviour (2-5 objects per type of UI element)

The Custom Inspector (Not the actual code just snippets and with names changed!):

 public int Index = 0;
         private ClassName classnametargetvariable;
         private EditorGUILayout layout;
         private GUILayoutOption[] options;
 
         public ClassNameEditor(GUILayoutOption[] options, EditorGUILayout layout)
         {
             this.options = options;
             this.layout = layout;
         }
 
 
         /// <summary>
         /// <para>
         /// Implement this function to make a custom inspector.
         /// </para>
         /// </summary>
         public override void OnInspectorGUI()
         {
             //base.OnInspectorGUI();
 
             classnametargetvariable = (class) target;
             //options = new GUILayoutOption[1];
             //options[0] = new GUILayoutOption(GUILayoutOption.Type.stretchWidth, true);
             //string[] bStrings = classnametargetvariable .Methods;
         
             Index = EditorGUILayout.Popup(Index, ClassNametargetvariable.Methods);
             classnametargetvariable .Targetmethod = Index;
             switch (Index)
             {
                 case 0:
                     StartSceneUi();
                     break;
                 case 1:
 
                    ....
 
             }
 
 private void ChangeTextUi()
         {
             
             ClassNametargetvariable.Object1 = EditorGUILayout.ObjectField("Text Object Old", classnametargetvariable .Object1 as Text, typeof(Text), true, options);
             classnametargetvariable .Object2 = EditorGUILayout.ObjectField("Text Object New", classnametargetvariable .Object2 as Text, typeof(Text), true, options);
 
         }
         }

While the dynamic aspect of the custom inspector works the functionality does not. I get nullreference exceptions. Has anyone an Idea how to solve this ?

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Modifying default Inspector GUI creator 3 Answers

Distribute terrain in zones 3 Answers

InspectorEditor: Find target object that has focus 1 Answer

Problem using EditorGUILayout.ObjectField with custom type 1 Answer

Multiple Cars not working 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