Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    266
    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,408
    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.