Search Unity

Custom Inspector that calculate percentage of some values

Discussion in 'Editor & General Support' started by Poinball, Jun 2, 2020.

  1. Poinball

    Poinball

    Joined:
    Feb 23, 2016
    Posts:
    42
    Hey I want to show in the Inspector the % value of the result of all the Probability that are added.

    I got it to work but I dont know how to move it beside the Probability slider



    This is what I got right now :



    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using Sirenix.OdinInspector.Editor;
    5.  
    6. [CustomEditor(typeof(MobSpawner))]
    7. public class CustomOdinEditor : OdinEditor
    8. {
    9.     SerializedProperty probability;
    10.  
    11.     public override void OnInspectorGUI()
    12.     {
    13.         base.OnInspectorGUI();
    14.  
    15.         MobSpawner mobSpawner = (MobSpawner)target;
    16.  
    17.  
    18.         foreach (MobSpawn mobSpawn in mobSpawner.mobSpawns)
    19.         {
    20.             float totalProbability = 0;
    21.             foreach (MobLootTable mobLootTable in mobSpawn.mobLootTables)
    22.             {
    23.                 if (mobLootTable.ForceDrop == false)
    24.                 {
    25.                     totalProbability = totalProbability + mobLootTable.probability;
    26.                 }
    27.             }
    28.  
    29.             foreach (MobLootTable mobLootTable in mobSpawn.mobLootTables)
    30.             {
    31.                 if (mobLootTable.ForceDrop == false)
    32.                 {
    33.                     float percentage = Mathf.Ceil(mobLootTable.probability / totalProbability * 100);
    34.                     GUILayout.Label(percentage.ToString() + "%");
    35.                 }
    36.             }
    37.         }
    38.     }
    39. }
    40.  
    Thanks !
     

    Attached Files:

    Last edited: Jun 2, 2020