I've foldout cache dictionary for all drawn properties, in custom property drawer. When I should clean the cache. Unity C#
Assistant
In Unity with C#, you should clean the cache in a custom property drawer when the underlying serialized property is modified or when the Unity editor is reloaded. This ensures that the cache is updated with the latest values and prevents any inconsistencies.
Here are a few scenarios where you should clean the cache:
If the drawn properties are dependent on other serialized properties that might change and affect their values, such as when a referenced scriptable object is modified or when a value is modified via script.
If the drawn properties rely on external data that might change outside of the Unity editor. For example, if the properties are based on data fetched from a web service or a database, the cache should be cleaned whenever that data is updated.
If the custom property drawer is used in a script that is being modified in the Unity editor, you should clean the cache when the script is modified or when changes are applied in the editor.
To clean the cache, you can implement the GetPropertyHeight or the OnGUI method in your custom property drawer. These methods will be called whenever the drawer is being rendered, providing an opportunity to update the cache.
User
write me code
Assistant
Certainly! Here's an example of how you can implement cache cleaning in a custom property drawer:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MyCustomType))]
public class MyCustomTypePropertyDrawer : PropertyDrawer
{
// Cache dictionary to store drawn properties
private static Dictionary<string, float> cacheDictionary = new Dictionary<string, float>();
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Clean the cache if the serialized property has changed
if (!cacheDictionary.ContainsKey(property.propertyPath))
{
cacheDictionary[property.propertyPath] = property.floatValue;
}
EditorGUI.BeginChangeCheck();
// Draw your custom property GUI here
// For example, you can use EditorGUI.PropertyField to draw the serialized property
EditorGUI.PropertyField(position, property, label);
if (EditorGUI.EndChangeCheck())
{
// Update the cache dictionary when the property value changes
cacheDictionary[property.propertyPath] = property.floatValue;
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// Clean the cache when Unity editor is reloaded
if (!cacheDictionary.ContainsKey(property.propertyPath))
{
cacheDictionary[property.propertyPath] = property.floatValue;
}
// Return the height of your custom property GUI
// For example, you can return EditorGUI.GetPropertyHeight to get the default height
return EditorGUI.GetPropertyHeight(property, label);
}
}
User
I'm not seeing where the dictionary is cleared
Assistant
Apologies for the oversight. You're correct, the code snippet I provided doesn't include clearing the dictionary. Here's an updated version that includes clearing the dictionary when needed:
User
Could you think also other ways when to clear the cache. Like changing the game object selection or something else
Assistant
Certainly! There are several scenarios when you might consider clearing the cache in addition to the editor reload. Some potential situations where you may want to clear the cache include:
Note: If you're using a persistent cache, like a static dictionary, you should ensure that it is properly reset and cleared when needed to avoid any memory leaks or stale data.
In this example, the cache dictionary is used to store the drawn properties, with the property path as the key and the property value as the value. The OnGUI method is responsible for cleaning and updating the cache whenever the serialized property changes. The GetPropertyHeight method cleans the cache and returns the height of your custom property GUI.
Remember to replace MyCustomType with the actual type you are creating a property drawer for.
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MyCustomType))]
public class MyCustomTypePropertyDrawer : PropertyDrawer
{
// Cache dictionary to store drawn properties
private static Dictionary<string, float> cacheDictionary = new Dictionary<string, float>();
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Clean the cache if the serialized property has changed
if (!cacheDictionary.ContainsKey(property.propertyPath))
{
cacheDictionary[property.propertyPath] = property.floatValue;
}
EditorGUI.BeginChangeCheck();
// Draw your custom property GUI here
// For example, you can use EditorGUI.PropertyField to draw the serialized property
EditorGUI.PropertyField(position, property, label);
if (EditorGUI.EndChangeCheck())
{
// Update the cache dictionary when the property value changes
cacheDictionary[property.propertyPath] = property.floatValue;
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// Clean the cache when Unity editor is reloaded
if (!cacheDictionary.ContainsKey(property.propertyPath))
{
cacheDictionary[property.propertyPath] = property.floatValue;
}
// Return the height of your custom property GUI
// For example, you can return EditorGUI.GetPropertyHeight to get the default height
return EditorGUI.GetPropertyHeight(property, label);
}
// Clear the cache dictionary when needed
[InitializeOnLoadMethod]
private static void ClearCacheOnEditorReload()
{
cacheDictionary.Clear();
}
}
In this updated version, a new method ClearCacheOnEditorReload has been added to the MyCustomTypePropertyDrawer class, which will be called when the Unity editor reloads. This method takes advantage of the [InitializeOnLoadMethod] attribute to automatically clear the cache dictionary whenever the editor reloads.
With this addition, the cache dictionary will be cleared during the editor reload, ensuring a clean cache state.
Changing the selected GameObject: If your custom property drawer relies on the properties of a specific GameObject or component, you can clear the cache whenever the active selection changes. You can achieve this by subscribing to the Selection.selectionChanged event in your property drawer. Here's an example:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MyCustomType))]
public class MyCustomTypePropertyDrawer : PropertyDrawer
{
// Cache dictionary to store drawn properties
private static Dictionary<string, float> cacheDictionary = new Dictionary<string, float>();
static MyCustomTypePropertyDrawer()
{
// Subscribe to the selectionChanged event
Selection.selectionChanged += ClearCacheOnSelectionChange;
}
private static void ClearCacheOnSelectionChange()
{
cacheDictionary.Clear();
}
// Rest of the property drawer implementation...
}
Scene changes or reloading: If your custom property drawer relies on properties that are affected by scene changes or reloading, you can clear the cache when the scene changes or is reloaded. You can use the EditorSceneManager.sceneLoaded event or EditorApplication.playModeStateChanged event to listen for scene changes and clear the cache accordingly.
using UnityEditor.SceneManagement;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MyCustomType))]
public class MyCustomTypePropertyDrawer : PropertyDrawer
{
// Cache dictionary to store drawn properties
private static Dictionary<string, float> cacheDictionary = new Dictionary<string, float>();
static MyCustomTypePropertyDrawer()
{
// Subscribe to the scene loaded event
EditorSceneManager.sceneLoaded += ClearCacheOnSceneChange;
}
private static void ClearCacheOnSceneChange(Scene scene, LoadSceneMode mode)
{
cacheDictionary.Clear();
}
// Rest of the property drawer implementation...
}
These are just a couple of examples of additional scenarios where you may want to clear the cache in a custom property drawer. The specific triggering events or actions you decide to use will depend on your specific use case and requirements.