コガネブログ

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

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

【Unity】UI の Image では Material Property Block は使用できない

参考サイト様

【Unity】Package Manager の Install・Remove ボタンの隣にボタンを表示するサンプル

ソースコード using System; using System.Collections.Generic; using System.IO; using System.Reflection; using UnityEditor; using UnityEditor.PackageManager.UI; using UnityEditorInternal; using UnityEngine.UIElements; using PackageInfo = Uni…

【Rider】選択中のテキストのバックグラウンドカラーを変更する方法

概要 Preferences の「Editor > Color Scheme > General」の 「Editor > Selection background」の「Background」の色を変更する

【Rider】Mac で Preferences のカラーピッカーが正常に動作しない場合

概要 「システム環境設定 > セキュリティとプライバシー > 画面収録」で 「Rider.app」のチェックを外したら正常に動作するようになった

【Unity】Zenn の Unity に関するフィードを購読する方法

概要 https://zenn.dev/topics/unity/feed 上記の URL を RSS リーダに追加する

【Unity】Device Simulator に Mute Audio ボタンを追加するエディタ拡張

ソースコード using JetBrains.Annotations; using UnityEditor; using UnityEditor.DeviceSimulation; using UnityEngine.UIElements; namespace Kogane.Internal { [UsedImplicitly] internal sealed class DeviceSimulatorMuteAudioPlugin : DeviceSimula…

【Unity】EditorWindow を開いた時に TextField をフォーカスして編集状態にする方法

概要 using UnityEditor; using UnityEngine; public sealed class Example : EditorWindow { private bool m_isInitialized; private string m_name; [MenuItem( "Tools/Hoge" )] public static void Open() { GetWindow<Example>(); } private void OnGUI() { GUI.S</example>…

【C#】文字列から HTML のタグを削除する例

C#

概要 var text = Regex.Replace( text, "<.*?>", string.Empty );

【Unity】EditorWindow の OnGUI を毎フレーム更新したい場合

概要 using UnityEditor; public sealed class Example : EditorWindow { private void Update() { Repaint(); } } Update で Repaint を呼び出すと OnGUI が毎フレーム更新される

【Unity】Transform の初期値を保持しておくためのコンポーネント

ソースコード using UnityEngine; namespace Kogane { /// <summary> /// Transform の初期値を保持しておくためのコンポーネント /// </summary> [DisallowMultipleComponent] [DefaultExecutionOrder( -1000 )] public sealed class DefaultTransform : MonoBehaviour { //===…

【Unity】スクリプトから UIElements の要素を横に並べる方法

概要 var root = new VisualElement(); root.style.flexDirection = FlexDirection.Row; VisualElement の style.flexDirection に FlexDirection.Row を指定する これだけだと要素が左に表示されてしまう var root = new VisualElement(); root.style.flexD…

【Unity】Scenes In Build が変更されたかどうかを検知する方法

ソースコード using System.Linq; using UnityEditor; using UnityEngine; internal sealed class Example : AssetPostprocessor { private const string PATH = "ProjectSettings/EditorBuildSettings.asset"; private static void OnPostprocessAllAssets …

【C#】2 つの配列の要素をインデックス同士で紐付けて Tuple にまとめる拡張メソッド

概要 using System.Collections.Generic; namespace Kogane { public static class SequenceJoinTupleExtensionMethods { public static (T1, T2)[] JoinTuple<T1, T2>( this T1[] self, T2[] other ) { var count = self.Length; var result = new (T1, T2)[ count </t1,>…

【C#】条件を満たす場合にのみ Prepend する拡張メソッド

概要 using System.Collections.Generic; using System.Linq; namespace Kogane { public static class EnumerableExtensionMethods { public static IEnumerable<T> PrependIf<T> ( this IEnumerable<T> self, bool conditional, T element ) { return !conditional </t></t></t>…

【C#】System.Text.Json で列挙型を文字列でシリアライズする方法

C#

概要 using System.Text.Json; using UnityEngine; public enum CharacterType { NONE, PLAYER, ENEMY, } public class Example : MonoBehaviour { private void Start() { var data = new { characterType = CharacterType.PLAYER }; Debug.Log( JsonSerial…

【Unity】InvalidProgramException: Invalid IL code in XXXX:CecilAttributesGenerated_ResetStatics (): IL_0001: call 0x06000301

概要 [ResetStatic] public static bool IsInitialized { get; private set; } Cecil Attributes の Reset Static をプロパティに対して付与したら InvalidProgramException: Invalid IL code in XXXX:CecilAttributesGenerated_ResetStatics (): IL_0001: c…

【Unity】Cecil Attributes の Reset Static を null 許容値型のフィールドに使うと Unity 再生時にエディタがクラッシュする

概要 [ResetStatic] private static float? m_value; null 許容値型のフィールドに Reset Static を使ったら Unity 再生時にエディタがクラッシュする現象に遭遇した null 許容値型のフィールドに Reset Static を使わないようにしたらクラッシュしなくなっ…

【Unity】GetComponent の結果をキャッシュしておくためのクラス

ソースコード using UnityEngine; namespace Kogane { /// <summary> /// GetComponent の結果をキャッシュしておくためのクラス /// </summary> public sealed class ComponentCache<T> { private T m_cache; private bool m_isCached; public T Get( GameObject gameObject ) { /</t>…

【Unity】AspectRatioFitter の aspectRatio にスプライトのアスペクト比を設定できるようにするエディタ拡張

ソースコード using UnityEditor; using UnityEngine; using UnityEngine.UI; namespace Kogane.Internal { internal static class AspectRatioFitterMenuItem { [MenuItem( "CONTEXT/AspectRatioFitter/Set Default Aspect Ratio" )] private static void S…

【Unity】Unity プロジェクトのキャッシュを削除するバッチファイルの例

ソースコード @echo off set /P ANSWER="キャッシュを削除しますか (Y/N)?" if /i {%ANSWER%}=={y} (goto :yes) if /i {%ANSWER%}=={yes} (goto :yes) exit :yes rd /s /q Library\ rd /s /q Logs\ rd /s /q obj\ rd /s /q Temp\ del *.csproj del *.sln ec…

【Unity】Device Simulator でデバイスが変更された時に呼び出されるイベント

ソースコード using System; using System.Linq; using System.Reflection; using UnityEditor; using UnityEditor.DeviceSimulation; using UnityEngine; namespace Kogane { [InitializeOnLoad] public static class DeviceSimulatorDeviceChecker { priva…

【Unity】Rider の Regenerate project files をメニューから実行できるようにするエディタ拡張

ソースコード using Packages.Rider.Editor; using UnityEditor; namespace Kogane.Internal { internal static class JetBrainsRiderRegenerateProjectFiles { [MenuItem( "Kogane/Rider/Regenerate project files" )] private static void SyncSolution() …

【Unity】The package cache was invalidated and rebuilt because the following immutable asset(s) were unexpectedly altered:

概要 The package cache was invalidated and rebuilt because the following immutable asset(s) were unexpectedly altered: Unity で作業していたら上記の警告が出力されるようになった Unity を再起動したら出なくなった

【Unity】Scene ウィンドウに簡単にデバッグ表示できる「Debugging」紹介

はじめに 「Debugging」を Unity プロジェクトに導入することで Scene ウィンドウに簡単にデバッグ表示できるようになります 使用例 using UnityEngine; using Vertx.Debugging; public class Example : MonoBehaviour { private void Update() { DebugUtils…

【Unity】UIElements でボタンのサイズをスクリプトから設定する方法

概要 var button = new Button { text = "ボタン" }; button.style.width = 10; style.width を使用する

【Unity】DeviceSimulator のイベントを取得する例

ソースコード using System.Reflection; using UnityEditor; using UnityEditor.DeviceSimulation; using UnityEngine; [InitializeOnLoad] public static class Example { static Example() { // 1 フレーム遅らせないと deviceSimulatorMain が null にな…

【Unity】パッケージの依存関係を確認できるようにする方法

概要 Project Settings を開いて左メニューで「Package Manager」を選択して 「Show Dependencies」をオンにすると Package Manager でパッケージの依存関係を確認できるようになる

【Unity】Unity 2022 から PolygonCollider2D などでポリゴン数を減らせるようになった

概要 Unity 2022 から PolygonCollider2D などで Use Delaunay Mesh をオンにすると ポリゴン数を減らせるようになった (左がオフ、右がオン) 参考サイト様

【Unity】int や float の配列は Inspector で Ctrl + D で要素を複製できる

概要 using UnityEngine; public class Example : MonoBehaviour { public int[] m_intArray; } int や float の配列は Inspector で Ctrl + D で要素を複製できる string や参照型は Ctrl + D を押しても何も起きない

【Unity】スプライトの Custom Outline を設定した場合としなかった場合の処理速度比較

検証内容 30,000 個のスプライトをシーンに配置 スプライトの Custom Outline を設定した場合としなかった場合の FPS を比較 検証環境 Unity 2021.3.1f1 Custom Outline を設定した場合 36 ~ 39 FPS Custom Outline を設定しなかった場合 38 ~ 42 FPS 結論 …