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
0
Question by Chocolade · Jul 24, 2017 at 11:49 AM · c#scripting problemscript.

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);
     }
 }

Comment
Add comment · Show 1
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 Umresh · Jul 24, 2017 at 12:13 PM 1
Share

http://answers.unity3d.com/comments/1005665/view.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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






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

375 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 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 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 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 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 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

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


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