void SaveCGList()
        {
            //check if cg list is empty
            if (CGCount <= 0)
            {
                EditorUtility.DisplayDialog("OOOOOps", "No CG Create :(", "OK");
                return;
            }

            for (int i = 0; i < CGCount; i++)
            {
                var CGInfo = CGList[i];
                //create CG
                if (CGInfo.CG != null)
                {
                    //get asset path
                    string directory = AssetDatabase.GetAssetPath(CGInfo.CG);
                    //save path
                    string path = ValueManager.CGPath + CGInfo.Name + ".jpg";

                    //if CG already existe, replace the cg
                    if (AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) != null)
                    {
                        //replace if cg has changed
                        if (directory != path)
                        {
                            FileUtil.ReplaceFile(directory, path);
                        }
                    }
                    else
                    {
                        //get old cg
                        string oldPath = ValueManager.CGPath + CGInfo.OldName + ".jpg";
                        var    oldCg   = AssetDatabase.LoadAssetAtPath(oldPath, typeof(Sprite)) as Sprite;
                        //change oldCg name if cg already existed
                        if (oldCg != null)
                        {
                            System.IO.File.Move(ValueManager.CGFullPath + CGInfo.OldName + ".jpg", ValueManager.CGFullPath + CGInfo.Name + ".jpg");
                        }
                        else
                        {
                            //save new cg
                            FileUtil.CopyFileOrDirectory(directory, path);
                        }
                    }
                }
            }

            //Delete removed cgs from list
            //get all cgs name
            var existeCGList = ObjectInfoHelper.GetCGsName();
            //delete list
            var deleteCGList = ObjectInfoHelper.GetCGsName();

            foreach (var name in existeCGList)
            {
                //if cg exite in cg manager ,remove it from delete list
                foreach (var cg in CGList)
                {
                    if (name == cg.Name)
                    {
                        deleteCGList.Remove(name);
                    }
                }
            }

            //delete removed cgs
            foreach (var name in deleteCGList)
            {
                string path   = "Assets/GameSources/CGs/" + name + ".jpg";
                var    sprite = AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) as Sprite;
                if (sprite != null)
                {
                    FileUtil.DeleteFileOrDirectory(path);
                }
            }

            EditorUtility.DisplayDialog("WOAH!!", "CGs saved!!", "OK");
        }
        private void OnGUI()
        {
            //import cg list
            if (RefreshCGList)
            {
                //refresh list
                CGCount = 0;
                CGList  = new List <CGItem>();

                //find all cg names
                var list = ObjectInfoHelper.GetCGsName();
                foreach (var name in list)
                {
                    CGCount += 1;
                    //get cg sprite
                    string path   = ValueManager.CGPath + name + ".jpg";
                    var    sprite = AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) as Sprite;
                    CGList.Add(new CGItem {
                        OldName = name, Name = name, CG = sprite
                    });
                }
                RefreshCGList = false;
            }
            //refresh cg list
            if (GUILayout.Button("Refresh CG list"))
            {
                RefreshCGList = true;
            }

            //cg count
            GUILayout.Label("CG count: " + CGCount, GUILayout.Height(15));
            Scroller = GUILayout.BeginScrollView(Scroller);
            for (int i = 0; i < CGCount; i++)
            {
                var myCG = CGList[i];
                GUILayout.BeginHorizontal("box");

                GUILayout.BeginVertical();
                //auto name if string is empty
                if (string.IsNullOrEmpty(myCG.Name))
                {
                    myCG.Name = "cg_" + i;
                }

                myCG.Name = EditorGUILayout.TextField("CG name:", myCG.Name);
                //body sprite
                myCG.CG = EditorGUILayout.ObjectField("CG", myCG.CG, typeof(Sprite), true) as Sprite;
                GUILayout.EndVertical();

                GUILayout.Space(10);
                if (GUILayout.Button("x", GUILayout.Width(20)))
                {
                    CGList.RemoveAt(i);
                    CGCount -= 1;
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndScrollView();

            //add new cg
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("+"))
            {
                CGList.Add(new CGItem());
                CGCount += 1;
            }
            if (GUILayout.Button("-") && CGCount > 0)
            {
                CGCount -= 1;
                CGList.RemoveAt(CGCount);
            }
            GUILayout.EndHorizontal();
            //auto rename cgs
            if (GUILayout.Button("Auto rename"))
            {
                RenameCGs();
            }

            GUILayout.Space(5);
            GUILayout.BeginHorizontal(GUILayout.Height(30));
            GUILayout.Space(5);
            //save cgs
            if (GUILayout.Button("Save", GUILayout.Height(25)))
            {
                SaveCGList();
            }
            //reload cgs
            if (GUILayout.Button("Discard", GUILayout.Height(25)))
            {
                RefreshCGList = true;
            }
            GUILayout.Space(5);
            GUILayout.EndHorizontal();
        }
        public override void Draw()
        {
            GUILayout.BeginArea(myRect, Title, Style);
            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            GUILayout.Space(SpacePixel);
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();
            GUILayout.Space(SpacePixel);

            //get all cg
            var list = ObjectInfoHelper.GetCGsName();

            if (Initialize)
            {
                //find origin object
                var origin = AssetDatabase.LoadAssetAtPath(Path, typeof(Sprite)) as Sprite;
                if (origin != null)
                {
                    //set index
                    Index = list.IndexOf(list.Where(c => c == origin.name).FirstOrDefault());
                }
                Initialize = false;
            }

            //selector for cg
            GUILayout.BeginHorizontal();
            GUILayout.Label("CG", WhiteTxtStyle, GUILayout.Width(LabelWidth));
            Index = EditorGUILayout.Popup(Index, list.ToArray());
            GUILayout.EndHorizontal();
            //set cg path
            Path = ValueManager.CGPath + list[Index] + ".jpg";

            //is wait for CG appear
            GUILayout.BeginHorizontal();
            GUILayout.Label("Is wait", WhiteTxtStyle, GUILayout.Width(LabelWidth));
            IsWait = EditorGUILayout.Toggle(IsWait);
            GUILayout.EndHorizontal();

            //GUILayout.BeginHorizontal();
            //GUILayout.FlexibleSpace();
            //load preview cg
            string path       = ValueManager.CGPath + list[Index] + ".jpg";
            var    imgPriveiw = AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)) as Sprite;

            if (imgPriveiw != null)
            {
                GUILayout.Label(imgPriveiw.texture, GUILayout.Width(200), GUILayout.Height(113));
            }
            //GUILayout.EndHorizontal();

            GUILayout.EndVertical();
            GUILayout.Space(SpacePixel);
            GUILayout.EndHorizontal();
            GUILayout.EndArea();

            InPoint.Draw();
            OutPoint.Draw();

            base.Draw();
        }