[Unity] 유니티 커스텀 에디터 _Custom Editor - string to lower.cs

2019. 7. 30. 14:32unity/unity script

it is very simple and useful code example

Extending the code saves time for team members.

 

used for simple and repeated work

this code change all name of animationclip to lowercase

if you need others(game object, prefab ..) just change the filter

this code locate the file name of the animationclip under the target folder and change it to lowercase

many parts overlapped at 'create sprite animation sheet'

 

 

////////////  - code detail explanation //// full code  is under this block

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using UnityEditor.Animations;

/<< use unity system, io, linq, unity animationsystem 

/<< system.io use at string control
/<< system.linq use at string Enum

 


public class StringToLower : EditorWindow
{
    static StringToLower myWindow;
    Object targetObj;

    string[] fileTypeNames = { "폴더", "더미생성" };
    int fileType = 0;
    string[] guids;
/<< many time explane this block, set base vriables


    [MenuItem("Window/stringtolower")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        myWindow = (StringToLower)GetWindow(typeof(StringToLower));

/<< this editor name is stringtolower 
        myWindow.Show();
    }

    private void OnDestroy()
    {
        CloseSpriteViewerWindow();
    }

    void CloseSpriteViewerWindow()
    {
        Reset();
    }



    void Reset()
    {
        targetObj = null;
    }
/<< all custom editor used parameter or method{reset, close, destory..) 

    void OnGUI()
    {
        int type = EditorGUILayout.Popup(fileType, fileTypeNames, GUILayout.Height(20));
        if (fileType != type)
        {
            CloseSpriteViewerWindow();
            fileType = type;
            Reset();
        }

        if (fileType == 0)
        {
            var obj = EditorGUILayout.ObjectField("폴더 :", targetObj, typeof(DefaultAsset), true);
            if (targetObj != obj)
            {
                targetObj = obj;
            }
            else
            { 
                if (targetObj != null)
                {
                    if (GUILayout.Button("문자바꾸기", GUILayout.Height(40)))

/<< click button action

                    {
                        var tempPath = AssetDatabase.GetAssetPath(targetObj);

/<<this line already explained, temp path set target folder's path
                        string[] guids = AssetDatabase.FindAssets("t:AnimationClip", new string[] { tempPath });
/<<find animationclip and put to guids array
                        for (int i = 0; i < guids.Length; ++i)
                        {
                            string path = AssetDatabase.GUIDToAssetPath(guids[i]);
/<<take materials path                            
                            string[] tempsprit = path.Split('/');

/<<Splits a path into '/' to separate folders and resources

/<<for example c:/work/a.clip to [c:, work, a.clip]
                            var lastindex = tempsprit.Length;
                            string localpath = path.Replace(tempsprit[lastindex - 1], "");
                            string newname = tempsprit[lastindex-1].ToLowerInvariant();
/<<Converts all separated resource names to lower case.
                           // System.IO.File.Move(path, localpath + newname);
                           // System.IO.File.Move(path+".meta", localpath + newname+".meta");
/<<system.io not used in this code but it is useful code line when assetdatabase.rename can't use
/ << this line is based on c # reference, c # do not have 'rename file action' so replace  'move with new name'           
                            AssetDatabase.RenameAsset(path, newname);

/ <<unity rename acrion it nees origine full path(c:/aaa/bbb/c.png) and target resource name(a.png)
                            AssetDatabase.SaveAssets();
                            AssetDatabase.Refresh();
                            
                        }
                    }
                }
            
            }
        }
    }
}

 

 

 

//////////////////////////////full code

 

 

 

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEditor; 
using System.IO; 
using System.Linq; 
using UnityEditor.Animations; 

public class StringToLower : EditorWindow 
{ 
    static StringToLower myWindow; 
    Object targetObj; 


    int frameSamples = 10; 
    int spriteInterval = 1; 

    string[] fileTypeNames = { "폴더", "더미생성" }; 
    int fileType = 0; 

  
    string[] guids; 

    [MenuItem("Window/stringtolower")] 
    static void Init() 
    { 
        // Get existing open window or if none, make a new one: 
        myWindow = (StringToLower)GetWindow(typeof(StringToLower)); 
        myWindow.Show(); 
    } 

    private void OnDestroy() 
    { 
        CloseSpriteViewerWindow(); 
    } 

    void CloseSpriteViewerWindow() 
    { 
        Reset(); 
    } 

    void Reset() 
    { 
         
        targetObj = null; 
    } 


    void OnGUI() 
    { 
        int type = EditorGUILayout.Popup(fileType, fileTypeNames, GUILayout.Height(20)); 
        if (fileType != type) 
        { 
            CloseSpriteViewerWindow(); 
            fileType = type; 
            Reset(); 
        } 

        if (fileType == 0) 
        { 
            var obj = EditorGUILayout.ObjectField("폴더 :", targetObj, typeof(DefaultAsset), true); 
            if (targetObj != obj) 
            { 
                targetObj = obj; 
            } 
            else 
            {  
                if (targetObj != null) 
                { 
                    if (GUILayout.Button("문자바꾸기", GUILayout.Height(40))) 
                    { 

                        var tempPath = AssetDatabase.GetAssetPath(targetObj); 
                        string[] guids = AssetDatabase.FindAssets("t:AnimationClip", new string[] { tempPath }); 

                        for (int i = 0; i < guids.Length; ++i) 
                        { 
                            string path = AssetDatabase.GUIDToAssetPath(guids[i]); 
                             
                             
                            string[] tempsprit = path.Split('/'); 
                            var lastindex = tempsprit.Length; 
                            Debug.Log(lastindex); 
                            string localpath = path.Replace(tempsprit[lastindex - 1], ""); 
                            string newname = tempsprit[lastindex-1].ToLowerInvariant(); 

                           // System.IO.File.Move(path, localpath + newname); 
                           // System.IO.File.Move(path+".meta", localpath + newname+".meta"); 

                             
                            AssetDatabase.RenameAsset(path, newname); 
                                     
                            AssetDatabase.SaveAssets(); 
                            AssetDatabase.Refresh(); 
                             
                        } 
                    } 
                } 
             
            } 
        } 
    } 
}