Created
June 20, 2023 03:22
-
-
Save FUJI-bayashi/1bb8f129dea7570fe39b82d22c685885 to your computer and use it in GitHub Desktop.
Staticな自作ライブラリの例。オブジェクトにアタッチする必要はない(できない)。これをプロジェクトウインドウのどこかに置いておいて、使いたいクラスで「using static Tool;」と書けば使えます。
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 System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.SceneManagement; | |
| public static class Tool | |
| { | |
| /// <summary> | |
| /// コンポーネントを持つゲームオブジェクトを取得 | |
| /// </summary> | |
| public static GameObject GetGameObjectHasComponent<T>() | |
| { | |
| // ActiveなSceneのRootにあるGameObject[]を取得する | |
| var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects(); | |
| foreach (var item in rootGameObjects) | |
| { | |
| if(item.TryGetComponent(out T comp)) | |
| return item; | |
| } | |
| return null; | |
| } | |
| //------Nullチェック------------------------------------------------------ | |
| /// <summary> | |
| /// Nullか | |
| /// </summary> | |
| public static bool IsNull<T>(T _var) | |
| { | |
| T v = _var; | |
| return v == null; | |
| } | |
| public static bool IsNull(GameObject _var) | |
| { | |
| GameObject v = _var; | |
| return v == null; | |
| } | |
| /// <summary> | |
| /// リストがNullか | |
| /// </summary> | |
| public static bool IsNull<T>(List<T> _list) | |
| { | |
| if (_list == null || _list.Count <= 0) | |
| return true; | |
| return false; | |
| } | |
| public static bool IsNull(List<GameObject> _list) | |
| { | |
| if (_list == null || _list.Count <= 0) | |
| return true; | |
| GameObject g = null; | |
| bool exists = false; | |
| foreach (var item in _list) | |
| { | |
| g = item; | |
| if (g != null) | |
| exists = true; | |
| } | |
| return !exists; | |
| } | |
| /// <summary> | |
| /// 配列がNullか | |
| /// </summary> | |
| public static bool IsNull<T>(T[] _ar) | |
| { | |
| if (_ar == null || _ar.Length <= 0) | |
| return true; | |
| return false; | |
| } | |
| /// <summary> | |
| /// リストのIndexの要素がNullか | |
| /// </summary> | |
| public static bool IsNull<T>(List<T> _list, int _index) | |
| { | |
| if (IsNull(_list)) | |
| return true; | |
| if (_list.Count - 1 >= _index) | |
| return false; | |
| return true; | |
| } | |
| /// <summary> | |
| /// Nullでないか | |
| /// </summary> | |
| public static bool IsNotNull<T>(T _var) | |
| { | |
| T v = _var; | |
| return v != null; | |
| } | |
| public static bool IsNotNull(GameObject _var) | |
| { | |
| GameObject v = _var; | |
| return v != null; | |
| } | |
| /// <summary> | |
| /// リストがNullではないか | |
| /// </summary> | |
| public static bool IsNotNull<T>(List<T> _list) | |
| { | |
| if (_list == null || _list.Count <= 0) | |
| return false; | |
| return true; | |
| } | |
| /// <summary> | |
| /// リストのIndexの要素がNullではないか | |
| /// </summary> | |
| public static bool IsNotNull<T>(List<T> _list, int _index) | |
| { | |
| if(IsNull(_list)) | |
| return false; | |
| if (_list.Count - 1 >= _index) | |
| return true; | |
| return false; | |
| } | |
| /// <summary> | |
| /// 配列がNullでないか | |
| /// </summary> | |
| public static bool IsNotNull<T>(T[] _ar) | |
| { | |
| if (_ar == null || _ar.Length <= 0) | |
| return false; | |
| return true; | |
| } | |
| /// <summary> | |
| /// オブジェクトがNullならコンポーネントとともに取得する | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="_obj">入れるGameObject</param> | |
| public static void NullCheck<T>(ref GameObject _obj, ref T _comp) | |
| { | |
| if (IsNull(_obj)) | |
| { | |
| _obj = GetGameObjectHasComponent<T>(); | |
| _comp = _obj.GetComponent<T>(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment