Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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:
    322
    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:
    1,935
    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.