/// <summary>
        /// 通过name转换颜色
        /// </summary>
        /// <param name="chartName"></param>
        public void SwapColors(string chartName)
        {
            if (string.IsNullOrEmpty(chartName) || mainTexture == null || srcChart == null || swapCharts == null)
            {
                return;
            }

            // 如果是源颜色组的name,清除颜色,否则转换
            if (chartName == srcChart.name)
            {
                ClearSwapColors();
            }
            else
            {
                ColorChart chart = swapCharts.Find(c => c.name == chartName);
                if (chart != null)
                {
                    Texture2D swapTexture;
                    if (chart.TryGetSwapTexture(mainTexture, srcChart, out swapTexture))
                    {
                        SetMaterialPropertyBlockTexture(swapTexture);
                    }
                }
            }
        }
        private void OnGUI()
        {
            // 设置src与swap
            src  = EditorGUILayout.ObjectField("Source Chart", src, typeof(ColorChart), false) as ColorChart;
            swap = EditorGUILayout.ObjectField("Swap Chart", swap, typeof(ColorChart), false) as ColorChart;
            if (src == null || swap == null)
            {
                EditorGUILayout.HelpBox("Please select source chart and swap chart.", MessageType.Info);
                return;
            }

            // 设置Color的数量
            EditorGUI.BeginChangeCheck();
            int count = Mathf.Max(0, EditorGUILayout.DelayedIntField("Color Count", src.count));

            if (EditorGUI.EndChangeCheck() && src.count != count)
            {
                src.count = count;
                EditorUtility.SetDirty(src);
            }

            if (swap.count != src.count)
            {
                int swapCount = swap.count;
                swap.count = src.count;
                if (swapCount < src.count)
                {
                    ColorChart.CopyColors(src, swapCount, swap, swapCount, src.count - swapCount);
                }
                EditorUtility.SetDirty(swap);
            }

            EditorGUILayout.Space();

            // 设置颜色
            EditorGUI.BeginChangeCheck();
            m_Scroll = EditorGUILayout.BeginScrollView(m_Scroll, "box");
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Index", GUILayout.MaxWidth(50));
                EditorGUILayout.LabelField("Source Chart", GUILayout.MaxWidth(150));
                EditorGUILayout.LabelField("Swap Chart", GUILayout.MaxWidth(150));
                EditorGUILayout.EndHorizontal();
                for (int i = 0; i < count; i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(i.ToString(), GUILayout.MaxWidth(50));
                    src[i]  = EditorGUILayout.ColorField(src[i], GUILayout.MaxWidth(150));
                    swap[i] = EditorGUILayout.ColorField(swap[i], GUILayout.MaxWidth(150));
                    EditorGUILayout.EndHorizontal();
                }
            }
            EditorGUILayout.EndScrollView();
            if (EditorGUI.EndChangeCheck())
            {
                EditorUtility.SetDirty(src);
                EditorUtility.SetDirty(swap);
            }
        }
        /// <summary>
        /// 获取转换后的Texture2D
        /// </summary>
        /// <param name="srcTexture"></param>
        /// <param name="srcChart"></param>
        /// <returns></returns>
        public Texture2D GetSwapTexture(Texture2D srcTexture, ColorChart srcChart)
        {
            Texture2D swapTexture;

            if (!TryGetSwapTexture(srcTexture, srcChart, out swapTexture))
            {
                return(null);
            }
            return(swapTexture);
        }
        /// <summary>
        /// 尝试获取转换后的Texture2D
        /// </summary>
        /// <param name="srcTexture"></param>
        /// <param name="srcChart"></param>
        /// <param name="swappingTexture"></param>
        /// <returns></returns>
        public bool TryGetSwapTexture(Texture2D srcTexture, ColorChart srcChart, out Texture2D swapTexture)
        {
            swapTexture = null;
            if (srcTexture == null || srcChart == null)
            {
                return(false);
            }

            // 动态创建没有命名,或动态把名称更改成了空。
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(srcTexture.name))
            {
                Debug.LogError("Name of SwapChart or Name of SrcTexture is null or empty.");
                return(false);
            }

            // 尝试获取缓存Texture2D
            if (SwapTextureCache.TryGetTexture2D(name, srcTexture.name, out swapTexture))
            {
                return(true);
            }

            // 获取源图所有颜色,并转换颜色
            Color[] colors = srcTexture.GetPixels();
            for (int i = 0; i < colors.Length; i++)
            {
                if (colors[i].a != 0)
                {
                    colors[i] = GetSwapColor(colors[i], srcChart);
                }
            }

            // 和源相同的设置创建Texture2D
            Texture2D clone = new Texture2D(srcTexture.width, srcTexture.height)
            {
                alphaIsTransparency = srcTexture.alphaIsTransparency,
                wrapMode            = srcTexture.wrapMode,
                filterMode          = srcTexture.filterMode,
            };

            // 填充转换后的颜色,并保存
            clone.SetPixels(colors);
            clone.Apply();

            /// 如果加入缓存失败,销毁swapTexture,
            /// 重新尝试获取,
            /// 异步执行有极小的可能发生。
            if (!SwapTextureCache.AddTexture2D(name, srcTexture.name, clone))
            {
                Texture2D.Destroy(clone);
                return(TryGetSwapTexture(srcTexture, srcChart, out swapTexture));
            }

            swapTexture = clone;
            return(true);
        }
        /// <summary>
        /// 获取在本表中的对应位置的颜色,如果没有找到,返回原颜色
        /// </summary>
        /// <param name="color"></param>
        /// <param name="srcChart"></param>
        /// <returns></returns>
        public Color GetSwapColor(Color color, ColorChart srcChart)
        {
            if (srcChart == null)
            {
                return(color);
            }

            int index = srcChart.IndexOf(color);

            if (index == -1 || index >= m_Colors.Length)
            {
                return(color);
            }

            return(m_Colors[index]);
        }
        /// <summary>
        /// 从Texture中读取颜色
        /// </summary>
        /// <param name="chart"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static bool LoadColorsFromTexture(ColorChart chart, Texture2D texture)
        {
            if (chart == null || texture == null)
            {
                return(false);
            }

            Color[]      colors = texture.GetPixels();
            List <Color> list   = new List <Color>();

            for (int i = 0; i < colors.Length; i++)
            {
                if (colors[i].a != 0 && !list.Contains(colors[i]))
                {
                    list.Add(colors[i]);
                }
            }
            chart.m_Colors = list.ToArray();
            return(true);
        }
        /// <summary>
        /// 克隆一个颜色表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T Clone <T>(string newName) where T : ColorChart
        {
            if (string.IsNullOrEmpty(newName))
            {
                Debug.LogError("Name can not be null or empty.");
                return(null);
            }

            if (newName == name)
            {
                Debug.LogError("Duplicated Name!");
                return(null);
            }

            T clone = ColorChart.CreateInstance <T>();

            clone.name = newName;
            clone.SetColors(m_Colors);
            return(clone);
        }
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            EditorGUILayout.Space();
            if (GUILayout.Button("Load Colors From Texture"))
            {
                string path = EditorUtility.OpenFilePanelWithFilters("Create Chart", "Assets", new string[] { "PNG Image", "png", "JPG Image", "jpg", "GIF Image", "gif" });
                if (!string.IsNullOrEmpty(path) && path.Contains(Application.dataPath))
                {
                    path = path.Replace(Application.dataPath, "Assets");
                    Texture2D texture = AssetDatabase.LoadAssetAtPath <Texture2D>(path);
                    ColorChart.LoadColorsFromTexture(chart, texture);
                }
            }

            if (GUILayout.Button("Open Edit Window"))
            {
                ColorChartEditorWindow window = ColorChartEditorWindow.OpenColorChartEditorWindow();
                window.src = chart;
            }
        }
 /// <summary>
 /// 复制颜色
 /// </summary>
 /// <param name="src"></param>
 /// <param name="srcIndex"></param>
 /// <param name="dst"></param>
 /// <param name="dstIndex"></param>
 /// <param name="length"></param>
 public static void CopyColors(ColorChart src, int srcIndex, ColorChart dst, int dstIndex, int length)
 {
     Array.Copy(src.m_Colors, srcIndex, dst.m_Colors, dstIndex, length);
 }