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
3
Question by haruna9x · Jul 31, 2017 at 09:50 AM · c#variablecustom-editorprivate

How to keep private variables, while being able to access them in the custom EditorWindow.

I am making an editor for character stats. However, I would be forced to set the variables to public, or at least internal. How can I keep them private.

I am working here:

  public class EntityDatabaseEditor : EditorWindow
     {
  [MenuItem("Window/GameSystems/EntityDatabase")]
         static void Init()
         {
             EditorWindow window = GetWindow(typeof(EntityDatabaseEditor));
             window.minSize = new Vector2(600, 400);
             window.Show();
         }
 }
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
4
Best Answer

Answer by haruna9x · Aug 29, 2017 at 05:00 PM

I was able to access private members by Reflection:

 public static class EditorReflection
     {
         /// <summary>
         /// Returns a _private_ Property Value from a given Object. Uses Reflection.
         /// Throws a ArgumentOutOfRangeException if the Property is not found.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is returned</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <returns>PropertyValue</returns>
         public static T GetProperty<T>(this object obj, string propName)
         {
             if (obj == null) throw new ArgumentNullException("obj");
             PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
             if (pi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
             return (T)pi.GetValue(obj, null);
         }
 
         /// <summary>
         /// Returns a private Property Value from a given Object. Uses Reflection.
         /// Throws a ArgumentOutOfRangeException if the Property is not found.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is returned</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <returns>PropertyValue</returns>
         public static T GetField<T>(this object obj, string propName)
         {
             if (obj == null) throw new ArgumentNullException("obj");
             Type t = obj.GetType();
             FieldInfo fi = null;
             while (fi == null && t != null)
             {
                 fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                 t = t.BaseType;
             }
             if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
             return (T)fi.GetValue(obj);
         }
 
         /// <summary>
         /// Sets a _private_ Property Value from a given Object. Uses Reflection.
         /// Throws a ArgumentOutOfRangeException if the Property is not found.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is set</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <param name="val">Value to set.</param>
         /// <returns>PropertyValue</returns>
         public static void SetProperty<T>(this object obj, string propName, T val)
         {
             Type t = obj.GetType();
             if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null)
                 throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
             t.InvokeMember(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { val });
         }
 
         /// <summary>
         /// Set a private Property Value on a given Object. Uses Reflection.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is returned</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <param name="val">the value to set</param>
         /// <exception cref="ArgumentOutOfRangeException">if the Property is not found</exception>
         public static void SetField<T>(this object obj, string propName, T val)
         {
             if (obj == null) throw new ArgumentNullException("obj");
             Type t = obj.GetType();
             FieldInfo fi = null;
             while (fi == null && t != null)
             {
                 fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                 t = t.BaseType;
             }
             if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
             fi.SetValue(obj, val);
         }
 
         /// <summary>
         /// Get name a property as a string
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="propertyLambda">Lambda of the form: '() => Class.Property' or '() => object.Property'</param>
         /// <returns></returns>
         public static string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
         {
             var me = propertyLambda.Body as MemberExpression;
 
             if (me == null)
             {
                 throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
             }
 
             return me.Member.Name;
         }
     }


Example:

 EditorReflection.SetProperty(item, EditorReflection.GetPropertyName(() => item.Name), 
                                              EditorGUILayout.TextField("Name", item.Name));


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 ShadyProductions · Jul 31, 2017 at 10:04 AM

Add [SerializeField] above it.

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

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

71 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

Related Questions

When issuing to print a private variable from the script, the script prints all the variables attached to objects using the same script instead of the specific object. How do I fix this? 3 Answers

C# - public variable not exposed to the Project Panel? (n00b to c#) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Modifying prefab's private variables with properties from values of another script. 2 Answers


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