コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

2022-09-01から1ヶ月間の記事一覧

【Unity】指定された関数が UniTask の非同期メソッドかどうかを確認する関数

概要 private static bool IsAsynchronousMethod( MethodInfo methodInfo ) { return IsUniTask( methodInfo.ReturnType ); } private static bool IsUniTask( Type type ) { return type.IsGenericType ? type.GetGenericTypeDefinition() == typeof( UniTa…

【Unity】指定された型が UniTask かどうかを確認する関数

概要 private static bool IsUniTask( Type type ) { return type.IsGenericType ? type.GetGenericTypeDefinition() == typeof( UniTask<> ) : type == typeof( UniTask ) ; }

【Unity】Transform の Inspector のコンテキストメニューに「Copy Component」と「Paste Component Values」を表示するエディタ拡張

ソースコード using UnityEditor; using UnityEditorInternal; using UnityEngine; namespace Kogane.Internal { internal static class TransformCopyComponentPasteComponentValues { [MenuItem( "CONTEXT/Transform/Copy Component" )] public static voi…

【C#】構造体のフィールドに初期値を設定する代替案

C#

概要 C# 9.0 までは引数なしコンストラクタを定義できないため 構造体のフィールドに初期値を設定できない 代替案としてフィールドを nullable にして プロパティの getter でフィールドが null なら初期値を設定する方法がある using UnityEngine; public s…

【Unity】空配列の生成は Array.Empty を使うと GC Alloc を抑えられる

概要 using UnityEngine; public class Example : MonoBehaviour { private void Update() { // GC Alloc が 31.2 KB for ( var i = 0; i < 1000; i++ ) { var strings = new string[ 0 ]; } } } using System; using UnityEngine; public class Example : M…

【Unity】Unity プロジェクトのフォルダをエクスプローラで開けるようにするエディタ拡張

ソースコード using System.Diagnostics; using System.IO; using UnityEditor; namespace Kogane.Internal { internal static class ShowProjectInExplorer { #if UNITY_EDITOR_WIN [MenuItem( "File/Show Project in Explorer" )] #else [MenuItem( "File/…

【Unity】アセットのパスを Resources.Load で使用するパスに変換する方法

概要 using System.Text.RegularExpressions; namespace Kogane { public static class ResourcesPath { private static readonly Regex REGEX = new( @"^.*Resources/(.*)\..*" ); public static string ToResourcesPath( string assetPath ) { return REGE…

【Unity】ScriptableObject を CreateAssetMenu を使わずに生成できるようにする

ソースコード using UnityEditor; using UnityEngine; namespace Kogane.Internal { [InitializeOnLoad] internal static class ScriptableObjectCreatorOnInspector { static ScriptableObjectCreatorOnInspector() { Editor.finishedDefaultHeaderGUI -= O…

【Unity】Line Renderer を角丸にする方法

概要 End Cap Vertices を設定する

【Unity】スクリプトから Additional extensions to include を参照する方法

概要 Debug.Log( string.Join( "\n", EditorSettings.projectGenerationUserExtensions ) );

【Unity】コンポーネントの名前をゲームオブジェクトに反映できるエディタ拡張

ソースコード using UnityEditor; using UnityEngine; namespace Kogane.Internal { internal static class RenameGameObjectMenuItem { [MenuItem( "CONTEXT/Component/Rename Game Object" )] private static void Rename( MenuCommand menuCommand ) { va…

【Unity】ディープリンクで Unity のアセットを開く例

概要 using Needle.Deeplink; using UnityEditor; using UnityEngine; internal static class Example { [DeepLink( RegexFilter = @"com.unity3d.kharma:open-asset\/(.*)" )] private static bool OpenAsset( string assetName ) { Debug.Log( assetName )…

【Unity】ディープリンクでゲームオブジェクトの PingObject を実行する例

概要 using Needle.Deeplink; using UnityEditor; using UnityEngine; internal static class Example { [DeepLink( RegexFilter = @"com.unity3d.kharma:ping-game-object\/(.*)" )] private static bool PingGameObject( string gameObjectName ) { Debug.…

【Unity】ディープリンクでアセットの PingObject を実行する例

概要 using Needle.Deeplink; using UnityEditor; using UnityEngine; internal static class Example { [DeepLink( RegexFilter = @"com.unity3d.kharma:ping-asset\/(.*)" )] private static bool PingAsset( string assetPath ) { Debug.Log( assetPath )…

【Unity】ディープリンクで MenuItem を実行する例

概要 using Needle.Deeplink; using UnityEditor; using UnityEngine; internal static class Example { [DeepLink( RegexFilter = @"com.unity3d.kharma:execute-menu-item\/(.*)" )] private static bool ExecuteMenuItem( string menuItemPath ) { Debug.…

【Unity】エディタ拡張で検索欄を簡単に表示する方法

概要 using UnityEditor; using UnityEditor.IMGUI.Controls; public class Example : EditorWindow { private SearchField m_searchField; private string m_text; [MenuItem( "Tools/Hoge" )] private static void Open() { GetWindow<Example>(); } private void O</example>…

【Unity】CompilationPipeline.compilationFinished の中で EditorApplication.delayCall は呼ばれない

概要 [InitializeOnLoadMethod] private static void Hoge() { CompilationPipeline.compilationFinished += _ => { Debug.Log( "ピカチュウ" ); // 呼ばれる EditorApplication.delayCall += () => { Debug.Log( "カイリュー" ); // 呼ばれない }; }; } Ass…

【Unity】Scene Template の Thumbnail の Preview に画像を設定するとエラーが出る

概要 Assertion failed on expression: 'IsInSyncWithParentSerializedObject()' UnityEditor.RetainedMode:UpdateSchedulers () Scene Template の Thumbnail の Preview に画像を設定すると Project ウィンドウで Scene Template を選択するたびに 上記の…

【Unity】Scene Template からシーンを作成するエディタ拡張

概要 using UnityEditor; using UnityEditor.SceneTemplate; using UnityEngine; public static class Example { [MenuItem( "Tools/Hoge" )] private static void Hoge() { var sceneTemplateAssetPath = "【.scenetemplate のパス】"; var scenePath = "【…

【Unity】パッケージを埋め込みパッケージに変更できるボタンを Inspector に表示するエディタ拡張

ソースコード using System.IO; using UnityEditor; using UnityEditor.PackageManager; using UnityEditorInternal; using UnityEngine; namespace Kogane.Internal { [InitializeOnLoad] internal static class PackageEmbedder { static PackageEmbedder(…

【Unity】Mute Audio を Unity メニューから切り替えられるエディタ拡張

ソースコード using UnityEditor; using UnityEngine; namespace Kogane.Internal { [InitializeOnLoad] internal static class MuteAudioMenuItem { private const string MENU_ITEM_NAME = "Kogane/Mute Audio"; static MuteAudioMenuItem() { EditorAppli…

【Unity】iOS ビルドでのみ「The referenced script on this Behaviour (XXXX) is missing!」が発生した

概要 Unity エディタだと正常に動作するが iOS ビルドだと 「The referenced script on this Behaviour (XXXX) is missing!」が発生する現象に遭遇した Library フォルダを削除してからクリーンビルドしたら解決した

【Unity】Inspector のヘッダに「Lock」「Debug」「Properties...」「Reveal In Finder」ボタンを表示するエディタ拡張

ソースコード using System.Linq; using System.Reflection; using UnityEditor; using UnityEngine; namespace Kogane.Internal { [InitializeOnLoad] internal static class InspectorHeaderGUI { static InspectorHeaderGUI() { Editor.finishedDefaultHe…