Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Reading and Writing to Inspector Values via Editor Script

Discussion in 'Scripting' started by chees502, Feb 25, 2015.

  1. chees502

    chees502

    Joined:
    Jan 25, 2014
    Posts:
    22
    I am needing to create a system that allows me to take snapshots of the public values that the inspector currently has. Assuming somthing like this gameObject.component.inspectorValue[0];

    What I will make is a system to look through all the GameObjects, all of their components, and then all of the values those components have.

    I am aware I could hard code it, and have a condition to read out all of the Transform component data, one for Light data, and one for ArbitraryScriptX. but I need this system to be pretty dynamic.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Well, you would mainly just need GetComponent for that. Put all the GameObjects in an array and do a for each loop through them and check the values, or you can use a tag and use GameObjectsWithTag("atag"); That returns an array, but would use more computation.
     
  3. chees502

    chees502

    Joined:
    Jan 25, 2014
    Posts:
    22
    Not quite what I was after, I figured out that I am able to use
    Code (CSharp):
    1.  
    2.         System.Type tempType = obj.GetType();
    3.         FieldInfo[] tempFields = tempType.GetFields();
    4.         foreach (FieldInfo field in tempFields){
    5.             if(!field.IsStatic){
    6.                 Debug.Log(field.FieldType + ": "+field.Name+"= "+field.GetValue(obj));
    7.                 // System.String: myString = My Custom Inspector Value!!
    8.             }
    9.         }
    obj is the component I am getting, and I am grabbing the type from it. I then loop through all the Fields in it. I check to see if the field is static, because it will give a false positive on static, (type field is more public than inspector value)