コガネブログ

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

2019-11-28から1日間の記事一覧

【Unity】Vector2 や Vector3 の ToString では小数部の桁数を指定できる

概要 using UnityEngine; public class Example : MonoBehaviour { private void Awake() { var vec = new Vector3( 1.234f, 5.678f, 9.012f ); // (1.2, 5.7, 9.0) Debug.Log( vec.ToString() ); // (1.2340, 5.6780, 9.0120) Debug.Log( vec.ToString( "F4…

【Unity】List.Find の null チェックを少しだけ簡潔に記述できる拡張メソッド

ソースコード using System; using System.Collections.Generic; public static class ListExt { public static bool TryFind<T>( this List<T> self, Predicate<T> match, out T result ) where T : class { result = self.Find( match ); return result != null; } </t></t></t>…

【Unity】GetComponentInParent の null チェックを少しだけ簡潔に記述できる TryGetComponentInParent 拡張メソッド

ソースコード using UnityEngine; public static class ComponentExt { public static bool TryGetComponentInParent<T>( this Component self, out T component ) where T : Component { component = self.GetComponentInParent<T>(); return component != null; </t></t>…

【Unity】GetComponentInChildren の null チェックを少しだけ簡潔に記述できる TryGetComponentInChildren 拡張メソッド

ソースコード using UnityEngine; public static class ComponentExt { public static bool TryGetComponentInChildren<T>( this Component self, out T component ) where T : Component { component = self.GetComponentInChildren<T>(); return component != nu</t></t>…

【Unity】TryGetComponent を Unity 2018 でも使用できるようにする拡張メソッド

ソースコード using UnityEngine; public static class ComponentExt { public static bool TryGetComponent<T>( this Component self, out T component ) where T : Component { component = self.GetComponent<T>(); return component != null; } } public stati</t></t>…

【Unity】TryGetComponent の活用方法

概要 var collider = GetComponent<BoxCollider>(); if ( collider != null ) { Debug.Log( collider ); } GetComponent だとこのように記述する処理を if ( TryGetComponent<BoxCollider>( out var collider ) ) { Debug.Log( collider ); } TryGetComponent を使うとちょっとだけ簡潔</boxcollider></boxcollider>…

【Unity】Shortcut Manager でキー割り当てが変更された時に呼び出されるイベント

概要 Shortcut Manager でキー割り当てが変更された時に using UnityEditor; using UnityEditor.ShortcutManagement; using UnityEngine; [InitializeOnLoad] public static class Example { static Example() { ShortcutManager.instance.shortcutBindingCh…

【Unity】non convex な Mesh Collider を綺麗に補間できる「UniColliderInterpolator」紹介

はじめに 「UniColliderInterpolator」を Unity プロジェクトに導入することで non convex な Mesh Collider を綺麗に補間できます 使用例 通常 Mesh Collider の「Convex」をオンにすると このように凹凸の部分に正しく Mesh Collider が適用されません Uni…