Last active
November 12, 2019 16:39
-
-
Save Suzeep/6d624fe1777c514df7459588ba16f39b to your computer and use it in GitHub Desktop.
Unity用の汎用メソッド集
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Linq; | |
//================================================================================ | |
// Unity用 汎用ライブラリ | |
//================================================================================ | |
public static class UniUtil | |
{ | |
//-------------------------------------------------------------------------------- | |
// タイマーチェック | |
//-------------------------------------------------------------------------------- | |
// 減算 | |
public static bool CheckSubTime( ref float timer ) | |
{ | |
if( (timer -= Time.deltaTime) < 0.0f ) | |
return true; | |
else | |
return false; | |
} | |
public static bool CheckSubTime( ref float timer, float sub_time ) | |
{ | |
if( (timer -= sub_time) < 0.0f ) | |
return true; | |
else | |
return false; | |
} | |
// 加算 | |
public static bool CheckAddTime( ref float timer, float check_time ) | |
{ | |
if( (timer += Time.deltaTime) > check_time ) | |
return true; | |
else | |
return false; | |
} | |
public static bool CheckSubTime( ref float timer, float add_time, float check_time ) | |
{ | |
if( (timer += add_time) > check_time ) | |
return true; | |
else | |
return false; | |
} | |
//-------------------------------------------------------------------------------- | |
// 指定したタグのゲームオブジェクトから指定のコンポーネントを取得 | |
//-------------------------------------------------------------------------------- | |
public static T GetComponentWithTag<T>( string tag ) where T : MonoBehaviour | |
{ | |
var obj = GameObject.FindGameObjectWithTag( tag ); | |
return ( obj == null ) ? null : obj.GetComponent<T>(); | |
} | |
//-------------------------------------------------------------------------------- | |
// 指定したオブジェクトにぶら下がってる子オブジェクトを全削除 | |
//-------------------------------------------------------------------------------- | |
public static void DestroyChildObject( Transform parent_trans ) | |
{ | |
for( int i=0; i < parent_trans.childCount; ++i ){ | |
GameObject.Destroy( parent_trans.GetChild( i ).gameObject ); | |
} | |
} | |
// Immediate版 即時に消えるのでリストの参照の仕方が異なる | |
public static void DestroyImmediateChildObject( Transform parent_trans ) | |
{ | |
#if false | |
int cnt = parent_trans.childCount; | |
for( int i=0; i < cnt; ++i ){ | |
GameObject.DestroyImmediate( parent_trans.GetChild( 0 ).gameObject ); | |
} | |
#else | |
for( int i = parent_trans.childCount - 1; i >= 0; --i ){ | |
GameObject.DestroyImmediate( parent_trans.GetChild( i ).gameObject ); | |
} | |
#endif | |
} | |
//-------------------------------------------------------------------------------- | |
// 数値を3ケタ毎にカンマ区切りした文字列にして返す | |
//-------------------------------------------------------------------------------- | |
public static string Get3ketaFormatStr( int val ){ | |
return string.Format( "{0:#,0}", val ); | |
} | |
//-------------------------------------------------------------------------------- | |
// 指定したenum型の要素数を取得する(値重複分を除く) | |
// ※iOSでLinqが動かない可能性があるので, エディタ上でしか動かないものに使用すること. | |
//-------------------------------------------------------------------------------- | |
public static int GetEnumLength<T>(){ | |
return Enum.GetValues(typeof(T)).Cast<T>().Distinct().Count(); | |
} | |
//-------------------------------------------------------------------------------- | |
// 引数に渡したオブジェクトをディープコピーしたオブジェクトを生成して返す | |
// ジェネリックメソッド版 | |
//-------------------------------------------------------------------------------- | |
public static T DeepCopy<T>( T target ) | |
{ | |
T result; | |
BinaryFormatter b = new BinaryFormatter(); | |
MemoryStream mem = new MemoryStream(); | |
try { | |
b.Serialize(mem, target); | |
mem.Position = 0; | |
result = (T)b.Deserialize(mem); | |
} | |
finally { | |
mem.Close(); | |
} | |
return result; | |
} | |
// 拡張メソッド版 | |
public static object DeepCopy( this object target ) | |
{ | |
object result; | |
BinaryFormatter b = new BinaryFormatter(); | |
MemoryStream mem = new MemoryStream(); | |
try { | |
b.Serialize(mem, target); | |
mem.Position = 0; | |
result = b.Deserialize(mem); | |
} | |
finally { | |
mem.Close(); | |
} | |
return result; | |
} | |
//-------------------------------------------------------------------------------- | |
// 値の入れ替え(※プリミティブ型に使うことを推奨) | |
//-------------------------------------------------------------------------------- | |
public static void ChangeValue<T>( ref T valueA, ref T valueB ) | |
{ | |
T temp = valueA; | |
valueA = valueB; | |
valueB = temp; | |
} | |
//-------------------------------------------------------------------------------- | |
// Listから要素をランダムで1つ取得する | |
//-------------------------------------------------------------------------------- | |
public static T GetRandom<T>( List<T> list ) | |
{ | |
return list[ UnityEngine.Random.Range(0, list.Count) ]; | |
} | |
//-------------------------------------------------------------------------------- | |
// "X/MAX_X" の文字列を返す | |
//-------------------------------------------------------------------------------- | |
public static string GetValueOfMaxStr( int x, int max_x ) | |
{ | |
return string.Format( "{0}/{1}", x, max_x ); | |
} | |
//-------------------------------------------------------------------------------- | |
// 数値の桁数を取得 | |
//-------------------------------------------------------------------------------- | |
public static int GetDigit( int num ) | |
{ | |
return (num == 0) ? 1 : (int)Mathf.Log10( num ) + 1; | |
} | |
//-------------------------------------------------------------------------------- | |
// 指定した確率でtrueを返す | |
//-------------------------------------------------------------------------------- | |
public static bool CheckRate( int rate ) | |
{ | |
if( UnityEngine.Random.Range( 0, 100 ) < rate ) | |
return true; | |
else | |
return false; | |
} | |
public static bool CheckRate( float rate ) | |
{ | |
if( (UnityEngine.Random.value * 100.0f) < rate ) | |
return true; | |
else | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment