Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Copy/Paste between gradients doesn't work

Discussion in '2022.1 Beta' started by Rowlan, Jan 1, 2022.

  1. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,200
    I created a GradientField with a gradient in an Editor Window and wanted to transfer the gradients between my gradient field and a gradient of the particle system. Problem is: It doesn't work. My guess is that the gradients use different objects for serialization.

    Here's example code:

    Code (CSharp):
    1. using System;
    2. using UnityEditor;
    3. using UnityEditor.UIElements;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class GradientEditor : EditorWindow
    8. {
    9.     [Serializable]
    10.     public class MyData : ScriptableObject
    11.     {
    12.         [SerializeField]
    13.        public Gradient gradient = new Gradient();
    14.     }
    15.  
    16.     [SerializeField]
    17.     private MyData myData;
    18.  
    19.     private SerializedObject so;
    20.     private SerializedProperty gradientProperty;
    21.  
    22.     [MenuItem("Bug Test/Gradient Editor")]
    23.     static void Init()
    24.     {
    25.         EditorWindow editorWindow = GetWindow(typeof(GradientEditor));
    26.         editorWindow.titleContent = new GUIContent("Editor Window");
    27.         editorWindow.Show();
    28.     }
    29.  
    30.     public void CreateGUI()
    31.     {
    32.  
    33.         myData = ScriptableObject.CreateInstance<MyData>();
    34.         so = new SerializedObject(myData);
    35.  
    36.         GradientField gradientField = new GradientField("Gradient");
    37.         gradientField.value = myData.gradient;
    38.         gradientField.bindingPath = nameof(myData.gradient);
    39.         rootVisualElement.Add(gradientField);
    40.  
    41.         // log clipboard
    42.         Button logClipboardButton = new Button() { text = "Log Clipboard" };
    43.         logClipboardButton.clicked += () =>
    44.         {
    45.             var systemClipboard = EditorGUIUtility.systemCopyBuffer;
    46.             Debug.Log("Clipboard content: " + systemClipboard.ToString());
    47.         };
    48.         rootVisualElement.Add(logClipboardButton);
    49.  
    50.         rootVisualElement.Bind(so);
    51.     }
    52.  
    53.     void OnDisable()
    54.     {
    55.         if (myData != null)
    56.         {
    57.             ScriptableObject.DestroyImmediate(myData);
    58.             myData = null;
    59.         }
    60.     }
    61.  
    62. }
    63.  
    The option for copy/paste is there and Copy works:

    cp.png

    Problem is that the clipboard content doesn't match e. g. a Gradient of the Particle System.

    The only way to transfer the gradients I've found so far is store it as Preset in the one dialog and load it as Preset in the other.
     
  2. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,200
    I uploaded the project and created a support ticket: 1391971
     
    richardkettlewell and LeonhardP like this.
  3. vickycl

    vickycl

    Unity Technologies

    Joined:
    Aug 23, 2018
    Posts:
    9
    Hi Rowlan, thank you for reporting this!

    You're quite right that the Gradients in the Particle System are serialized differently to standard Gradients, and that's why the Copy/ Paste didn't work as you expected. We've made an update to the MinMaxGradient type that is used in the Particle System (eg Start Color), adding custom paste options to enable the workflow outlined in your support ticket. You should now be able to paste a Gradient when the MinMaxGradient is in a Gradient mode, to either the max or min value where relevant, or a color when the MinMaxGradient is in Color mode. This change will be released in 2022.2, along with the same custom paste options for MinMaxCurve (eg Start Lifetime).

    In the meantime, although right clicking the text copies or pastes the whole MinMaxGradient field, you can still get to the gradients or colors directly by right clicking the actual data field. Hope this helps!
     
    Rowlan, SugoiDev, LeonhardP and 2 others like this.
  4. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,200
    Thank you very much! By the way, I needed it for something I coded quickly. I'm just pointing you to the free Asset I released because I think it might be beneficial for Unity and I hope Unity would provide something like this out of the box in the future:

    https://assetstore.unity.com/packages/tools/utilities/gradient-picker-210950

    It's really no fuss to get a gradient from anywhere on your desktop easily. I kinda wish Unity would provide that functionality built-in. Getting gradient colors has always been such a pain. But if you can sample them from anywhere on your desktop like my asset does, it's such a time saver. Maybe that's worth considering in the future. That and supporting more than 8 gradient keys :)
     
    vickycl likes this.