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

Enable class elements

Discussion in 'Scripting' started by Ben Norman, Oct 20, 2015.

  1. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    Hi people of Unity.

    In line 44, I want to make "GameObject inside" and "float relatieveFactorInside" visible (enabled) in the inspector for ONLY the elements where the bool tube is checked on ( when tube == true).
    They are both elements from the class ModelInfo.

    I want to try to read the data from a tube. The tube is build with 2 cylinders. 1 for the outside and 1 for the empty inside.
    In my code, you need to drag the outside cylinder into the "element" gameobject and the inside cylinder in the "inside" gameobject that will become visable when you press the "tube" checkbox in the inspector. (look image)

    Can somebody help me to make to make the float and gameobject visible.
    Thanks for your time.

    Here is my code:
    (I still need to change some values in the function "Correctie" << case "Cylinder", but i need "GameObject inside" and "float relatieveFactorInside" for it)
    the important lines are around line 10 and line 44.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using UnityEditor;
    4. using System.Collections.Generic;   //For list
    5.  
    6. //Registratie van de verschillende elementen waaruit het model is gemaakt.
    7. [System.Serializable]
    8. public class ModelInfo
    9. {
    10.     public GameObject element;
    11.     public float relatieveFactor;
    12.     public bool tube;
    13.     private GameObject inside;  //For Tube
    14.     private float relatieveFactorInside; //For Tube
    15. }
    16.  
    17. public class GetTransform : MonoBehaviour
    18. {
    19.     public List<ModelInfo> objecten;
    20.     private ushort teller = 1;  //Voor volumenummer toe te wijzen aan de verschillende objecten.
    21.  
    22.     private GameObject buffer;  //for tube
    23.  
    24.     void Start()
    25.     {
    26.         //buffer.disabled;
    27.         Debug.Log("Druk de \"p\" toets in nadat u de verschillende elementen met hun respectievelijke relatieve factor hebt ingegeven.");
    28.     }
    29.  
    30.    void Update()
    31.     {
    32.         //Als men op de "p" toets drukt zullen de gegevens worden overgehaald.
    33.         if (Input.GetKeyDown("p"))
    34.         {
    35.             //Om ieder gameobject in de lijst aan beurt te laten komen.
    36.             for (int i = 0; i < objecten.Count; i++)
    37.             {
    38.                 buffer = GetComponent<GameObject>();    //Make buffer empty for new line in console/file
    39.                 //When Tube!!   AANPASSEN!!!!!
    40.                 if (objecten[i].tube == true)
    41.                 {
    42.                     Debug.Log(objecten[i].element.name + " " + objecten[i].tube);
    43.                  
    44.                     //Inside + relatieveFactorInside + Material from Inside need to become visable for this element so the gameobject can be dragged on it in the inspector.
    45.                  
    46.                     buffer = objecten[i].element;   //Tube gameobject in buffer (for correctie coördinaten)
    47.                 }
    48.  
    49.                 //defaultwaarde 1 instellen voor als relatieve factor niet is ingevuld door de gebruiken.
    50.                 if (objecten[i].relatieveFactor == 0)
    51.                 {
    52.                     objecten[i].relatieveFactor = 1.0f;
    53.                 }
    54.                 WriteFile(objecten[i].element,objecten[i].relatieveFactor);
    55.                 teller++;
    56.             }
    57.         }
    58.     }
    59.  
    60.     void WriteFile(GameObject obj,float relatieveF)
    61.     {
    62.         //Type mesh. Hierdoor kunnen we zien of het object een cube, cylinder,.. of dergelijk is. (Gebruiken we bij de correctie op de coördinaten)
    63.         Mesh objectType = obj.GetComponent<MeshFilter>().mesh;
    64.         int index1 = objectType.name.IndexOf(" ");
    65.         string meshType = objectType.name.Substring(0, index1);
    66.  
    67.         //name of material + dichtheid in g/cm³
    68.         Material input = obj.GetComponent<MeshRenderer>().material;
    69.         int index2 = input.name.IndexOf(" ");
    70.         string materiaal = input.name.Substring(0, index2);
    71.  
    72.         //Geprinte regel met correctie afh van type mesh ingebouwd in de correctie. (zie funtie "Correctie")
    73.         string transformatie = Correctie(obj, meshType, obj.transform.position.x, obj.transform.position.y, obj.transform.position.z);
    74.         string text = teller + " " + obj.name + " " + obj.tag + " " + transformatie + " " + relatieveF + " " + materiaal + Environment.NewLine;
    75.         //Schaal moet misschien nog gewijzigd worden naar een betere units/cm verhouding.
    76.         //Vindt de naam van het object:         Debug.Log(obj.name);
    77.         //Vindt de tag (groep) van het object:  Debug.Log(obj.tag);
    78.  
    79.         Debug.Log(text);
    80.  
    81.         //Later, write to txt file.
    82.         /*System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\Users\\Ben!\\Documents\\tmp\\test.txt");
    83.         file.WriteLine(text);
    84.         file.Close();   //Kan na de foreach loop komen (dan zijn alle lijnen in het txt bestand geschreven    */
    85.     }
    86.  
    87.     //Om correctie afhankelijk te maken van het type mesh (cube, cylinder,..)
    88.     string Correctie(GameObject obj, string meshType, float x, float y, float z)
    89.     {
    90.         switch (meshType)
    91.         {
    92.             case "Cube":
    93.                 //transformatiegegevens cube: Dit zijn de coördinaten van een hoekpunt.
    94.                 float xCubeV = z - (obj.transform.lossyScale.z / 2);  //Omzetting coördinaten van centerpunt naar hoekpunt.
    95.                 float yCubeV = x - (obj.transform.lossyScale.x / 2);
    96.                 float zCubeV = y - (obj.transform.lossyScale.y / 2);
    97.                 string positieCube = xCubeV + " " + yCubeV + " " + zCubeV;
    98.                 string rotatieCube = obj.transform.rotation.eulerAngles.x + " " + obj.transform.rotation.eulerAngles.y + " " + obj.transform.rotation.eulerAngles.z;
    99.                 string schaalCube = obj.transform.lossyScale.x + " " + obj.transform.lossyScale.y + " " + obj.transform.lossyScale.z;
    100.                 string cube = positieCube + " " + schaalCube + " " + rotatieCube;
    101.                 return cube;
    102.             case "Cylinder":
    103.                 if (objecten[teller - 1].tube == true)
    104.                 {   //CHANGE VALUES FOR TUBE!!!!! dikte van wand is (buffer-inside)/??2??
    105.                     //transformatiegegevens cylinder: Dit zijn de coördinaten van het centerpunt vh onderste oppervlak.
    106.                     float xCylV = z;  //Omzetting coördinaten van centerpunt naar hoekpunt.
    107.                     float yCylV = x;
    108.                     float zCylV = y - buffer.transform.lossyScale.y;
    109.                     string positieTube = xCylV + " " + yCylV + " " + zCylV;
    110.                     string rotatieTube = buffer.transform.rotation.eulerAngles.x + " " + buffer.transform.rotation.eulerAngles.y + " " + buffer.transform.rotation.eulerAngles.z;
    111.                     string schaalTube = buffer.transform.lossyScale.x + " 0 " + (buffer.transform.lossyScale.y * 2);
    112.                     //buitenstraal + binnenstraal (voor tube; bij cilinder = 0) + hoogte
    113.                     string tube = positieTube + " " + schaalTube + " " + rotatieTube;
    114.                     return tube;
    115.                 }
    116.                 else
    117.                 {
    118.                     //transformatiegegevens cylinder: Dit zijn de coördinaten van het centerpunt vh onderste oppervlak.
    119.                     float xCylV = z;  //Omzetting coördinaten van centerpunt naar hoekpunt.
    120.                     float yCylV = x;
    121.                     float zCylV = y - obj.transform.lossyScale.y;
    122.                     string positieCyl = xCylV + " " + yCylV + " " + zCylV;
    123.                     string rotatieCyl = obj.transform.rotation.eulerAngles.x + " " + obj.transform.rotation.eulerAngles.y + " " + obj.transform.rotation.eulerAngles.z;
    124.                     string schaalCyl = obj.transform.lossyScale.x + " 0 " + (obj.transform.lossyScale.y * 2);
    125.                     //buitenstraal + binnenstraal (voor tube; bij cilinder = 0) + hoogte
    126.                     string cyl = positieCyl + " " + schaalCyl + " " + rotatieCyl;
    127.                     return cyl;
    128.                 }
    129.             default:
    130.                 return "fout";  //aanpassen naar defaultwaarde
    131.         }
    132.     }
    133. }
    Image is in this link:
    https://www.dropbox.com/s/sv7u1y1vhkeg5lj/Enable case elements.png?dl=0
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Omg, stop with the Dutch in code, especially the mixing it with English.... very, very bad practice. :(

    Then to the next question. Why don't you just have them always visible? If you are worried about it cluttering the view for when there is no "inside" then you could put those elements in their own class so that you can collapse the collection of elements for each object.

    As for how you could make what you want work, you'll have to make your own custom inspector:
    http://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector
     
  3. Ben Norman

    Ben Norman

    Joined:
    Sep 8, 2015
    Posts:
    9
    Sorry for the Dutch and English language mix. I wrote all my commentary in Dutch so I changed some important comments in English befor I placed it here. :s

    I was also thinking about the option to make 2 classes. That will probably be the best.
    And I saw that tutorial several times befor, but I don't get it. It seems like my brain can't accept it :)

    I'll try some stuff. If it doesn't work, i'll write it here and if it works, i'll write the code here.

    Thanks for your time, effort and different vieuw on my problem :)