Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 mark-gehan · Feb 10, 2013 at 05:26 AM · c#editorwindowreflection

How can you use Reflection to create an object editor window

I created an editor window control in Unity to easily edit the PlayerInfo object in my game without changing code. You can edit the object in the window and save it out a serialized file that the game can read. I was originally doing this manually by adding each field to the control when I edited the PlayerInfo object. I would like to use Reflection to have this done automatically when the PlayerInfo object changes. However, my attempt to do this causes Unity to crash (not the script or my game, Unity itself crashes). This is what I have been trying to do...

     Type type = typeof(PlayerInfo);
     
     var fields = type.GetFields();
             foreach(FieldInfo field in fields)
             {
                 object temp = field.GetValue(playerInfo);
                 if(temp is string)
                 {
                     field.SetValue(EditorGUILayout.TextField(field.Name + ": ", (string)temp), (string)temp);    
                 }
                 else if(temp is int)
                 {
                     field.SetValue(EditorGUILayout.IntSlider(field.Name + ": ", (int)temp, -1, 100),(int)temp);
                 }
                 else if(temp is float)
                 {
                     field.SetValue(EditorGUILayout.Slider(field.Name + ": ", (float)temp, 0f, 200f), (float)temp);
                 }
               }
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

1 Reply

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

Answer by Bunny83 · Feb 10, 2013 at 06:36 AM

Just take a look at the SetValue function. It takes two parameters.

The first one is the object instance of which you want to set the field. If the field is a static field you would pass null. However in your case it's an instance field so you have to pass your object reference.

The second parameter is the actual value you want to assign to the field. Here you should pass the "changed value". Since the sliders / TextField will return the changed value, this is what you want to pass

 field.SetValue(playerInfo ,EditorGUILayout.TextField(field.Name + ": ", (string)temp));  

What you've done is you tried to set the field on the boxed string / int / float which is returned by the TextField / slider function. Of course they are not actual reference types and are not of type PlayerInfo.

Last thing is: There's no difference between Unity, your game and your script. They all run in the same Mono Environment inside the editor. If any part crashes the whole system will crash.

This line will make the editor and your game crash as well:

 while(true){ }

Comment
Add comment · Show 1 · 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 mark-gehan · Feb 16, 2013 at 11:52 PM 0
Share

Worked great. Thanks!

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

10 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

Related Questions

How to open an Editor Window by using the MethodInfo.Invoke function ? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Reflecting Objects off of Boundaries/Walls 2 Answers

Why are the reflected bullets in my 2D game acting strange? 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