LoginSignup
0
0

More than 5 years have passed since last update.

Unity複数起動するときにどれかわからなくなった人のためのEditorスクリプト

Last updated at Posted at 2017-10-19

複数Unity起動していると、どれがどのプロジェクトか忘れちゃうので、
メニューに表示するスクリプト作成。
ゴリゴリで書いているので、汚いソースには目をつぶってください。

Assetsの親の親の名前を取ってきているのは、
git管理とかだと、Assetsの親を
「Project」とか「Unity」とか「UnityScript」とか統一していて、
同階層にドキュメントとかツールをおいている人も多いかなぁと思ってそうしております。

ProjectUtil.cs
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace yyama2.Editor {

    public partial class ProjectUtil : AssetPostprocessor {
        private const string THIS_FILE_NAME = "ProjectUtil.cs";
        private const string PROJECT_NAME = ""; // ここが勝手に書き換わる。
        private const string PROJECT_NAME_REPLACE_FORMAT = "private const string PROJECT_NAME = \"{0}\";";
        private const string MENU_ITEM_ROOT = "【" + PROJECT_NAME + "】" + ":ProjectUtil/";
        public static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath)
        {
            // コンストラクタ起動用に定義
        }
        private const string PROJECT_DIR_NAME = "Project";

        static ProjectUtil()
        {
            var path = Application.dataPath;
            var projName = path.Substring( 0, path.Length - path.Substring( path.LastIndexOf( PROJECT_DIR_NAME ) ).Length );

            var temp = Path.GetDirectoryName( projName ).Split( "/"[0] );
            projName = temp[temp.Length-1];

            if( projName != PROJECT_NAME)
            {
                var thisFilePath = CombineAll( Application.dataPath,"Editor",THIS_FILE_NAME);
                string text = "";
                using ( StreamReader reader = new StreamReader( thisFilePath, System.Text.Encoding.UTF8 ) )
                {
                    text = reader.ReadToEnd();
                }
                text = text.Replace( string.Format( PROJECT_NAME_REPLACE_FORMAT, PROJECT_NAME ), string.Format( PROJECT_NAME_REPLACE_FORMAT, projName ) );

                using ( StreamWriter writer = new StreamWriter( thisFilePath, false, System.Text.Encoding.UTF8 ) )
                {
                    writer.Write( text );
                }
                AssetDatabase.Refresh();
            }
        }

        private static string CombineAll( string root, params string[] stringArray )
        {
            if( stringArray == null || stringArray.Length == 0 ) return stringArray[0];

            string path = null;
            path = Path.Combine( root, stringArray[0] );

            if (stringArray.Length == 1)
            {
                return path;
            }
            else
            {
                var stringList = new List<string>( stringArray );
                stringList.RemoveAt(0);
                return CombineAll( path, stringList.ToArray() );
            }
        }

        [MenuItem( MENU_ITEM_ROOT + "Hello" )]
        public static void ShowCurrentProjectDir()
        {
            // 
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0