- Home /
How can i rename a bool variable in inspector according to false/true mode ?
The variable globalRotation: public bool globalRotation = false; In the inspector i see all the time the name Global Rotation but i want to do somehow that when the Global Rotation is checked(true) change the name in the inspector to Global Rotation. When it's unchecked(false) change the name to Individual Rotation. It's working fine like that but i think it will be more nice to change the bool name according to it's mode too and not only the state(false/true).
I want to know when the settings of Global Rotation are on in use change the bool variable name to Global Rotation when the mode when settings are on for the Individual Rotation change the bool variable name to Individual Rotation.
In general how to change the bool variable name in the inspector according to it's state: true/false ?
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
[Header("Global Rotation")]
[Space(5)]
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public GameObject[] allObjects;
[Space(5)]
[Header("Rotation Mode")]
public bool globalRotation = false;
[Header("Individual Rotation")]
[Space(3)]
public SpinableObject[] individualObjects;
private void Start()
{
allObjects = GameObject.FindGameObjectsWithTag("Propeller");
}
// Update is called once per frame
void Update()
{
if (globalRotation == false)
{
foreach (var spinner in individualObjects)
spinner.RotateObject();
}
else
{
for (int i = 0; i < allObjects.Length; i++)
{
RotateAllObjects(allObjects[i].transform);
}
}
}
private void RotateAllObjects(Transform t)
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
Answer by andrew-lukasik · Jul 24, 2017 at 01:06 PM
SOLUTION A
Write small PropertyAttribte to change labels for bool fields:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class LabeledBool : PropertyAttribute
{
public string labelWhenTrue;
public string labelWhenFalse;
public LabeledBool ( string labelWhenTrue , string labelWhenFalse )
{
this.labelWhenTrue = labelWhenTrue;
this.labelWhenFalse = labelWhenFalse;
}
#if UNITY_EDITOR
[CustomPropertyDrawer( typeof(LabeledBool) )]
public class ThisPropertyDrawer : PropertyDrawer
{
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
LabeledBool propertyAttribute = this.attribute as LabeledBool;
label.text = property.boolValue ? propertyAttribute.labelWhenTrue : propertyAttribute.labelWhenFalse;
EditorGUI.PropertyField( position , property , label );
}
}
#endif
}
@Chocolade It's very simple to use too :
public class MyMonoBehaviour : MonoBehaviour
{
[LabeledBool( "Global Rotation" , "Individual Rotation" )]
[SerializeField]
bool _rotationMode = true;
}
SOLUTION B
By creating new struct RotationMode and writing desired PropertyDrawer for it:
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[System.Serializable]
public struct RotationMode
{
#region FIELDS
[SerializeField] int _value;
#endregion
#region OPERATORS
public RotationMode ( int value )
{
this._value = value;
}
public RotationMode ( bool value )
{
this._value = value ? 1 : 0;
}
// add cast to/from int:
public static implicit operator int ( RotationMode o )
{
return o._value;
}
public static implicit operator RotationMode ( int o )
{
return new RotationMode( o );
}
// add cast to/from bool:
public static implicit operator bool ( RotationMode o )
{
return o._value==1 ? true : false;
}
public static implicit operator RotationMode ( bool o )
{
return new RotationMode( o );
}
#endregion
}
#if UNITY_EDITOR
[CustomPropertyDrawer( typeof(RotationMode) )]
public class RotationMode_PropertyDrawer : PropertyDrawer
{
const string labelTrue = "Global Rotation";
const string labelFalse = "Individual Rotation";
public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
{
EditorGUI.BeginProperty( position , label , property );
{
EditorGUI.BeginChangeCheck();
SerializedProperty relativeProperty_value = property.FindPropertyRelative( "_value" );
switch( relativeProperty_value.intValue )
{
case 0:
label.text = labelFalse;
break;
case 1:
label.text = labelTrue;
break;
default:
label.text = "value not implemented";
break;
}
EditorGUI.IntSlider( position , relativeProperty_value , 0 , 1 , label );
if( EditorGUI.EndChangeCheck() )
{
property.serializedObject.ApplyModifiedProperties();
}
}
EditorGUI.EndProperty();
}
}
#endif
Your answer
![](https://koobas.hobune.stream/wayback/20220612131027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers