Search Unity

The CinemachineImpulseDefinition can not work for private field with SerializeField.

Discussion in 'Cinemachine' started by watsonsong, Aug 3, 2018.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I use the CinemachineImpulseDefinition in my own script to simulate the camera shake.
    I write it like this:
    Code (CSharp):
    1.  
    2. [SerializeField]
    3. [CinemachineImpulseDefinitionProperty]
    4. public CinemachineImpulseDefinition impulseDefinition =
    5.     new CinemachineImpulseDefinition();
    6.  
    I want to change the public to private, because there is no need to access the impulseDefinition outside, but the editor inspector show a lot of error when I switch to private field.

    I see the EmbeddedAssetPropertyDrawer.cs may not support the private field:
    Code (CSharp):
    1.  
    2. Type EmbeddedAssetType(SerializedProperty property)
    3. {
    4.     Type type = property.serializedObject.targetObject.GetType();
    5.     var a = property.propertyPath.Split('.');
    6.     for (int i = 0; i < a.Length; ++i)
    7.         type = type.GetField(a[i]).FieldType;
    8.         return type;
    9. }
    10.  
    It use GetField but it may not work with private field.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Yes, you are right, it only works with public fields. To fix it, the code should be
    Code (CSharp):
    1.         Type EmbeddedAssetType(SerializedProperty property)
    2.         {
    3.             Type type = property.serializedObject.targetObject.GetType();
    4.             var a = property.propertyPath.Split('.');
    5.             for (int i = 0; i < a.Length; ++i)
    6.                 type = type.GetField(a[i],
    7.                     System.Reflection.BindingFlags.Public
    8.                     | System.Reflection.BindingFlags.NonPublic
    9.                     | System.Reflection.BindingFlags.Instance).FieldType;
    10.             return type;
    11.         }
    Thanks for letting us know, we'll include a fix in the next release.