Instantly share code, notes, and snippets.
Last active
June 20, 2023 03:20
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
-
Save FUJI-bayashi/cd10378ee02fd845a7386e72ca896ebc to your computer and use it in GitHub Desktop.
MyButtonControllerを取りまとめておくためのクラス。これをアタッチしたオブジェクトをシーンにひとつ置いておく。下のほうの「Staticなライブラリにおくほうがいいメソッド」はMyButtonController内のメソッドと同じものです。
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 class MyButtonPool : MonoBehaviour | |
| { | |
| static public MyButtonPool inst; | |
| void Awake() | |
| { | |
| inst = this; | |
| } | |
| [SerializeField] List<MyButtonData> li_ButtonData; | |
| [SerializeField] List<MyButtonController> li_MyButtonController; | |
| [SerializeField] int idPool; | |
| Vector3 unbiggenScale = new Vector3(1.1f, 1.1f, 1.1f); | |
| //Add-Remove------------------------------------------------------------------------------------ | |
| /// <summary> | |
| /// ボタンをプールに追加 | |
| /// </summary> | |
| public void AddMyButtonToPool(MyButtonData _buttonData, MyButtonController _myButtonController) | |
| { | |
| //Debug.Log(this + ": ボタン追加処理を開始します。"); | |
| if (IsNull(_buttonData)) | |
| { | |
| //Debug.Log(this + ": ボタン追加処理中...ボタンを追加しようとしましたが、渡されたButtonDataがnullでした。処理中断。"); | |
| return; | |
| } | |
| if (IsNull(li_ButtonData)) | |
| { | |
| li_ButtonData = new List<MyButtonData>(); | |
| } | |
| else if (li_ButtonData.Contains(_buttonData)) | |
| { | |
| //Debug.Log(this + ": ボタン追加処理中...ボタンを追加しようとしましたが、既にリストに追加されていました。処理中断。"); | |
| return; | |
| } | |
| if (IsNull(li_MyButtonController)) | |
| li_MyButtonController = new List<MyButtonController>(); | |
| //Debug.Log(this + ": ボタン追加処理中...ボタンを追加します。"); | |
| //ボタンをリストに追加 | |
| li_ButtonData.Add(_buttonData); | |
| li_MyButtonController.Add(_myButtonController); | |
| //連番のIDをMyButtonに返す | |
| _buttonData.SetID(NewButtonID()); | |
| //Debug.Log(this + ": ボタン追加処理を終了します。ボタンID=" + _buttonData.GetID().ToString()); | |
| } | |
| /// <summary> | |
| /// ボタン連番IDを生成して返す | |
| /// </summary> | |
| int NewButtonID() | |
| { | |
| ++idPool; | |
| return idPool - 1; | |
| } | |
| /// <summary> | |
| /// ボタンIDをリセット | |
| /// </summary> | |
| void ResetButtonID() | |
| { | |
| idPool = 0; | |
| } | |
| /// <summary> | |
| /// ボタンを削除 | |
| /// </summary> | |
| public void RemoveButton(MyButtonData _buttonData) | |
| { | |
| Debug.Log(this + ": ボタン削除処理を開始します。"); | |
| if (li_ButtonData == null) | |
| { | |
| Debug.Log(this + ": ボタン削除処理中...ボタンを削除しようとしましたが、リストがありませんでした。処理中断。"); | |
| return; | |
| } | |
| if (li_ButtonData.Count == 0) | |
| { | |
| Debug.Log(this + ": ボタン削除処理中...ボタンを削除しようとしましたが、リストが宣言だけされていて要素がありませんでした。処理中断。"); | |
| return; | |
| } | |
| if (!li_ButtonData.Contains(_buttonData)) | |
| { | |
| Debug.Log(this + ": ボタン削除処理中...ボタンを削除しようとしましたが、リストにボタンがありませんでした。処理中断。"); | |
| return; | |
| } | |
| Debug.Log(this + ": ボタン削除処理中...ボタンを削除します。ボタンID=" + _buttonData.GetID().ToString()); | |
| li_ButtonData.Remove(_buttonData); | |
| Debug.Log(this + ": ボタン削除処理を終了します。"); | |
| } | |
| //Get---------------------------------------------------------------------------------- | |
| public List<MyButtonData> GetButtonDataList() | |
| { | |
| return li_ButtonData; | |
| } | |
| public List<MyButtonController> GetMyButtonControllerList() | |
| { | |
| return li_MyButtonController; | |
| } | |
| //Event------------------------------------------------------------------------------- | |
| public void AddEvent_Unbiggen(MyButtonController _myButtonController, RectTransform _scalingRT) | |
| { | |
| _myButtonController.AddAction_Enter(Unbiggen); | |
| _myButtonController.AddAction_Exit(Shrink); | |
| void Unbiggen() | |
| { | |
| _scalingRT.localScale = unbiggenScale; | |
| } | |
| void Shrink() | |
| { | |
| _scalingRT.localScale = Vector3.one; | |
| } | |
| } | |
| //------ここからはStaticな自作ライブラリに置いておうほうが良いメソッドです。(汎用性が高いため)------------------------------------------------------ | |
| /// <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; | |
| } | |
| /// <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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment