【C#】整数同士の割り算、小数点有りで出力
整数同士の割り算 => 答えも整数になる!
片方の型を小数点を含む型にして割り算を実行する。
blog.jnito.com
#if UNITY_EDITOR
エディタ上だけしたいこと、例えば、PlayerPrefs使って設定したけど、今エディタ上では必要ないときとか。
void Awake(){ #if UNITY_EDITOR PlayerPrefs.DeleteAll (); #endif }
それぞれのシーンに相当するオブジェクトの親をオンオフして、表示の切り替えをする。
UIの親につけるスクリプト
UIAchievementMenu.cs
using UnityEngine; using System.Collections; public class UIAchievementMenu : MonoBehaviour { private GameObject _container ; void Awake(){ _container = transform.FindChild("Container").gameObject; _container.SetActive(false); } void OnEnable(){ EventManager.onGameChangeSceneEvent += HandleonGameChangeSceneEvent; } void OnDisable(){ EventManager.onGameChangeSceneEvent -= HandleonGameChangeSceneEvent; } void HandleonGameChangeSceneEvent (GameController.GAME_SCENE scene){ switch(scene){ case GameController.GAME_SCENE.GAME_SCENE_ACHIEVEMENT_MENU: _container.SetActive(true); break; default: _container.SetActive(false); break; } } }
ボタンの付いているスクリプトにつける
HUDClass.cs
public void OnAchievementButtonClick(){ EventManager.GameChangeSceneMethod(GameController.GAME_SCENE.GAME_SCENE_ACHIEVEMENT_MENU); }
GameController.cs
using UnityEngine; using System.Collections; public class GameController : MonoBehaviour { public enum GAME_SCENE{ GAME_SCENE_TITLESCREEN, GAME_SCENE_STAGE_SELECTION, GAME_SCENE_INGAME, GAME_SCENE_PAUSE, GAME_SCENE_EXIT_MENU, GAME_SCENE_HERO_UPGRADE_MENU, GAME_SCENE_RESULT_SCREEN, GAME_SCENE_ACHIEVEMENT_MENU } public static GAME_SCENE _curGameScene = GAME_SCENE.GAME_SCENE_TITLESCREEN; public static GAME_SCENE _prevGameScene = GAME_SCENE.GAME_SCENE_TITLESCREEN; public static int _stage = 1; public static GameController Instance; public float _autoSaveCounter = 1f; void Awake(){ Instance = this; } void OnEnable(){ EventManager.onGameChangeSceneEvent += HandleonGameChangeSceneEvent; } void OnDisable(){ EventManager.onGameChangeSceneEvent -= HandleonGameChangeSceneEvent; } void HandleonGameChangeSceneEvent (GAME_SCENE scene) { _prevGameScene = _curGameScene; _curGameScene = scene; } }
UIテキストの自動改行、outlineの機能
自動改行は、日本語のテキストを用いる時に役に立つ!もっと早く知りたかった!
tsubakit1.hateblo.jp
www.atmarkit.co.jp
【C#】Delegate使用 Event 使用例 第二弾
EventManager.cs
using UnityEngine; using System.Collections; public class EventManager { public delegate void AchievementTapHandler(int number); public static event AchievementTapHandler onAchievementTapE; public static void onAchievementTap(int number) { if (onAchievementTapE != null) onAchievementTapE(number); } }
※オブジェクトとしてhierarckyに置かず、Assetに置いたままにできる。( : MonoBehaviourを外す)
AchievementNotif.cs
using UnityEngine; using System.Collections; using System.Collections.Generic; public class AchievementNotif : MonoBehaviour { public AchievementContentNotif _achievementContentNotif; #region Event void OnEnable(){ EventManager.onAchievementTapE += ShowBanner; } void OnDisable(){ EventManager.onAchievementTapE -= ShowBanner; } #endregion void Awake (){ } void ShowBanner (int i) { List<AchievementContents> _myCollection = AchievementSource._instance._myCollection; _achievementContentNotif.SetContent(_myCollection[i]._name, _myCollection[i]._type, _myCollection[i]._goal, _myCollection[i]._reward); } }
AchievementManager.cs
public void CheckAchievement(ACHIEVEMEN_TYPE type , double value){ List<AchievementContents> items = new List<AchievementContents>(); switch(type){ case ACHIEVEMEN_TYPE.TAP: items = AchievementSource._instance.RetrieveAchievement(type); for(int i = 0 ; i < items.Count ; i++){ if(!items[i]._unlocked){ double t = 0; double.TryParse(items[i]._goal,out t ); if( tapCounter >= t ){ items[i]._unlocked = true; EventManager.onAchievementTap(i); } } } break; case ACHIEVEMEN_TYPE.UPGRADE_HERO: break; case ACHIEVEMEN_TYPE.GOLD_COLLECT: break; } }
"文字列型"を"数値型"に変換する。int.TryParse double.TryParse
intに変換したい時
- sample1
public List<AchievementContents> RetrieveAchievement(AchievementManager.ACHIEVEMEN_TYPE type){ List<AchievementContents> items= new List<AchievementContents>(); for(int i = 0 ; i < _myCollection.Count ; i++){ int t = 0; int.TryParse(_myCollection[i]._type,out t ); if((AchievementManager.ACHIEVEMEN_TYPE) t == type ){ items.Add(_myCollection[i]); } } return items; }
- sample2
public void CheckAchievement(ACHIEVEMEN_TYPE type , double value){ List<AchievementContents> items = new List<AchievementContents>(); switch(type){ case ACHIEVEMEN_TYPE.TAP: items = AchievementSource._instance.RetrieveAchievement(type); for(int i = 0 ; i < items.Count ; i++){ if(!items[i]._unlocked){ double t = 0; double.TryParse(items[i]._goal,out t ); if( value >= t ){ items[i]._unlocked = true; //reward // EventManager.onAchievementTap(); Debug.Log("AChievement"); } } } break; } }
参考サイト
【Unity】Object Pool
Perpose
- Recycling Object
- Avoid from making the same object
ObjectPool:きっとゲームに関するブログ@tk_game
http://d.hatena.ne.jp/asakichy/20090406/1238985837d.hatena.ne.jp
sample1
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; public class ScrollPanel : MonoBehaviour { public GameObject _contentContainer; private int _sumAchievement; public AchievementContent _achievementContent; void Awake (){ _achievementContent.CreatePool (); } void Start () { _sumAchievement = AchievementSource._instance._myCollection.Count; List<AchievementContents> _myCollection = AchievementSource._instance._myCollection; float contentHight = 0; for (int i =0; i<_sumAchievement; i++) { AchievementContent content = _achievementContent.Spawn(); content.gameObject.transform.SetParent(_contentContainer.transform); content.gameObject.transform.localScale = new Vector3(1,1,1); content.SetContent(_myCollection[i]._name, _myCollection[i]._type, _myCollection[i]._goal, _myCollection[i]._reward); RectTransform contentTrans = content.GetComponent<RectTransform>(); contentHight = contentTrans.sizeDelta.y + 10; } RectTransform contentRectTrans = _contentContainer.GetComponent<RectTransform> (); contentRectTrans.sizeDelta = new Vector2 (GetComponent<RectTransform> ().sizeDelta.x, contentHight * _sumAchievement); } void Update () { } }
sample2
notifcation を表示させる
sing UnityEngine; using System.Collections; using System.Collections.Generic; public class AchievementNotif : MonoBehaviour { public GameObject _notifContainer; public AchievementContentNotif _achievementContentNotif; #region Event void OnEnable(){ EventManager.onAchievementTapE += ShowBanner; } void OnDisable(){ EventManager.onAchievementTapE -= ShowBanner; } #endregion void Awake (){ _achievementContentNotif.CreatePool (); } void ShowBanner (int i) { List<AchievementContents> _myCollection = AchievementSource._instance._myCollection; AchievementContentNotif notif = _achievementContentNotif.Spawn(); notif.gameObject.transform.SetParent(_notifContainer.transform); notif.gameObject.transform.localScale = new Vector3(1,1,1); notif.SetContent(_myCollection[i]._name, _myCollection[i]._type, _myCollection[i]._goal, _myCollection[i]._reward); } }
using UnityEngine; using System.Collections; using System.Collections.Generic; public sealed class ObjectPool : MonoBehaviour { static ObjectPool _instance; Dictionary<Component, List<Component>> objectLookup = new Dictionary<Component, List<Component>>(); Dictionary<Component, Component> prefabLookup = new Dictionary<Component, Component>(); public static void Clear() { instance.objectLookup.Clear(); instance.prefabLookup.Clear(); } public static void CreatePool<T>(T prefab) where T : Component { if (!instance.objectLookup.ContainsKey(prefab)) instance.objectLookup.Add(prefab, new List<Component>()); } public static T Spawn<T>(T prefab, Vector3 position, Quaternion rotation) where T : Component { if (instance.objectLookup.ContainsKey(prefab)) { T obj = null; var list = instance.objectLookup[prefab]; if (list.Count > 0) { while (obj == null && list.Count > 0) { obj = list[0] as T; list.RemoveAt(0); } if (obj != null) { obj.transform.parent = null; obj.transform.localPosition = position; obj.transform.localRotation = rotation; obj.gameObject.SetActive(true); instance.prefabLookup.Add(obj, prefab); return (T)obj; } } obj = (T)Object.Instantiate(prefab, position, rotation); instance.prefabLookup.Add(obj, prefab); return (T)obj; } else return (T)Object.Instantiate(prefab, position, rotation); } public static T Spawn<T>(T prefab, Vector3 position) where T : Component { return Spawn(prefab, position, Quaternion.identity); } public static T Spawn<T>(T prefab) where T : Component { return Spawn(prefab, Vector3.zero, Quaternion.identity); } public static void Recycle<T>(T obj) where T : Component { if (instance.prefabLookup.ContainsKey(obj)) { instance.objectLookup[instance.prefabLookup[obj]].Add(obj); instance.prefabLookup.Remove(obj); obj.transform.parent = instance.transform; obj.gameObject.SetActive(false); } else Object.Destroy(obj.gameObject); } public static int Count<T>(T prefab) where T : Component { if (instance.objectLookup.ContainsKey(prefab)) return instance.objectLookup[prefab].Count; else return 0; } public static ObjectPool instance { get { if (_instance != null) return _instance; var obj = new GameObject("_ObjectPool"); obj.transform.localPosition = Vector3.zero; _instance = obj.AddComponent<ObjectPool>(); return _instance; } } } public static class ObjectPoolExtensions { public static void CreatePool<T>(this T prefab) where T : Component { ObjectPool.CreatePool(prefab); } public static T Spawn<T>(this T prefab, Vector3 position, Quaternion rotation) where T : Component { return ObjectPool.Spawn(prefab, position, rotation); } public static T Spawn<T>(this T prefab, Vector3 position) where T : Component { return ObjectPool.Spawn(prefab, position, Quaternion.identity); } public static T Spawn<T>(this T prefab) where T : Component { return ObjectPool.Spawn(prefab, Vector3.zero, Quaternion.identity); } public static void Recycle<T>(this T obj) where T : Component { ObjectPool.Recycle(obj); } public static int Count<T>(T prefab) where T : Component { return ObjectPool.Count(prefab); } }
UnityでJsonデータを扱う。
- LitJSONのインストール
http://lbv.github.io/litjson/
LitJson.dllをダウンロード=>UnityプロジェクトのAssets/Pluginsに入れる
- JSONファイル
Assets/Resourcesにディレクトリを作成、そこに.txtか.jsonを入れる
UIカメラとメインカメラを位置を分けるとUnityエディタの管理がしやすい。
- カメラの設定
UI Canvasの分だけカメラを作る。その際に、カメラの位置をずらして、常に確認できるようにする。
複製したCameraのAudio Listenerを消去する。
- Canvasの設定
Canvas - Render Mode [Screen Space - Camera] - Render Camera [指定したいカメラ]
Canvas Scaler - UI Scale Mode [Scale With Screen Size]にする